FreeKB - Create, view, and extract an archive using the TAR command in Linux
Create, view, and extract an archive using the TAR command in Linux

Home > Search > Linux commands


The tar command can be used to create or extract an archive. 


Create an uncompressed archive

The -c or --create and -f or --file optiions can be used to create a tar archive. In this example, the contents of the /etc directory are stored in an archive named example.tar. tar is recursive, meaning that all files and folders below the target directory will be included in the archive.

tar -cf example.tar /etc 

 

The -v or --verbose option can be used to display the files being added to the archive.

tar -cvf /home/john.doe/example.tar /etc 

/etc/adjtime
/etc/aliases
/etc/aliases.db
. . .

 

The -C or --directory options can be used to specify the directory that will be archived. In this example, example.tar contains an archive of the foo directory.

tar -cf example.tar /opt foo

 


Create a compressed archive

Adding the -z or --use-compress-program=gzip option and the .gz extension will create a gzip compressed archive.

tar -zcf /home/john.doe/example.tar.gz /etc 

 

Adding the -j or --use-compress-program=bzip option and .bz2 extension will create a bzip2 compressed archive.

tar -jcf /home/john.doe/example.tar.bz2 /etc 

 

Or, pigz could be used.

tar --use-compress-program=pigz -cf /home/john.doe/example.tar.pigz /etc 

 

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

 


View the contents of an archive

The -t or --list and -f or --file optiions can be used to view the contents of an uncompressed tar archive. In this example, the example.tar uncompressed archive contains the contents of the /etc directory.

tar -tf /home/john.doe/example.tar

abrt
adjtime
akonadi
aliases
aliases.db
. . .

 

The -z option will be needed if the archive is gzip compressed.

tar -ztf /home/john.doe/example.tar.gz

 

The -j option will be needed if the archive is bz2 compressed.

tar -jtf /home/john.doe/example.tar.bz2

 


Appended files to an archive

The -r or --append option can be used to append files to a tar archive. In this example, the contents of the /var directory is appended to example.tar.

tar -rf /home/john.doe/example.tar /var

 


Extract the contents of an archive

The tar command with the -x or --extract and -f or --file optiions will extract the contents of the tar archive. In this example, the contents of example.tar will be extracted into the present working directory.

tar -xf example.tar

 

The  -C or --directory options can be used to extract the contents to a specific directroy. In this example, both of these commands have the same result, where the contents of example.tar are extracted to /tmp.

tar -xf example.tar -C /tmp
cd /tmp; tar -cf example.tar

 

The -z option will be needed if the archive is gzip compressed.

tar -zxf example.tar.gz

 

The -j option will be needed if the archive is bz2 compressed.

tar -jxf example.tar.bz2

 

To extract a single file in the archive, include the path to the file in the archive.

tar -xf example.tar path/to/example.txt

 


Retain SELinux context

The --selinux option can be used to retain the SELinux context of a file. In this example, example.tar contains the contents of the /etc directory and records the SELinux context of the files in the /etc directory.

tar --selinux -cf /home/john.doe/example.tar /etc 

 


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
gzip -9 tmp.tar

 


Exclude directories

The --exclude option can be used to exclude directories from being included in the tar archive. In this example, a tar archive named example.tar is created, and the tar archive will contain everything below the /home directory except for anything at and below /home/john.doe.

tar -cf example.tar --exclude /home/john.doe /home

 



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




Please enter cbc17 in the box below so that we can be sure you are a human.




Comments