
The tar command is the standard way to archive and compress files and directories on Linux. It’s a bit of a Swiss army knife, with flags for creating, extracting, listing, and adding to archives, so this cheatsheet collects the commands you’ll actually reach for, with a short note on what each flag does.
Create an Archive
tar -cvf archive.tar file1 file2 directory/
This bundles file1, file2, and everything in directory/ into a single archive.tar. The flags are -c to create, -v for verbose output so you can see what’s being added, and -f to name the archive file.
Create a Compressed Archive
Add -z to compress with gzip, which is the most common choice and gives you a .tar.gz:
tar -czvf archive.tar.gz directory/
For better compression at the cost of speed, swap -z for -J to use xz, which produces a smaller .tar.xz:
tar -cJvf archive.tar.xz directory/
Extract an Archive
Use -x to extract. For a plain archive:
tar -xvf archive.tar
For a gzipped one, add -z again to decompress on the way out:
tar -xzvf archive.tar.gz
Modern versions of tar can usually detect the compression on their own, so tar -xvf often works regardless, but being explicit never hurts.
List the Contents
To see what’s inside an archive without extracting it, use -t:
tar -tvf archive.tar
Append Files to an Archive
Use -r to add a file to an existing archive. Note this only works on uncompressed .tar files, not gzipped ones:
tar -rvf archive.tar newfile.txt
That covers the everyday uses. Once you’re comfortable with these, the pattern is easy to remember: c to create, x to extract, t to list, and add z or J whenever compression is involved.


Leave a Reply