Using Zip and Tar on Command Line

Video loading...

  • 0:02
    Using Zip and Tar on Command Line with Addison Berry
  • 0:08
    Welcome to the next video in the Command Line Basics series.
  • 0:12
    In this video, we're going to be looking
  • 0:13
    at file compression and archiving.
  • 0:15
    Specifically, we will be looking at the zip,
  • 0:17
    gzip and tar commands for various ways to squish
  • 0:21
    your files up and moosh them together.
  • 0:23
    So we're going to start off here,
  • 0:24
    and I've created a copy of my stuff folder and called it zip.
  • 0:28
    So I'm going to go into this folder so we can play around
  • 0:31
    without destroying the stuff on my machine.
  • 0:35
    And so, you can see we just have a whole bunch of files in here,
  • 0:38
    and I'm just going to play around with these.
  • 0:41
    Now, if I do an ls -al to get that more
  • 0:46
    detailed listing that I want to have, you can see not only the files
  • 0:51
    that we have and the fact that we have a directory in here,
  • 0:55
    but we can also see file sizes, and we'll be looking at that
  • 0:59
    as we go through this lesson.
  • 1:01
    Many people are familiar with zip format,
  • 1:05
    and that's the first thing that we're going
  • 1:06
    to be looking at today.
  • 1:08
    And this is common on Windows machines,
  • 1:12
    and it's now common on most modern *nix systems, as well,
  • 1:15
    that you can use zip.
  • 1:17
    So we're going to just use the zip command,
  • 1:20
    and then the first thing we need to put after the zip command,
  • 1:23
    the first argument here, is we need to put the name
  • 1:26
    of the new zip file that we want to create.
  • 1:28
    So, once I zip something up, it's going to be named
  • 1:33
    form_alter.zip. And then this is the file.
  • 1:37
    form_alterChat.txt is the one I want to do.
  • 1:39
    And you can tell right there it told me how much
  • 1:41
    it actually deflated, squished things down.
  • 1:44
    So if I do another ls -al now, you'll see I have,
  • 1:48
    here's my original file, and the zip file is right above it
  • 1:51
    and you can see the difference in file size there.
  • 1:54
    So it wasn't a big file to begin with, but it was text,
  • 1:56
    and those compress really well.
  • 1:58
    You can see we cut it down to less than half.
  • 2:03
    That's simple, to create a zip.
  • 2:07
    Now, to undo that and get the file back out again,
  • 2:11
    all you need to do is unzip it.
  • 2:13
    So, I'll unzip the file I just created.
  • 2:17
    Now, it's asking me, do I want to replace it?
  • 2:19
    Because when it unzips it, it's saying, hey,
  • 2:21
    you already have a file with that name.
  • 2:23
    You can choose what you want to do;
  • 2:24
    it gives you some options, which is pretty cool.
  • 2:27
    I'm just going to go ahead and replace it,
  • 2:29
    because that's fine.
  • 2:30
    It's the same file.
  • 2:32
    Inflates it, done.
  • 2:33
    And it replaced the file for me.
  • 2:36
    And now when we list, you can see the zip file
  • 2:38
    still exists and the text file is there.
  • 2:44
    So, an important thing to remember is that it didn't
  • 2:46
    destroy my zip file.
  • 2:47
    Zip file is still there, and it extracted the file
  • 2:50
    that was in there, as well.
  • 2:53
    So, very handy.
  • 2:54
    The other really popular compression you can do on
  • 2:57
    a *nix system uses the gzip command.
  • 3:00
    So, that stands for GNU zip, which would be the free
  • 3:02
    and open source version of zip.
  • 3:05
    And so, on older systems that don't have zip for some reason,
  • 3:10
    you can always use gzip.
  • 3:12
    Now, when you use gzip, the only thing I'm putting
  • 3:14
    after that is the file.
  • 3:16
    I'm not giving it a destination file name
  • 3:18
    or anything like that.
  • 3:18
    I'm just saying, I want you to zip up this file.
  • 3:21
    And gzip is just going to go ahead and crunch it up for me.
  • 3:26
    So, that's it.
  • 3:27
    I didn't get any feedback.
  • 3:31
    And whoops, I can't type ls.
  • 3:35
    Now when I go to do my ls -al.
  • 3:39
    Now you can see here, it actually replaced my text
  • 3:42
    file with the gzipped version, so I don't have just the plain
  • 3:46
    text file anymore, like I did when I did the zip.
  • 3:49
    You can also see the file size here.
  • 3:50
    It's even smaller than the zip, so it's even a little bit
  • 3:55
    better compression that happened there as well.
  • 3:57
    But it just took the file, replaced the file,
  • 3:59
    and appended gz.
  • 4:00
    Now, to undo it, surprisingly, you're going to use gunzip.
  • 4:05
    So I'm going to do the same thing.
  • 4:07
    gunzip, put the file name that I want to open up
  • 4:11
    and go ahead and run that.
  • 4:16
    And now that we have that, when I do my ls,
  • 4:22
    we can see that I have the .txt file again.
  • 4:27
    And notice that the .gz file is gone.
  • 4:30
    So, when you use gunzip, it actually replaces the file,
  • 4:33
    whereas when you use zip, it sort of creates a separate
  • 4:36
    thing and keeps them separated and retains them as you go.
  • 4:40
    So, just something to really keep in mind,
  • 4:42
    that you're sort of overwriting and replacing
  • 4:44
    things when you use gzip.
  • 4:45
    The next thing we're going to look at is archiving.
  • 4:48
    And we're going to look at the tar command,
  • 4:51
    which creates tarballs
  • 4:52
    you may have heard of.
  • 4:54
    And pretty much what this is going to do,
  • 4:57
    instead of actually squishing the file size,
  • 4:58
    it just takes a bunch of files and puts them together
  • 5:00
    into one for you.
  • 5:02
    And so, tar is the command, and then, after that,
  • 5:07
    you put a series of letters to indicate what
  • 5:10
    the tar command is supposed to do.
  • 5:12
    Now I started off with a dash.
  • 5:14
    You don't have to do that on modern systems.
  • 5:16
    The old-school way was to put the dash, so I just still do it,
  • 5:20
    but you don't need to anymore, normally.
  • 5:22
    c is for create, v is for verbose
  • 5:25
    to give us some feedback to the screen,
  • 5:27
    and then f is file, saying I want to make a file here.
  • 5:30
    And then, this is my filename that I want to create.
  • 5:36
    I'm going to make something called form stuff.tar
  • 5:39
    for the extension there.
  • 5:43
    And then, what do I want to put in there?
  • 5:44
    I'm just going to put anything that begins with the word form.
  • 5:46
    This is just a simple way for me to do,
  • 5:48
    rather than listing out all the different files.
  • 5:50
    So, I'm just doing a form and then an asterisk.
  • 5:53
    Now, that verbose gives me that list,
  • 5:54
    so it actually shows me what it got.
  • 5:56
    That's what that v did for me, is it actually made it print
  • 6:00
    this list to the screen so I could see what it
  • 6:01
    was really doing, which is handy.
  • 6:04
    So now, if we go ahead and do an ls,
  • 6:08
    we can see that I now have this new file, form_stuff.tar.
  • 6:13
    It didn't do anything to the actual files.
  • 6:15
    It just created a new file which happens to contain
  • 6:18
    all of those other 3 in 1.
  • 6:23
    So now what we want to do is extract that,
  • 6:26
    get that stuff out, and instead of untar,
  • 6:28
    we're just going to use tar again.
  • 6:29
    There's no "un" part.
  • 6:31
    And this time, I'm going to do xvf rather than c.
  • 6:34
    x for extract rather than c for create,
  • 6:37
    and then put my file name.
  • 6:39
    I still put the v for verbose, so it'll show me which files
  • 6:43
    it's extracted.
  • 6:44
    So, that's awesome.
  • 6:45
    And now I'll do an ls, and you'll see.
  • 6:51
    The tar file still exists, so doing a tar extract
  • 6:54
    doesn't destroy the tar file; it keeps it there.
  • 6:56
    The other thing to keep in mind is that these files
  • 7:00
    actually got overwritten.
  • 7:01
    It didn't warn me, didn't ask me, like it did when I did zip.
  • 7:04
    It just overwrote those files when it extracted.
  • 7:08
    Really important thing to keep in mind.
  • 7:10
    Very, very handy sometimes when you just want to replace
  • 7:13
    a whole bunch of stuff, but you just need to keep
  • 7:18
    that in mind so that you don't accidentally
  • 7:19
    overwrite files that you didn't intend to.
  • 7:21
    Now, the other thing I want to show here,
  • 7:24
    to kind of wrap things up, is that you can also do
  • 7:27
    compression and archiving at the same time by adding
  • 7:30
    a z into the command.
  • 7:31
    So, you can see I have c, v for verbose, z for zipped,
  • 7:36
    and then f for file. And then,
  • 7:38
    the z for zip is not using zip command;
  • 7:40
    it's actually using the gzip command.
  • 7:43
    So it uses z, but it's using gzip.
  • 7:46
    Keep that straight.
  • 7:48
    And again, what I'll do is just put in the name,
  • 7:52
    but this time for the extension, I'm going to do tar.gz.
  • 7:57
    The short form of this, though, is to just to tgz,
  • 8:00
    and this is the most common extension that you'll see
  • 8:04
    for what we call tarballs.
  • 8:08
    Either of those extensions will work,
  • 8:10
    and tar will recognize it when it goes to extract.
  • 8:12
    Same thing, going to put them all in here.
  • 8:15
    You'll notice it actually included the tar file that
  • 8:19
    I had created previously and stuff like that,
  • 8:22
    so this one has even more stuff.
  • 8:23
    A duplicate of it, but you know, anyway.
  • 8:28
    So, I've gone ahead and created this.
  • 8:30
    This is actually a compressed archive,
  • 8:34
    rather than just a plain archive like it was before.
  • 8:36
    If I do this ls -al, and we look at the file sizes.
  • 8:40
    If you look at the,
  • 8:43
    so, I have form_stuff.tar, which was not compressed,
  • 8:47
    and then I have the tgz.
  • 8:49
    Now, the tarball is bigger in size,
  • 8:53
    if you actually look at the numbers,
  • 8:55
    but keep in mind that the compressed one also has a
  • 9:00
    full copy of the other, so it actually is a duplicate.
  • 9:03
    So, it would have been twice the size,
  • 9:07
    and it still got compressed down.
  • 9:09
    So you can see you have this, plus the actual other file.
  • 9:14
    So multiply that by 2, and that would be
  • 9:16
    the actual file size.
  • 9:18
    And then when it got compressed, it actually got squished down
  • 9:21
    from 16 to 13.
  • 9:23
    So, anyway, there's the compressed tarball.
  • 9:27
    And again, when we come to extracting, we just use tar,
  • 9:30
    we use x for extraction rather than c.
  • 9:35
    Put the z in there again so it knows that it needs to
  • 9:37
    uncompress as well as extract, and the verbose gives me my list.
  • 9:44
    We'll do a little ls -al.
  • 9:47
    And again, it didn't destroy my tarball,
  • 9:49
    but it did overwrite all of those files for me.
  • 9:54
    So, just keep that in mind.
  • 9:59
    Real quickly, to wrap up, I just want to point out from
  • 10:01
    way back at the very first lesson,
  • 10:02
    you can do a man on tar, and you see all the interesting
  • 10:08
    stuff that you can do.
  • 10:09
    You can see there are lots of options here.
  • 10:11
    I'm just going to scroll down a little bit.
  • 10:12
    But they have some examples, so you can always sort of jog your
  • 10:16
    memory by just popping open man and looking at the
  • 10:18
    examples and reading through to see what's going on.
  • 10:21
    So that's it for archiving and compression,
  • 10:23
    and I'll see you next time for Command Line Basics.
Loading ...

Using Zip and Tar on Command Line

Loading...

This command line video covers the three most common commands for compressing and archiving files, including how to get them back out again. Most commonly you first hit the need for these when you download a file and need to get it uncompressed. We'll cover the following commands:

  • zip
  • unzip
  • gzip
  • gunzip
  • tar

Note: this video was originally released October 28, 2009 on Lullabot.com.

Additional resources:
There are no resources for this video. If you believe there should be, please contact us.