Skip to content

tar Command

Archives and compressed files are used for many different purposes in the Linux world. In this article, we will review the tar command examples. Using tar; we will create "tarballs" with "gzip", "bzip2" and "xz".

tar Command Examples

As stated in the manual page; "GNU tar" saves many files together in a single tape or disk archive and can restore individual files from the archive. tar stands for "Tape Archive". You can get tar.gz extension files by compressing a tar file. Such compressed archive files are known as "tarball".

Creating an Archive with the Tar Command

Let's create some files first. Let's create a text file with I/O redirection and a data file with the dd command:

creating example files
[root@gnuadmin ~]# cat /var/log/lastlog > last.log
[root@gnuadmin ~]# journalctl > system_logs
[root@gnuadmin ~]# dd if=/dev/urandom of=data_file bs=1M count=100
100+0 records in
100+0 records out
104857600 bytes (105 MB) copied, 3,47781 s, 30,2 MB/s
[root@gnuadmin ~]# ls -lh
total 101M
-rw-r--r--. 1 root root 100M Jan 13 22:55 data_file
-rw-r--r--. 1 root root 286K Jan 13 22:52 last.log
-rw-r--r--. 1 root root  70K Jan 13 22:51 system_logs

Now let's add these files to a "tar archive":

creating a tar archive
[root@gnuadmin ~]# tar -cf my_archive.tar last.log data_file system_logs 
[root@gnuadmin ~]# tar -cvf my_verbose.tar last.log data_file system_logs 
last.log
data_file
system_logs

The "-c" option of the tar command stands for "create". "-f" allows you to specify the name of the archive you will create. The arguments in the continuation of the command specify the files to be added to the archive. If you want to see the processed files one by one during the creation of the archive, you can "verbose" with "-v".

Listing Files in the Tar Archive

With the tar -tvf command you can list the files in the tar archive:

listing the contents of the tar archive
[root@gnuadmin ~]# tar -tvf my_archive.tar 
-rw-r--r-- root/root    292000 2022-01-13 22:52 last.log
-rw-r--r-- root/root 104857600 2022-01-13 22:55 data_file
-rw-r--r-- root/root     70933 2022-01-13 22:51 system_logs

Extracting Files from a tar Archive

With the tar -xf command you can extract the files in the tar archive:

unpacking the tar archive
[root@gnuadmin ~]# ls -l
total 205520
-rw-r--r--. 1 root root 105226240 Jan 13 22:58 my_archive.tar
-rw-r--r--. 1 root root 105226240 Jan 13 22:58 my_verbose.tar
[root@gnuadmin ~]# tar -xf my_archive.tar 
[root@gnuadmin ~]# ls -l
total 308280
-rw-r--r--. 1 root root 104857600 Jan 13 22:55 data_file
-rw-r--r--. 1 root root    292000 Jan 13 22:52 last.log
-rw-r--r--. 1 root root 105226240 Jan 13 22:58 my_archive.tar
-rw-r--r--. 1 root root 105226240 Jan 13 22:58 my_verbose.tar
-rw-r--r--. 1 root root     70933 Jan 13 22:51 system_logs

Creating a tarball

You can create "tarball" by compressing your tar archives.

With the gzip Algorithm

If you want to use the "gzip" algorithm for compression, you can use the "-z" option:

gzip archive
[root@gnuadmin ~]# tar -czf gzip.tar.gz data_file last.log system_logs 

With the bzip2 Algorithm

If you want to use the "bzip2" algorithm for compression, you can use the "-j" option:

bzip2 archive
[root@gnuadmin ~]# tar -cjf bzip2.tar.bz2 data_file last.log system_logs 

Note: If you get "tar (child): bzip2: Cannot exec: No such file or directory" error, you need to install "bzip2" package on your system.

With the xz Algorithm

If you want to use the "xz" algorithm for compression, you can use the "-J" option:

xz archive
[root@gnuadmin ~]# tar -cJf xz.tar.xz data_file last.log system_logs