Common GNU Tar Commands Cheatsheet

A photograph of the internals of a slim hard drive.

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.

Common GNU Tar Commands Cheatsheet Download

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.

CommandDescription
Creating Archives
tar -cf ./output.tar ./input/Create an archive with the name “output.tar” from the folder “input.”
tar -cf ./device.tar /dev/sda1Create 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.fileSplit the file “large.file” into a set of 1GB multi-part archives.
tar -hcf ./output.tar ./input.linkCreate 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.tarPrint the contents of “output.tar” in a short format.
tar -tvf ./output.tarPrint the contents of “output.tar” in a long format.
tar –index-file=foo -tvf ./output.tarPrint the contents of “output.tar” in a long format and save in the file “foo.”
tar –utc -tvf ./output.tarPrint the contents of “output.tar” and convert all modification times to UTC.
tar –totals -tvf ./output.tarPrint 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.tarPrint the block number for every member in “output.tar.”
Modifying Archives
tar –group=foo -xf ./output.tarOnly include newer content at the end of the “output.tar” archive.
tar –user=foo -xf ./output.tarOnly 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.tarExtract “output.tar” on the current working directory.
tar -xkf ./output.tarExtract “output.tar” but do not replace any currently existing files in the directory.
tar –keep-newer-files -xf ./output.tarExtract “output.tar” but do not replace any files that are newer than the archive’s contents.
tar –no-overwrite-dir -xf ./output.tarExtract “output.tar” but do not modify the properties of any folder in the current directory.
tar –keep-directory-symlink -xf ./output.tarExtract “output.tar” but do not modify any symbolic links in the current directory.
tar –overwrite -xf ./output.tarExtract “output.tar” and replace all of the files in the current directory.
tar -mxf ./output.tarExtract “output.tar” but do not use the “mtime” values of the archived files during extraction.
tar –preserve -xf ./output.tarExtract “output.tar” and do not update the user and group permissions to the current user.
tar -wxf ./output.tarEnable Tar’s interactive mode during archive extraction.
tar –strip-components=3 -xf ./output.tarOnly extract any files inside “output.tar” that are three directories deep.
tar –transform=s/pattern/string/ -xf ./output.tarExtract the “output.tar” archive and replace every file with the name “pattern” with “string.”
tar –warning=none -xf ./output.tarDisable all the warning messages while extracting “output.tar.”
tar –warning=all -xf ./output.tarPrint all the warning messages while extracting “output.tar.”

Image credit: Unsplash. All alterations by Ramces Red.

Ramces Red Avatar

Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox

Read next

In 2016, archaeologists dated two rings of snapped stalagmites in France’s Bruniquel Cave to 176,500 years ago, evidence that Neanderthals had walked 336 metres into darkness with fire and built architecture deep underground long before modern humans reached Europe
Otto von Bismarck was 74 when Germany adopted the world’s first national old-age social insurance program in 1889, setting the pension age at 70 after years of fighting socialists with bans, laws, and a promise few workers would live long enough to use
When cosmonaut Valeri Polyakov stepped out of his Soyuz capsule in March 1995 after 437 consecutive days aboard Mir, doctors recorded him at several centimetres above his pre-flight height, and his spine had become so unaccustomed to gravity that the recovery team carried him to a chair rather than risk the compression of letting him walk.
When Harvard astronomer Cecilia Payne submitted her 1925 doctoral thesis arguing that the Sun was made almost entirely of hydrogen, the field’s senior figure Henry Norris Russell talked her into adding a line calling the result ‘almost certainly not real,’ and then published the same conclusion himself four years later to widespread acclaim.
When Edme Mariotte stared at marks on a wall in the 1660s, one mark vanished inside a six-degree hole where the optic nerve leaves the eye and the brain has been filling in wallpaper, sky, and faces ever since
When seismic waves from the Chicxulub impact reached what is now North Dakota roughly ten minutes after the asteroid struck, they appear to have triggered a ten-metre standing wave in an inland river that flung fish onto the bank and buried them under glass beads still falling from the sky.
When survivors near Lake Nyos woke on the morning of 22 August 1986, the cattle were dead in the fields, the birds had fallen out of the trees, and 1,746 of their neighbours were lying where they had stood the night before, with no fire, no flood, and no wound to explain it.
In 1959, a Soviet research team in Novosibirsk began breeding silver foxes for nothing but tameness, and within forty generations the animals had floppy ears, curled tails, piebald coats, and a bark, traits no one had selected for but which appeared on their own once fear was removed.