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:
Navigate through the content with your arrow keys, PAGE UP and PAGE DOWN. Press q to quit the manual.
2. Create a Directory Tarball
Create a directory with nine empty files so that you can test tar commands:
Now switch to your home directory.
To send the contents of a directory to a tar file:
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:
Now append it to the already existing tar archive (
If you open test.tar in a graphical archive manager, you will see the following content.
4. List Contents of tar File
-t flag to list the contents of the tar file.
5. Extract Contents from Tarball
Contents will be extracted in your current directory. Create a new directory and then switch to it:
Now extract test.tar in the current directory:
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.
And create a compressed tar file.
As you can see, the only difference is the added
z parameter, which instructs tar to use gzip to compress the file.
7. Create Even Smaller Compressed Tarball (bzip)
Instead of the
z parameter, you can use
j to compress with bzip.
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:
This extracts only
9. Extract Only Files/Directories that Match a Pattern
If you want to extract all files that end in the .jpg extension:
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:
11. Delete Files or Directories from Tarball
Works only on uncompressed tar files.
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
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.
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.
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:
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.
Our latest tutorials delivered straight to your inbox