tar (tape archive) is the standard Unix tool for bundling files into a single archive. It is commonly combined with gzip or bzip2 compression for backups, deployments, and sharing file collections. Mastering tar flags is essential for any Linux workflow.
LinuxtarArchiveCLItar -cvf archive.tar files/
-c creates an archive, -v shows verbose output (filenames being added), and -f specifies the output filename. This produces an uncompressed .tar archive of the files/ directory.tar -czvf archive.tar.gz files/
-z compresses with gzip, producing a .tar.gz (also called a tarball). This is the most common archive format on Linux — it offers a good balance of compression speed and ratio.tar -cjvf archive.tar.bz2 files/
-j flag uses bzip2 compression, which produces smaller files than gzip at the cost of more CPU time. Use .tar.bz2 when archive size matters more than compression speed.tar -czvf backup.tar.gz /etc /var/www /home/user
tar -xzvf archive.tar.gz
-x extracts files, -z handles gzip, -v shows progress, and -f specifies the archive. Files are extracted into the current directory by default.tar -xzvf archive.tar.gz -C /destination/
-C flag sets the extraction destination directory. The target directory must already exist. This avoids cluttering your current working directory when extracting large archives.tar -xzvf archive.tar.gz path/to/file.txt
tar -tzvf archive.tar.gz first to list all files and find the exact internal path you need.tar -tzvf archive.tar.gz
-t lists the archive contents without extracting. This shows every file path, size, and timestamp inside the archive. Use this to verify an archive before extracting or to find the path of a specific file.tar -xzvf archive.tar.gz --strip-components=1
--strip-components=1 flag removes the top-level directory from the extracted paths. Useful when an archive wraps everything in a version-named folder (e.g., myapp-1.0/) and you want the contents directly.tar -rvf archive.tar newfile.txt
-r flag appends files to an existing uncompressed .tar archive. Note: you cannot append to a compressed archive (.tar.gz); you must extract, add, and recompress.-c (create), -x (extract), -t (list), -f (filename).-z for gzip (.tar.gz) or -j for bzip2 (.tar.bz2) compression.-v to see progress when working with large archives.-C /path/ to extract to a specific directory instead of the current one.-t before extracting to avoid surprises..tar is an uncompressed archive (just bundles files). .tar.gz adds gzip compression (faster, widely compatible). .tar.bz2 uses bzip2 (better compression ratio, slower). Most Linux software distributions use .tar.gz or the newer .tar.xz format.
Use the --exclude flag: tar -czvf archive.tar.gz ./project --exclude="./project/node_modules" --exclude="./project/.git". You can use multiple --exclude flags and wildcards.
Yes! Combine tar with SSH for efficient transfers: tar -czvf - ./files | ssh user@host 'tar -xzvf - -C /destination/'. The dash - uses stdout/stdin instead of a file, enabling streaming without a temporary archive file.