GNU Tar is a powerful command line archival utility for Linux and other UNIX-like operating systems. It was first developed in Bell Laboratories to reliably store any digital media on archival tape backups. Nowadays, GNU Tar is an “all-rounder” that stores any file in an easy-to-transfer format.
Download this Cheatsheet
Enter your email below to receive this PDF cheatsheet in your Inbox.
One of the powerful features of GNU Tar is its flexibility with regard to data handling. It provides options that allow you to compress, extract, and even redirect an archive to UNIX pipes. This makes GNU Tar a useful tool for storing data and manipulating them through the command line.
This cheatsheet will show you the commands in GNU Tar that you can use to manipulate Tar files in Linux. Not only that, it will also show some advanced options that will let you peek into the internals of an archive.
Command | Description |
---|---|
Creating Archives | |
tar -cf ./output.tar ./input/ | Create an archive with the name “output.tar” from the folder “input.” |
tar -cf ./device.tar /dev/sda1 | Create an archive with the name “device.tar” using the contents of the entire sda1 partition. |
tar -czf ./output.tar.gz ./input/ | Create an archive named “output.tar” from the folder “input.” |
tar -cjf ./output.tar.bz2 ./input/ | Create an archive named “output.tar.gz” and compress it using the GZIP algorithm. |
tar -cJf ./output.tar.xz ./input/ | Create a new archive with the name “output.tar.xz” and compress it using the XZ algorithm. |
tar -cZf ./output.tar.z ./input/ | Create a new archive named “output.tar.xz” and compress it using the XZ algorithm. |
tar –lzip -cf ./output.tar.lz ./input/ | Create a new archive named “output.tar.z” and compress it using the LZ algorithm. |
tar –lzop -cf ./output.tar.lzo ./input/ | Create a new archive named “output.tar.lz” and compress it using the LZIP algorithm. |
tar -cML 1024000 -f ./output.tar ./large.file | Split the file “large.file” into a set of 1GB multi-part archives. |
tar -hcf ./output.tar ./input.link | Create a new archive named “output.tar.lzo” and compress it using the LZOP algorithm. |
tar –format=v7 -cf ./output.tar ./input/ | Ensure that the “output.tar” archive is compatible to the original V7 UNIX tar. |
tar –sort=name -cf ./output.tar ./input/ | Create a new archive with the name “output.tar” and sort its contents alphabetically. |
tar –warning=none -cf ./output.tar ./input/ | Disable all the warning messages when creating “output.tar.” |
tar –warning=all -cf ./output.tar ./input/ | Print all the warning messages when creating “output.tar.” |
tar -wcf ./output.tar ./input/ | Enable Tar’s interactive mode during archive creation. |
Filtering Archive Members | |
tar –exclude-backups -cf ./output.tar ./input/ | Create an archive but do not include any database lock files as well as binary backup files in the final output. |
tar –exclude-caches -cf ./output.tar ./input/ | Create an archive but do not include any cache file in the final output. |
tar –exclude-vcs -cf ./output.tar ./input/ | Create an archive but do not include any file named “foo” in the final output. |
tar –exclude=foo -cf ./output.tar ./input/ | Create an archive but do not include any file with the name “foo” in the final output. |
tar –exclude-tag-all=foo -cf ./output.tar ./input/ | Create an archive but do not include any folder that contains the file “foo.” |
tar –no-recursion -cf ./output.tar ./input/ | Create an archive but do not descend to any subdirectories in the current folder. |
tar –no-unquote -cf ./output.tar ./”input”/ | Create an archive and treat the quotes in the input folder as literal characters. |
tar -Pcf ./output.tar /home/$USER/input/ | Create an archive and make sure that all of the directory paths inside are absolute. |
tar –newer=2023-06-30 -cf ./output.tar ./input/ | Create an archive but only include files inside the “input” folder that were made after June 30. |
tar –remove-files -cf ./output.tar ./input/ | Create an archive and remove the contents of the “input” folder once the archive is done. |
tar -Wcf ./output.tar ./input/ | Create an archive and verify its integrity after it is done. |
tar –ignore-case -cf ./output.tar ./InpuT/ | Disable the case sensitivity of Tar when creating new archives. |
tar -C /other/directory -cf ./output.tar ./input/ | Move the current working directory of Tar and create an archive using the input folder inside it. |
Checking Archives | |
tar -tf ./output.tar | Print the contents of “output.tar” in a short format. |
tar -tvf ./output.tar | Print the contents of “output.tar” in a long format. |
tar –index-file=foo -tvf ./output.tar | Print the contents of “output.tar” in a long format and save in the file “foo.” |
tar –utc -tvf ./output.tar | Print the contents of “output.tar” and convert all modification times to UTC. |
tar –totals -tvf ./output.tar | Print the contents of “output.tar” and display the total amount of bytes for the archive. |
tar –checkpoint -cf ./output.tar ./input/ | Print the archival process of the current tar file every 10 archive members. |
tar -tvRf ./output.tar | Print the block number for every member in “output.tar.” |
Modifying Archives | |
tar –group=foo -xf ./output.tar | Only include newer content at the end of the “output.tar” archive. |
tar –user=foo -xf ./output.tar | Only include content that is newer at the end of the “output.tar” archive. |
tar -rf ./output.tar ./new-input/ | Include the contents of the folder “new-input” at the end of the “output.tar” archive. |
tar -uf ./output.tar ./input/ | Update the owner of all of the archive’s contents to “foo.” |
tar –checkpoint-action=’cmd’ -cf ./output.tar ./input/ | Run the command “cmd” on the archive every 10 archive members. |
Extracting Archives | |
tar -xf ./output.tar | Extract “output.tar” on the current working directory. |
tar -xkf ./output.tar | Extract “output.tar” but do not replace any currently existing files in the directory. |
tar –keep-newer-files -xf ./output.tar | Extract “output.tar” but do not replace any files that are newer than the archive’s contents. |
tar –no-overwrite-dir -xf ./output.tar | Extract “output.tar” but do not modify the properties of any folder in the current directory. |
tar –keep-directory-symlink -xf ./output.tar | Extract “output.tar” but do not modify any symbolic links in the current directory. |
tar –overwrite -xf ./output.tar | Extract “output.tar” and replace all of the files in the current directory. |
tar -mxf ./output.tar | Extract “output.tar” but do not use the “mtime” values of the archived files during extraction. |
tar –preserve -xf ./output.tar | Extract “output.tar” and do not update the user and group permissions to the current user. |
tar -wxf ./output.tar | Enable Tar’s interactive mode during archive extraction. |
tar –strip-components=3 -xf ./output.tar | Only extract any files inside “output.tar” that are three directories deep. |
tar –transform=s/pattern/string/ -xf ./output.tar | Extract the “output.tar” archive and replace every file with the name “pattern” with “string.” |
tar –warning=none -xf ./output.tar | Disable all the warning messages while extracting “output.tar.” |
tar –warning=all -xf ./output.tar | Print all the warning messages while extracting “output.tar.” |
Image credit: Unsplash. All alterations by Ramces Red.
Our latest tutorials delivered straight to your inbox