Loading
Lesson 05
Courses / Learn Unix Linux Bash Command Line Terminal Tutorial
Using the ZIP Command to Archive and Compress or Decompress Files in Linux

Video Transcript

This lesson I'll teach you how to use the zip command to archiving, compress, and decompress files in a Linux-based system. So you might know the zip format. It's widely used if you come from Windows. In the Unix world, they would typically have tar files that would then be compressed with, for example, gzip or bzip2. But you might be from a different kind of system where they usually use zip files. So let's see how we can make zip files and extract zip files in Linux. The command is going to be zip, and the one for decompressing is going to be unzip. Very simple, very similar to dealing with tar files and gzip. So let's get started. Here I have a file structure with some files. I have notes.txt and sample.jpg, for example. Let's say I want to add that to a zip file. How would I do it? Simply do the following. Say the zip command. Now you have to say the name of the zip file you want to create. Let's say myzip. And you can add the extension here, .zip if you want. And then you have to give the file names you want to add in this example. Let's say notes.txt, space, sample.jpg. So at the end would be a list of files, or one, or a directory, and so on. Now let's see what it created. Created a file called myzip.zip. If you want to check the contents of that, we can use the .zip.l option. So unzip.l with myzip.zip. And it will tell you what it would be extracted if you were to actually run unzip. So there's notes.txt and sample.jpg inside. That's nice. Let's try to add the directory here. Let's say I want to try zip. So one thing with zip is if you want to add more files to the existing one, you can just say zip, name of the existing zip file and the files you want to add to. You might want to contrast that with tar, because I think tar, if you were to use it, it would actually replace the whole thing with a new set of files instead of appending. So be careful that you might want to check that out first before doing anything. Important. So let's add a dear one to the zip file. I'm going to say zip. What's the name of the file? Myzip.zip. And what's the file? Or directory I want to add? Direct everyone. Now you might notice something weird. The length of the directory there is zero and it seems it's empty. And that's because we have to actually recurse over the files inside that directory if you actually want to add to the zip file. So if you were to do it again, you have to use the dash r option so it can actually go through this everything inside dear one because it's a directory. So use the dash r if you're dealing with adding directories. So now you can see the output added notes.txt sample.jpg and image.bmp from the directory dear one. And if you do unzip dash l myzip.zip, you can see it's actually there, not just the empty directory, right? So be careful that don't forget dash r if you want to add a directory to the zip file. And notice the other files remained. It didn't replace everything with just dear one. That's one thing to keep in mind. Okay, so let's let's see. So zip when you use zip it archives and compresses the file. So it's kind of different when you start and then use it, for instance, because in Linux, the standard way would be to first archiving so you can combine all bunch of files into a single file. And then after that, you do the compression to actually reduce the file size. But when you do zip, it does those two things kind of simultaneously at the same time. So you don't have to separate that process. Okay, so let's try to extract my zip into a new directory. I'm going to create make the IR. Dear two, I'm going to see D2 dear two, and there's nothing here yet. I do ls I want to extract the contents of my zip. That's one level up to this current directory. So I'm going to use a command unzip. Now have to specify the file. The file is one level up. So I'm going to say dot dot slash my zip dot zip. And let's see what happens if I do like that. So it said archive inflating inflating creating and so on. If I do ls of the current directory, it's actually here now. The contents let's do a listing recursively dash LR to see what's inside dear one. So dear one actually has the content we wanted. Notes.txt sample.jpg which are different from the ones on one level above. Okay, if we see the dot dot to go back to one level one directory up the tree we have. My zip is still there. No problem. OK, so that's how you use the zip and unzip commands to compress and decompress zip files in a Linux system. You liked the video and don't forget to like and subscribe and I'll see you on the next one. Goodbye.
No comments yet (loading...)
No comments yet (loading...)
Did you like the lesson? 😆👍
Consider a donation to support our work: