15 Tar Commands You Should Try in Linux

Sometimes you need to pack multiple files and/or directories into a single file. It’s easier to transfer a single file over a network and even a USB stick (creating thousands of files on flash memory can be slow). On operating systems such as Windows, this is typically done by archiving into .zip (zipping) or .rar files. On *nix operating systems, such as Linux, the go-to utility is “tar.” The name of this command is derived from Tape ARchive, since it was initially designed for backing up data on magnetic tape. Nowadays it can still be used for this purpose, but in most cases it’s simply a method of creating so-called “tarballs.” The purpose of tarballs is to facilitate uploading, downloading and moving data around, or keeping backups.

1. Consult tar Manual

Every command in Linux has a manual page. You can consult tar’s manual by entering this into your terminal:

man tar

tar-manual

Navigate through the content with your arrow keys, PAGE UP and PAGE DOWN. Press q to quit the manual.

Also read: 6 of the Most Useful Linux Commands for New Users

2. Create a Directory Tarball

Create a directory with nine empty files so that you can test tar commands:

mkdir $HOME/test9 && touch $HOME/test9/{1..9}

tar-test-directory

Now switch to your home directory.

cd

To send the contents of a directory to a tar file:

tar -cvf test.tar test9
  • c – stands for create
  • v – verbose, outputs what file is currently added to the archive; useful for long running jobs, to see progress
  • f – tells the utility to create a tar file, with the name specified, test.tar

3. Append Content to an Existing Tarball

Create another directory with empty files named from a to z:

mkdir $HOME/test-az && touch $HOME/test-az/{a..z}

tar-test-az

Now append it to the already existing tar archive (r parameter):

tar -rvf test.tar test-az

If you open test.tar in a graphical archive manager, you will see the following content.

tar-xarchiver

4. List Contents of tar File

Use the -t flag to list the contents of the tar file.

tar -tf test.tar

5. Extract Contents from Tarball

Contents will be extracted in your current directory. Create a new directory and then switch to it:

mkdir $HOME/test-extract && cd $HOME/test-extract

Now extract test.tar in the current directory:

tar -xvf $HOME/test.tar

tar-extracted-contents

6. Create Compressed Tarball (gzip)

If you intend to transfer the tar file through the network, making it smaller is good practice, to increase transfer speed. Change back to your home directory.

cd

And create a compressed tar file.

tar -cvzf test.tar.gz test9

As you can see, the only difference is the added z parameter, which instructs tar to use gzip to compress the file.

tar-file-sizes-compared

Also read: How to Easily Remember Linux Commands

7. Create Even Smaller Compressed Tarball (bzip)

Instead of the z parameter, you can use j to compress with bzip.

tar -cvjf test.tar.bz test9

This will further reduce the file size but take longer to compress and decompress.

8. Extract a Single File or Directory

Just append the exact name of the file or directory, as it appears in the tar archive:

tar -xvf test.tar test9/7

This extracts only test9/7 file.

9. Extract Only Files/Directories that Match a Pattern

If you want to extract all files that end in the .jpg extension:

tar -xvf test.tar --wildcards '*.jpg'

10. Preserve Permissions

Permissions are unimportant when creating tarballs out of documents, pictures and similar media. But if you are backing up your system, you can add the p parameter to preserve file permissions:

tar -cvpf test-permissions.tar test9

11. Delete Files or Directories from Tarball

Works only on uncompressed tar files.

tar -f test.tar --delete test-az

This removes the test-az directory from the tarball, including the files it contains. tar f test.tar --delete test-az/z will just remove the “test-az/z” file.

12. Exclude Files or Directories from Being Added to Tarball

tar -cvf test-exclude.tar --exclude test9/9 test9

tar-exclude

As you can see, the file 9 was excluded from the tar archive. Multiple exclude parameters can be added: tar cvf test-exclude.tar --exclude test9/9 --exclude test9/8 test9.

13. Stay in One Filesystem

--one-file-system can be added in a command such as tar -cvpzf backup.tar.gz --one-file-system /.

This would back up your root filesystem, excluding other filesystems you may have mounted in /home, /mnt, /media, /proc and so on.

Also read: Getting Started with Awk Command

14. Preserve Extended Attributes

Besides file permissions, some Linux distros also have extended file attributes. To preserve all of them, add these parameters: --acls --selinux --xattrs.

Example command:

tar -cvpzf backup.tar.gz --one-file-system --acls --selinux --xattrs /

15. Create New Directory for Extracted Files

Normally, tar extracts into your current directory. You can mkdir new_directory and cd new_directory before using tar, if the directory where want to extract to doesn’t exist yet. But instructing tar to do everything for you is more elegant:

tar -xvf test.tar --one-top-level=new_directory

Conclusion

These are the most commonly used tar commands. But the utility is versatile and can be used in many other ways. If you don’t like reading manuals in the terminal, you may find the HTML version of the tar manual easier to browse.

Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox

Alexandru Andrei

Fell in love with computers when he was four years old. 27 years later, the passion is still burning, fueling constant learning. Spends most of his time in terminal windows and SSH sessions, managing Linux desktops and servers.