Tar command in Linux is used to archive files and folder into a single archived compressed file. Linux admins use tar command to perform day to day backup and restoration tasks. In other words, we can say that tar command is a command line utility which is used to archive important files and folders of a Linux system and later we can extract files and folders from that archive for restoration purpose.
Tar (tape archive) command can also be used to free disk space on linux filesystems because it supports compression while archiving large log files and folders. In this article, we will discuss top 15 tar command examples which helps you to learn tar command like a professional.
Syntax of Tar Command
$ tar <options> <tar-file-name> <file/folders-to-be-archived>
Now, let’s deep dive into the examples of tar command.
1) Create archive file from files and folders
Let’s assume we want to archive all files and folders of /home directory into a tar ball named sysadm.tar under /mnt directory, run below command.
$ sudo tar -cvf /mnt/sysadm.tar /home/ $ ls -l /mnt/sysadm.tar
Note: Extension of archive file should always be .tar provided no compression technique is used.
2) List all the files from archive tar files verbosely
To list all the available files from tar ball using option ‘-tvf’, example is shown below,
$ tar -tvf /mnt/sysadm.tar
Output,
3) Extract Files from an archive
To extract files from tar ball use ‘-xf’ option, if in case you want verbose output too then use ‘-xvf’. In the below example we are extracting files from sysadm.tar file to /tmp
$ cd /tmp/ $ tar -xvf /mnt/sysadm.tar $ ls -l home/
Output of above commands,
4) Create archive file along with gunzip compression
Tar command supports two compression techniques like gunzip and bzip2. Using tar command, we can compress files and folders and then archive them into a file.
To create archive along with gunzip compression use ‘-z’ option, example is shown below:
$ sudo tar -zcpvf rockylinux-logs.tgz /var/log/
Here we have used following options in tar command:
- z for gunzip compression
- c for creating tar and gunzip with extension .tgz
- p for preserving permissions
- v for verbose output
- f for the file name
Output
Note: Extension of archive file when using gunzip compression is always ‘.tgz’ or ‘.tar.gz’
5) Create archive file along with bzip2 compression
To create archive file along with bzip2 compression, use ‘-jpvcf’ option, example is shown below.
$ sudo tar -jcpvf rockylinux-logs.tbz2 /var/log/
Output:
6) Extract Compressed archive file (.tgz)
To extract compressed archived file use ‘-x’ option. Example is shown below
$ tar -xvf rockylinux-logs.tgz
Above command will extract files and folders from archive in the current working directory.
7 ) Extract Compressed tar archive file (.tbz2)
To extract compressed archived file (.tbz2) use ‘-x’ option, example is listed below
$ tar -xvf rockylinux-logs.tbz2
8) Extract tar archive file to specific folder
To extract archive file to a specific folder, use ‘-C’ option followed by directory path. Example is shown below,
$ sudo tar -xvf rockylinux-logs.tbz2 -C /var/log/
Above command will extract the files and directories from compressed archive file to /var/log directory
9) List the contents of .tgz and .tbz2
To list the contents of .tgz and .tbz2 file use ‘-t’ options as shown below
$ tar -tf rockylinux-logs.tgz $ tar -tf rockylinux-logs.tbz2
10) Extract a single file from compressed archive (.tgz)
To extract a single file from compressed archive (.tgz), use -xvf followed by compressed archive file and file which we want to extract, example is shown below.
$ tar -xvf rockylinux-logs.tgz var/log/audit/audit.log var/log/audit/audit.log $
In above command we have extracted audit.log file from the archive file ‘rockylinux-logs.tgz’. This file will be extracted in current working directory.
11) Extract multiple files from compressed archive (.tbz2)
To extract multiple files from an archive file, use ‘-xvf’ followed by archive and files names to be extracted. Let’s assume we want to extract lvm.log and tuned.log files.
$ tar -xvf rockylinux-logs.tgz var/log/tuned/tuned.log var/log/anaconda/lvm.log var/log/tuned/tuned.log var/log/anaconda/lvm.log $
12) Append File/Directory to a existing tar archive file
To append a new file/directory to the existing tar archive file use ‘-r’ option, example is shown below,
Let’s assume we want append fstab file to existing tar archive (sysadm.tar) file,
$ sudo tar -rvf sysadm.tar /etc/fstab $ sudo tar -tvf sysadm.tar | grep fstab
Output of above commands,
Note : We cannot append a file / directory to the existing compressed tar archive file as it is not supported.
13) Exclude files/directory while archiving (-X)
There can be some scenarios where we want to exclude some files or directory while archiving. Let’s assume we want to exclude network related files from /etc while creating the archive, run
$ echo '/etc/sysconfig/network-scripts/ifcfg-enp0s3' > exclude.txt $ sudo tar zcpvf etc-archive.tgz -X exclude.txt /etc/ $ tar tvf etc-archive.tgz | grep -i 'ifcfg-enp0s3' $
In case you noticed carefully, we have created a exclude file where we mentioned the file name to be excluded. So while using exclude feature in tar command it is mandate to create exclude file.
14) Include and Exclude files while creating archive
If you wish tar command to pick files and directories to be include and excluded while creating archive then use -T and -X option.
Here -T specify the files to be used to include files in archive and -X specify the files to exclude. To use these options first we must create include and exclude files.
$ cat include.txt /var/log/ /etc /home/sysadm $ cat exclude.txt /var/log/hawkey.log /etc/sysconfig/network-scripts/ifcfg-enp0s3 $
Run below tar command to leverage these files for inclusion and exclusion
$ sudo tar zcpvf custom-backup.tgz -X exclude.txt -T include.txt
15) View the size of tar archive file
To view the size of tar archive file, run following command,
$ tar -czf - custom-backup.tgz | wc -c 12639540 $
Here the size is measured in KB.
That’s all from this article, I hope you have found these tar command examples informative. Please do share your feedback and comments.
Read Also : Learn IP Command to Manage Networking on Linux