Compress and Extract Files Using the tar Command on Linux

Linux Logo

Compress and Extract Files Using the tar Command on Linux

Compress an Entire Directory or a Single File

  • -c: Create an archive
  • -z: Compress the archive with gzip
  • -v: Verbose mode. Display progress in the terminal
  • -f: Allows you to specify the filename of the archive.
tar czvf name-of-archive.tar.gz /path/to/directory-or-file /path/to/another-directory-or-file

Exclude Directories and Files

tar czvf name-of-archive.tar.gz /path/to/directory-or-file --exclude=/home/user/folder-to-exclude --exclude=/home/user/file-to-exclude.txt --exclude=*.iso

Extract an Archive

tar -xzvf archive.tar.gz
tar -xzvf archive.tar.gz -C /home/user/extracted