Tar stands for Tape archive.
Linux tar command most widely used for backup and archive data.
It is very important to archive multiple files and directory into a single file.
It is an essential Linux command for all Linux system administrators, here you will get most of the uses of tar command by using the tar examples, which are used by system administrators and engineers in every day.
Suggested Reading: Linux sed command Structure
Use tar command to create ‘tar’ archive file
You can create a tar archive file by using the option “-cvf,” as shown below:
# tar -cvf linuxconcept-14-09-19.tar /home/linuxconcept/
c – is used to create a new “.tar” archive file.
v – it is use to get the output of command verbosely
f – it applies to give the file name for the archive file.
Use tar command to create tar.gz archive file
You can create a gzip archive file with extension tar.gz using the “tar” command with option “z.”
For example, here we create a tar.gz archive file for Linuxconcept home directory, as shown below:
# tar cvzf Linuxconcept-14-09-19.tar.gz /home/linuxconcept OR # tar cvzf Linuxconcept-14-09-19.tgz /home/linuxconcept
The files with extension “tar.gz” and “.tgz” are similar.
Use tar command to create tar.bz2 archive file
You can create a “tar.bz2” archive file, also using tar command. The bz2 file compression and decompression is taking more time as compared to gzip files.
To create a highly compressed file “tar.bz2” by using tar command, you should use “j” option in the command line, as shown below:
# tar cvfj Linuxconcept-org.tar.bz2 /home/linuxconcept OR # tar cvfj Linuxconcept-org.tar.tbz /home/linuxconcept OR # tar cvfj Linuxconcept-org.tar.tb2 /home/linuxconcept
The above-created file “tar.bz2”, “tar.tbz” and “tar.tb2” are similar.
Extract tar archive file
To extract “tar” file need to use option “x” with tar command, as shown below:
# tar -xvf linuxconcept-14-09-19.tar
If you want to extract files in any specific directory, you can use the option “-C” to specify the directory when a file should extract, as shown below:
# tar -xvf linuxconcept-14-09-19.tar -C /home/linuxconcept/videos/
Extract tar.gz archive file
You can extract the “tar.gz” file with tar command just like tar file, and you can use the “-C” option if you want to extract in a specific directory, as shown below:
# tar -xvf linuxconcept-14-09-19.tar.gz # tar -xvf linuxconcept-14-09-19.tar.gz -C /home/linuxconcept/videos/
Extract tar.bz2 archive file
You can extract “tar.bz2” file by using the tar command, as shown below:
# tar -xvf linuxconcept-14-09-19.tar.bz2
List out the content of tar archive file
You can get a list of the content of tar archive file, to get a list of content just run the tar command with the “t” option as shown below:
# tar -tvf linuxconcept.tar
List out the content of the tar.gz archive file
You can list out the content of the “tar.gz” archive file by using the following command:
# tar -tvf linuxconcept.tar.gz
List out the content of the tar.bz2 archive file
You can list out the content of “tar.bz2” highly archived file by using the following command:
# tar -tvf linuxconcept.tar.bz2
Extract Single file from tar.gz file
You can extract a single file from the tar archive file by using the following command:
# tar -xvf linuxconcept.tar satish.sh OR # tar --extract --file=linuxconcept.tar satish.sh
We are extracting a single file “satish.sh” from linuxconcept.tar archive file in above example.
Extract single file from tar.bz2 file
You can extract a single file from “tar.bz2” archive file by using the following command:
# tar -jxvf linuxconcept.tar.bz2 satish.sh OR # tar --extract --file=linuxconcept.tar.bz2 satish.sh
Extract multiple files from tar, tar.gz and tar.bz2 file
To extract multiple files from tar archive files, you can use the following commands:
# tar -xvf linuxconcept-14-09-19.tar "file 1" "file 2" # tar –zxvf linuxconcept-14-09-19.tar.gz "file 1" "file 2" # tar -jxvf linuxconcept.tar.bz2 "file 1" "file 2"
You should replace “file 1” and “file 2” with your actual file name.
Adding files or directories to tar archive file
You can also add a file or directory in prebuild tar archive file using option “r,” as shown below:
# tar -rvf linuxconcept-14-09-19.tar xyz.txt # tar -rvf linuxconcept-14-09-19.tar php
In the above example, we added the “xyz.txt” file and “php” directory into “linuxconcept-14-09-10.tar” archive file.
Adding files or directories to tar.gz and tar.bz2 files
You can also add file or directory in prebuild tar.gz and tar.bz2 archive files by using the following commands:
# tar -rvf linuxconcept-14-09-19.tar.gz xyz.txt # tar -rvf linuxconcept.tar.bz2 xyz.txt
Check the size of the tar, tar.gz and tar.bz2 archive file
You can use below example command to check the size of any archive files:
# tar -czf - linuxconcept-14-09-19.tar | wc -c 12820480 # tar -czf - linuxconcept-14-09-19.tar.gz | wc -c 112640 # tar -czf - linuxconcept.tar.bz2 | wc -c 20480