UbuntuFree.com — Get more Linux Cheat Sheets free

Linux Tar Command Cheat Sheet

Basic Tar Command Syntax

tar [options] [archive_name.tar] [files/directories]

1. Creating Archives (Compression)

Command Description tar -cvf archive.tar file1 file2 Create a .tar archive with multiple files tar -czvf archive.tar.gz folder/ Create a gzip-compressed .tar.gz archive tar -cjvf archive.tar.bz2 folder/ Create a bzip2-compressed .tar.bz2 archive tar -caf archive.tar.xz folder/ Create an xz-compressed .tar.xz archive

2. Extracting Archives (Decompression)

Command Description tar -xvf archive.tar Extract .tar archive tar -xzvf archive.tar.gz Extract .tar.gz archive tar -xjvf archive.tar.bz2 Extract .tar.bz2 archive tar -xvf archive.tar -C /path/to/directory Extract to a specific directory

3. Viewing Archive Contents

Command Description tar -tvf archive.tar List contents of .tar archive tar -tzvf archive.tar.gz List contents of .tar.gz archive

4. Adding & Removing Files from Archives

Command Description tar -rvf archive.tar newfile.txt Append a file to an existing archive tar --delete -f archive.tar file1 Remove a file from an archive (Only works on uncompressed tar files)

5. Extracting Specific Files

Command Description tar -xvf archive.tar file1 Extract a specific file from archive tar -xzvf archive.tar.gz folder/file2 Extract a file from compressed archive

6. Advanced Usage Examples

Command Description tar -czvf - folder/ | ssh user@remote "cat > backup.tar.gz" Compress a folder and send to a remote server

tar -cvf - folder/ | pv | gzip > archive.tar.gz Show progress bar while compressing

tar -czvf archive.tar.gz --exclude='*.log' folder/ Create archive excluding specific file types

Commonly Used Tar Options

Option Description

-c Create a new archive

-x Extract files from an archive-v Verbose mode (list files being processed)-f Specify archive file name-z Use gzip compression

-j Use bzip2 compression

-a Automatically determine compression from file extension-t List archive contents