Archive, Compress, and Extract Files in Linux Using the Command Line

Traducciones al Español
Estamos traduciendo nuestros guías y tutoriales al Español. Es posible que usted esté viendo una traducción generada automáticamente. Estamos trabajando con traductores profesionales para verificar las traducciones de nuestro sitio web. Este proyecto es un trabajo en curso.
Create a Linode account to try this guide with a $ credit.
This credit will be applied to any valid services used during your first  days.

tar and gzip provide a standard interface for creating archives and compressing files on Linux. These utilities take a large number of files, save them together in an archive, and compresses the archive to save space. tar does not compress files by itself. Used in conjunction with gzip, an archived file can be compressed to reduce disk space. The resulting archived file has the file extension, tar.gz and is sometimes called a “tarball”.

Archive a Directory

  1. Make a directory on your system and create a text file:

    mkdir testdir && touch testdir/example.txt
    
  2. Use tar to archive the directory:

    tar -cvf testdir.tar testdir/
    
  3. Check for the newly archived file:

    ls
    
    tesdir  testdir.tar

Compression with gzip

  1. Compress the file using gzip:

    gzip testdir.tar
    
  2. Checking for the file will show:

    ls
    
    testdir  testdir.tar.gz
  3. The chained file extension (.tar.gz) indicates that this is a compressed archive. You can see the difference in size between the two files:

    ls -l --block-size=KB
    
    total 9kB
    drwxrwxr-x 2 linode linode 5kB Jan 30 13:13 testdir
    -rw-rw-r-- 1 linode linode 1kB Jan 30 13:29 testdir.tar.gz

Extract a Tarball

Extract the directory:

tar -xzvf testdir.tar.gz
testdir/
testdir/test.txt

The flags used in these example stand for:

  • -c: Create a new archive in the form of a tar file.
  • -v: Verbose flag, outputs a log after running the command.
  • -z: Zips or unzips using gzip.
  • -x: Extract a file from the archive.
  • -f: Define STDOUT as the filename, or uses the next parameter.

Common Options for Archiving

Additional flags used with the tar command are:

FlagFunction
-AAppend tar files to an existing archive.
-dShow differences between an archive and a local filesystem.
-deleteDelete from the archive.
-rAppend files to the end of an archive.
-tList the contents of an archive.
-uAppend but don’t overwrite the current archive.

These are the basics for working within the command line. Be sure to check the man pages man tar for a more detailed listing of possible flags when compressing and extracting files.

This page was originally published on


Your Feedback Is Important

Let us know if this guide was helpful to you.


Join the conversation.
Read other comments or post your own below. Comments must be respectful, constructive, and relevant to the topic of the guide. Do not post external links or advertisements. Before posting, consider if your comment would be better addressed by contacting our Support team or asking on our Community Site.