The Linux tar command stands for tap archive, is not a compressed file and commonly called as a collection (which contains the contents of many files) of a single file.
Tar is the most commonly used command in Linux for creating a archive file. It saves multiple files, folders & other file formats (zip, gzip, bzip2 & tar) into a single archive file. Many of us very frequently uses three operations create, list & extract & two options file & verbose with tar to dealing the backups and restore.
Additionally we can use gzip & bzip2 commands for further compressing the tar archive. Mainly tar command used to performing backup (full and incremental) from the server by server administrator, other famous backup applications are using tar command for backup.
All the files and folders can be restore from the archive file whenever you want, every server administrator need to know the tar command and it’s usage for better backup solution.
By default tar command included into most of the distribution, so we can easily install from distribution official repository.
Example 1 – To create a new archive of folder named Yeahhub which contains 3 files named document.docx, file.txt and song.mp3, the command is:
Command: tar cf file1.tar Yeahhub/
Where c refers to the creation of new archive and f is the specified archive file.
Example 2 – Similarly you can also create an archive of a set of files in current working directory i.e. Yeahhub, the command is:
Command: tar cf file2.tar document.docx file.txt song.mp3
Example 3 – To extract the archive in current working directory, the command is:
Command: tar xf file1.tar
Where x refers for extraction.
Example 4 – And if you want to extract the content in a specific directory then you can use the following command:
Command: tar xf file1.tar -C Downloads/
Example 5 – If you want to extract the particular type (Extension) of file then you can use the below command with –wildcards option.
Command: tar -xf file.tar –wildcards “*.docx”
The above command will only extract .docx extension files from the tar file.
Example 6 – By default, tar always create .tar extension files but it may also create .tar.gz or .tgz file which is used for compression.
To create a .tar.gz archive, the command is:
Command: tar czf compressedfile.tar.gz Yeahhub/
Where z flag is used for gzip compression.
Example 7 – Similarly you can also create .tgz file by typing:
Command: tar czf compressed.tgz Yeahhub/
Example 8 – If you want to see the progress when creating the archive the flag is v:
Command: tar czvf newfile.tar.gz Yeahhub/
Example 9 – To create a gzip archive from a list of files, the command is:
Command: tar czf compressedfile.tar.gz document.docx file.txt song.mp3
Example 10 – To extract the gzip archive in current directory, the command is:
Command: tar xzf compressedfile.tar.gz
Example 11 – And if you want to extract to a specified directory then you can use -C flag as shown below:
Command: tar xzf compressedfile.tar.gz -C /root/Downloads/
Example 12 – To create a bzip archive of extension (.tar.bz2), the command is:
Command: tar cjf newfile.tar.bz2 Yeahhub/
Example 13 – Similarly for bzip archive file creation of extension (.tbz), you can use the following command:
Command: tar cjf file.tbz Yeahhub/
Example 14 – And to create a archive of extension .tar.bz2 from a list of files, the command is:
Command: tar cjf newfile.tar.bz2 document.docx file.txt song.mp3
Example 15 – To extract the bzip archive, you can use the following command with x flag:
Command: tar xjf newfile.tar.bz2
Example 16 – With -C option, you can extract the contents of bzip archive file in any location.
Command: tar xjf newfile.tar.bz2 -C /root/Downloads/
Example 17 – You can also create an archive of multiple directories and/or files at one time.
Command: tar czvf newfile.tar.gz Downloads/ Music/ Yeahhub/file.txt
Example 18 – To list the contents of the archive file without extraction, the command is:
Command: tar tf file.tgz
Example 19 – And if you want to see the detailed output, you can use the v flag as shown below:
Command: tar tvf file.tgz
Example 20 – And if you want to add any specific file to an existing archive, then you can use the following command:
Command: tar rf newfile.tar Hub/newfile.txt