
Let's say the /tmp/example directory contains two files, foo.txt and bar.txt, like this. Notice each file has a unique owner and permissions.
~]# ls -l /tmp/sample
-rw-r--r-- 1 jane.doe admins 0 Oct 20 03:19 foo.txt
-rwxrwxrwx 1 john.doe admins 0 Oct 20 03:19 bar.txt
The tar command with the --create and --preserve-permissions and --file options can be used to create a tar archive of a directory. tar is recursive, meaning that all files and folders below the target directory will be included in the archive.
tar --create --preserve-permissions --file example.tar /tmp/sample
Or, more commonly, the -cpf flag is used.
tar -cpf example.tar /tmp/sample
The -v or --verbose option can be used to display the files being added to the archive.
tar -cvf example.tar /tmp/sample
. . .
/tmp/sample/
/tmp/sample/bar.txt
/tmp/sample/foo.txt
The tar command with the -t or --list and -f or --file optiions can be used to view the contents of the tar achive.
tar -tf example.tar
Which will produce the following.
tmp/sample/
tmp/sample/bar.txt
tmp/sample/foo.txt
Present Working Directory
The tar command does take into consideration your present working directory. Notice in this example that tmp/ is not included in the tar archive, since we are in the /tmp directory.
cd /tmp
tar -cvpf example.tar sample
. . .
sample/
sample/foo.txt
sample/bar.txt
Compression (gzip, bzip, pigz)
Using the -z or --gzip option will create a gzip compressed archive.
tar --create --gzip --preserve-permissions --file example.tar.gz /tmp/sample
Or like this, using short hand -zcpf and the .tgz file extension.
tar -zcpf example.tgz /tmp/sample
When the archive is gzip compressed, the -z option will need to be used to view the contents of the archive, like this:
tar -ztf example.tar.gz
Adding the -j or --use-compress-program=bzip option and .bz2 extension will create a bzip2 compressed archive.
tar -jcpf example.tar.bz2 /tmp/sample
When the archive is gzip compressed, the -z option will need to be used to view the contents of the archive, like this:
tar -jtpf example.tar.gz
Or, pigz could be used.
tar --use-compress-program=pigz -cpf example.tar.pigz /tmp/sample
In this example, the uncompressed archive is 16179200 bytes, the gzip compressed archive is 3466105 bytes, and the bzip2 compressed archive is 289751 bytes.
ls -l
. . .
-rw-rw-r--. 1 john.doe john.doe 16179200 May 8 12:53 example.tar
-rw-rw-r--. 1 john.doe john.doe 3466105 May 8 12:55 example.tar.gz
-rw-rw-r--. 1 john.doe john.doe 2897541 May 8 12:58 example.tar.bz2
Compression level
Tar does not have an option to specify the compress level, such as 1 for the least compression, or 9 for the most compression. However, you can create a tar archive, and then use gzip or bzip2 to specify the compression level. For example, a tar archive of the tmp directory can be created, and then the tmp.tar archive can be compressed at level 9.
tar -cf tmp.tar /tmp
gzip -9 tmp.tar
Excluding directories
The --exclude option can be used to exclude directories. In this example, /tmp/sample/foo will be excluded.
tar -cpf example.tar --exclude="/tmp/sample/foo" /tmp/sample
Did you find this article helpful?
If so, consider buying me a coffee over at