Linux Commands · January 13, 2022

tar command examples

[Linux] tar Command Examples

Archives and compressed files are used in many variety. In this post, you’ll see examples of tar. By using these commands, we’ll try to create tarballs with gzip, bzip2 and xz.

tar Command Examples

As manual page states, GNU “tar” saves many files together into a single tape or disk archive, and can restore individual files from the archive. Tar stands for “Tape Archive“. By compressing a tar file, you can get files like “tar.gz” extension. These type of compressed archive files known as “tarball“.

Creating An Archive With tar Command

First, let’s create some files. I’ll create one data and one text file with I/O redirection. And a data file with “dd” command.

[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 put these files under 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

tar, followed by options and arguments… “-c” stands for “create” and “-f” shows the file which will be created. If you want to see which files included in you recent archive, you can use “-v” option for verbosity.

List the files inside tar archive:

You can use “tar -tvf” to list an 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

Extract files from tar archive:

You can use “tar -xf” to extract files from an 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 tarballs

In this section, we’ll create tarballs with 3 different algorithms. Performances of these algorithms depends on the data type and the data itself. Comparison of compression algorithms is not the point of this article.

Creating tarballs with gzip algoritm

By adding “-z” option, you can state that you want gzip compression.

[root@gnuadmin ~]# tar -czf gzip.tar.gz data_file last.log system_logs 
[root@gnuadmin ~]# ls -lh
total 402M
-rw-r--r--. 1 root root 100M Jan 13 22:55 data_file
-rw-r--r--. 1 root root 101M Jan 13 23:07 gzip.tar.gz
-rw-r--r--. 1 root root 286K Jan 13 22:52 last.log
-rw-r--r--. 1 root root 101M Jan 13 22:58 my_archive.tar
-rw-r--r--. 1 root root 101M Jan 13 22:58 my_verbose.tar
-rw-r--r--. 1 root root  70K Jan 13 22:51 system_logs

Creating tarballs with bzip2 algorithm

By adding “-j” option, you can state that you want bzip2 compression.

If you ran into “tar (child): bzip2: Cannot exec: No such file or directory” error, that means you need to install bzip2 package.

[root@gnuadmin ~]# tar -cjf bzip2.tar.bz2 data_file last.log system_logs 
[root@gnuadmin ~]# file bzip2.tar.bz2 
bzip2.tar.bz2: bzip2 compressed data, block size = 900k
[root@gnuadmin ~]# ls -lh
total 402M
-rw-r--r--. 1 root root 101M Jan 13 23:21 bzip2.tar.bz2
-rw-r--r--. 1 root root 100M Jan 13 22:55 data_file
-rw-r--r--. 1 root root    0 Jan 13 23:09 gzip.tar.gz
-rw-r--r--. 1 root root 286K Jan 13 22:52 last.log
-rw-r--r--. 1 root root 101M Jan 13 22:58 my_archive.tar
-rw-r--r--. 1 root root 101M Jan 13 22:58 my_verbose.tar
-rw-r--r--. 1 root root  70K Jan 13 22:51 system_logs

Creating tarballs with xz algorithm

By adding “J” option, you can state that you want xz compression.

[root@gnuadmin ~]# tar -cJf xz.tar.xz data_file last.log system_logs 
[root@gnuadmin ~]# file xz.tar.xz 
xz.tar.xz: XZ compressed data
[root@gnuadmin ~]# ls -lh
total 502M
-rw-r--r--. 1 root root 101M Jan 13 23:21 bzip2.tar.bz2
-rw-r--r--. 1 root root 100M Jan 13 22:55 data_file
-rw-r--r--. 1 root root    0 Jan 13 23:09 gzip.tar.gz
-rw-r--r--. 1 root root 286K Jan 13 22:52 last.log
-rw-r--r--. 1 root root 101M Jan 13 22:58 my_archive.tar
-rw-r--r--. 1 root root 101M Jan 13 22:58 my_verbose.tar
-rw-r--r--. 1 root root  70K Jan 13 22:51 system_logs
-rw-r--r--. 1 root root 101M Jan 13 23:26 xz.tar.xz
tar command examples
tar command examples