tar flags & options

-c, --create

Create a new archive.

tar -cf archive.tar file1 file2 dir/
tar -czf archive.tar.gz src/

-x, --extract

Extract files from an archive.

tar -xf archive.tar
tar -xzf archive.tar.gz

-t, --list

List the contents of an archive without extracting.

tar -tf archive.tar
tar -tzf archive.tar.gz

-f, --file

Specify the archive filename. Almost always required.

tar -cf output.tar input/
tar -xf input.tar

-v, --verbose

List files as they are processed.

tar -xvf archive.tar
tar -cvzf archive.tar.gz src/

-z, --gzip

Compress or decompress using gzip. Use with .tar.gz or .tgz files.

tar -czf archive.tar.gz src/
tar -xzf archive.tar.gz

-j, --bzip2

Compress or decompress using bzip2. Use with .tar.bz2 files.

tar -cjf archive.tar.bz2 src/
tar -xjf archive.tar.bz2

-J, --xz

Compress or decompress using xz. Use with .tar.xz files. Best compression ratio.

tar -cJf archive.tar.xz src/
tar -xJf archive.tar.xz

-C, --directory

Change to a directory before extracting or creating.

tar -xzf archive.tar.gz -C /opt/app/
tar -czf archive.tar.gz -C /home/user project/

--exclude

Exclude files matching a pattern.

tar -czf archive.tar.gz --exclude="*.log" --exclude="node_modules" src/

--strip-components

Strip N leading path components when extracting. Useful for removing a top-level directory.

tar -xzf archive.tar.gz --strip-components=1