The Linux tar command stands for tape archive, used by large number of Linux/Unix operating system administrators to deal with tape drives backup.
The tar command has many variants and commonly used tar command variants are :
tar tar gzip tar bzip
The tar commands are mostly used to create compressed archive files and that can be moved easily from one disk to another or from one server to another easily.Tar commands compresses directories and its sub-directories into one compressed folder which can be transferred or downloaded easily on a local machine or transfer to other servers via simple wget command.
In this article, we will discuss the useful tar commands that will help you to master the tar commands and to work easily on a server.
Creating TAR Archive File
The tar command will create a tar archive file directoryname.tar for a directory “directoryname” at the given location on a server. Lets discuss tar command in an example given below to understand it properly.
# tar -cvf directoryname.tar /home/directoryname/ /home/directoryname/ /home/directoryname/file1.txt /home/directoryname/file2.jpeg /home/directoryname/folder.tar Where Directoryname is the name of folder or directory you want to compress.
The options given in the tar command have their own meanings as given below .
- c – Creates a new .tar archive file.
- v – Sow the .tar file progress .
- f – File name type of the archive file.
(Important Note* ) If you want to compress current directory in which you are working use the command
# tar -cvf directoryname.tar ./
Creating tar.gz Archive File
Tar Command used above does not compress the folder and size of the folder will be large. To compress the tar directory with gzip we use the command tar.gz. Lets understand this command with an example , tar.gz will create a compressed directoryname.tar.gz file for the directory /home/directoryname.
# tar cvzf directoryname.tar.gz /home/directoryname /home/directoryname/ /home/directoryname/pic1.jpg /home/directoryname/sara_funny.jpg /home/directoryname/funnypics.jpg /home/directoryname/photo.png
Creating tar.bz2 Archive File
The bz2 command takes more time to compress and decompress files as compared to gzip .The bz2 command compresses and create archive file less than the size of the gzip. To create highly compressed tar file we use option as j.
# tar cvfj directorynamefiles.tar.bz2 /home/directoryname
Decompressing TAR ARCHIVE (directoryname.tar) file using tar command
If you are going to extract tar file in the current directory, use the tar command using option x and if you want to extract tar archive in different directory use the option -C . Lets understand this command using following example.
# Extract files in Current Directory ##
# tar -xvf directoryname.tar
# Extract files in specified Directory #
# tar -xvf public_html-14-09-12.tar -C /home/public_html/directoryname/
Decompressing TAR.gz ARCHIVE (directoryname.tar) file using tar command
To Uncompress a compressed tar.gz archive file, just use the following command. If you need to decompress the tar file in a different directory just use option -C and the path of the directory, Check Out The Example given below.
# tar cvzf DirectoryName.tar.gz /home/DirectoryName
/home/public_html/directoryname/ /home/public_html/directoryname/files.php /home/public_html/directoryname/index.html /home/public_html/directoryname/readme.txt
Decompressing TAR.bz2 ARCHIVE (directoryname.tar) file using tar command
For decompressing the compressed tar.bz2 file use the command given below and if you need to decompress the file in a different directory use the -C option as given above.
# tar -xvf videos.tar.bz2
/home/public_html/videos/video1.flv /home/public_html/videos/mpeg/video.mpeg /home/public_html/videos/video.3gp
Untar Single file from tar File compressed file
To extract a single file called video.mpeg from video.tar use the following command as given below example.
# tar -xvf video.tar video.mpeg video.mpeg
Extracting Single file from tar.gz File
Use the following command to extract a single file codefile.xml from websitecode.tar.gz archive file
# tar -zxvf websitecode.tar.gz code.xml code.xml
Extracting Single file from tar.bz2 File
Use the command to extract single file file.html from websitecode.tar.bz2
# tar -jxvfwebsitecode.tar.bz2 home/website/file.html /home/website/file.html
Verifying tar, tar.gz and tar.bz2 Archive File
If you want to verify compressed tar , tar.bz , tar.bz2 file uses the following command with option W .
# tar tvfW WEBSITE.tar
How to Check the Size of the tar, tar.gz and tar.bz2 Archive File
To check the size of compressed tar, tar.bz and tar.bz2 files use the command given in the below example.
# tar -czf – folder1.tar | wc -c 125373
# tar -czf – folder2.tar.gz | wc -c 3463823
# tar -czf – folder3.tar.bz2 | wc -c 363980
The options used in the tar commands has the following meanings :
- c – create an archive file.
- x – extract an archive file.
- v – show the progress of archive file.
- f – filename of an archive file.
- t – viewing the content of archive file.
- j – filter archive through bzip2.
- z – filter archive through gzip.
- r – append or update files or directories to existing archive file.
- W – Verify an archive file.
Hope this guide of useful tar commands will make you tar command master and will help you to transfer your files easily between your machines.
If you want to contact the author of this guide you can start the conversation at firstname.lastname@example.org