Tar command in Linux is used to rip a collection of files/folders into a single file either called a tarball, tar or gzip. We daily work with files and folders and often do various kinds of operations on those. We often do backups of these files and folders it is really hard to take the backup of each and every working folder and kept it in a separate space, the tar command in Linux is used in these cases to archive all the files/folders into a single file.
Tar command in Linux is really helpful for backups and also the transfer of files and folders with the system. There are many options in the tar command which I will explain to you in this blog which includes how to create a tar file, how to extract a tar file, how to create a gzip archive how to list all the contents of a tar file, etc.
The most common flags used in tar commands are
c= This flag is used to create a new archive file.
f= This is the file name type of the archive file.
v= This is used to print the output of the tar command verbosely.
r= Used to append or update the existing tar file.
How to create a tar file in Linux?
tar -cvf backup.tar test
In this command, I am trying to create a tar file named backup.tar for my test folder in the PWD.
How to extract the contents of the tar file?
tar -xvf backup.tar
This will extract the contents inside the contents of tar file. You can also you -C flag to extract the contents of tar file to a different directory ie
Tar -xvf backup.tar -C /home/user/Downloads.This will extract the contents to the Downloads folder.
How to create a tar.gz compression?
tar -cvzf folder.tar.gz test
The -z flag is used to create a gzip compress. This command will create a tar.gz file for the folder test we have earlier created.
The same command tar -xvf filename.tar.gz is used to extract the tar.gz file also.
How to list the contents of a tar.gz file?
tar -tvf folder.tar.gz
Here the -t flag is used to list the contents inside the tar file also you can use the same command to list the contents of tar file
Use of wildcards in the tar command.
tar -xvf backup.tar.gz –wildcards ‘*.png’
As you can see we have .png files under the folder test and we have already taken a backup of that folder name backup.tar.gz.Suppose we need to only extract the .png files we need to run the above command. The same applies to any wildcards ie you can extract only .png,.txt,.PHP files only from a tar file using the –wildcards option.
How to add a file to an existing tar file.
tar -rvf backup.tar a.txt
Suppose we need to add files to existing tar for that use the -r flag used to append or update files. This command will add a.txt to the existing tar file backup.tar.
We cannot append a file to an existing tar.gz file using the -r flag.
You can create a backup of files and folders using the tar command and use command-line tools like SCP and rsync to transfer the tar file from one server to another.
How to use the Rsync command in Linux.
In this blog, I have discussed various use cases and options of the tar command in Linux.