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

Home > Search > Linux commands
  by

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. The tar command is recursive, meaning that all files and folders below the parent folder will be included in the archive.

~]# tar -cf /home/john.doe/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
. . .

 


Create a compressed archive

Adding the -z option and the .gz extension will create a gzip compressed archive.

~]# tar -zcf /home/john.doe/example.tar.gz /etc 

 

Adding the -j option and .bz2 extension will create a bzip2 compressed archive.

~]# tar -jcf /home/john.doe/example.tar.bz2 /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
abrt
adjtime
akonadi
aliases
aliases.db
. . .

 

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

~]# tar -jtf /home/john.doe/example.tar.bz2
abrt
adjtime
akonadi
aliases
aliases.db
. . .

 


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 -x or --extract or --get and -C or --directory options can be used to extract the contents of an archive. In this example, the contents of example. tar are extracted to /home/john.doe/extraction.

~]# tar -xf /home/john.doe/example.tar -C /home/john.doe/extraction

 

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

~]# tar -zxf /home/john.doe/example.tar.gz -C /home/john.doe/extraction

 

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

~]# tar -jxf /home/john.doe/example.tar.bz2 -C /home/john.doe/extraction

 


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

 



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 in the box below so that we can be sure you are a human.




Comments