Code Yarns ‍👨‍💻
Tech BlogPersonal Blog

Tar cheatsheet

📅 2015-Jan-22 ⬩ ✍️ Ashwin Nanjappa ⬩ 🏷️ cheatsheet, tar, untar ⬩ 📚 Archive

Tape ARchive (TAR) is an classic file format that is used to archive files. Note that it does not compress the files to save space. Rather it is only for convenience of handling a single archive file instead of dealing with thousands of files. I typically use it to quickly move directories containing large number of small files.

Note that tar is an incredibly old Unix tool and confusingly accepts arguments in 3 styles: traditional (without dashes), UNIX short-option (single dash) and GNU long-option (double dash). I abhor using the traditional style (which is quite popular online) since it is un-UNIX and instead use the short-option style for interactive use (and below) and use the long-option style in scripts.

$ tar -cf foobar.tar foobar1.txt foobar2.txt
$ tar -cf foobar.tar foobar
$ tar -cf - foobar1.txt foobar2.txt > foobar.tar

$ tar -cf - foobar1.txt foobar2.txt | wc
$ tar --sort="name" -cf foobar.tar foobar

Apparently, this can sometimes result in better compression.

$ tar -cvf foobar.tar foobar
$ tar -xf foobar.tar
$ tar -xf foobar.tar --exclude exclude1
$ mkdir foobar
$ tar -xf haha.tar --directory foobar
$ tar -xf haha.tar -C foobar
$ tar -xf haha.tar
Writes out foo/bar/rocky/zoo.py

$ tar -xf haha.tar --strip-components 2
Writes out rocky/zoo.py
$ tar -xvf foobar.tar
$ tar -tvf foobar.tar
$ tar -cf - foobar1.txt foobar2.txt | openssl enc -e -aes256 -out foobar.tar

You will be asked to enter the password to use to encrypt the contents.

$ openssl enc -d -aes256 -in foobar.tar | tar -x

You will be asked to enter the password to decrypt the contents.

References:

Tried with: Tar 1.27.1 and Ubuntu 14.04


© 2022 Ashwin Nanjappa • All writing under CC BY-SA license • 🐘 @codeyarns@hachyderm.io📧