How to TAR a file or folder, GZIP, and UNTAR (create TAR files archive)

If you’re running a Linux based server, here a step by step guide on how to tar a file and then how to gzip (compress) that file using a simple SSH command.

When you are logged into your server through SSH, you can use this command line to tar and then gzip your folder into a tarball file. This will compress your file into the current directory

  • tar -czf whatever.tar foldername
    (another method would be…)
  • tar -czf whatever.tar.gz foldername

If you’d like to tar your file and have it put in another location use this:

  • tar -czf /directory/directory/whatever.tar foldername

Now that you have a tar file, here’s how to untar / ungzip (or unzip, uncompress) a file in SSH. Using this command you’ll untar and uncompress your file, then it will place that folder in the directory you are currently in restoring your old folder and file structure.

  • tar -xf whatever.tar
    (another method would be…)
  • gunzip -dc whatever.tar.gz | tar xvf –

Here are a list of tar options, and their significance.

-c = create
-f = read to/from the named file (instead of the device /tape)
-t = list contents of .tar file
-r = append to a .tar file
-v = verbose (tells you everything its doing)
-x = extract contents of .tar file
-z = compress files

Want to learn more SSH commands? I recommend the book Linux in a Nutshell

If you’re a server admin, another thing you might be interested in is how to clear the qmail queue.

10 thoughts on “How to TAR a file or folder, GZIP, and UNTAR (create TAR files archive)

  1. Very helpful, thanks!
    Made things so much easier. Just for one problem. I connected to one linux server via putty then to another via ssh. Trying to transfer files from one to the other. After compressing i am trying to transfer using the command
    get filename.tar.gz
    but am getting the response ‘-bash: get: command not found’
    What could be the reason? Is this correct way to download from one server to another?

  2. This is great. Thanks. If I tar only a particular folder and then extract it to restore, it will not delete any files – only write over and add files within that directory – correct?

    • You’re correct. If you have an existing folder, and you untar an archive with different files, it will add the new files to the existing folder and leave existing files there. If there’s a file with the same name it will prompt if you want to overwrite the existing file.

      Also doing this in reverse… you can add files to an existing tar file. Just use the option “–append” (or “-r“) and it will add new files to an existing tar archive (instead of overwriting the existing tar)

  3. Got error Message when i tried to use tar -czvf
    tar: z unknown function modifier

    Im using Solaris 10 btw and had to use tar -cvf

    Good day!

    • Correct syntax is tar -cvzf dir or filename filename.tar.gz

      -c – create archive
      -v – verbose mode, it show you all operations in real time
      -z – compress
      -f- output filename

Leave a Reply

Your email address will not be published. Required fields are marked *