15 Tar Command Examples in Linux
The Linux “tar” stands for tape archive, which is used by a large number of Linux system administrators to use this command for tape drives backup. The tar command used to rip a collection of files and directories into a highly compressed archive file commonly called tarball or tar, gzip and bzip in Linux.
Tar Usage and Options
c – create an archive file.
x – extract an archive file.
v – show the progress of the archive file.
f – filename of the archive file.
t – viewing the content of the archive file.
j – filter archive through bzip2.
z – filter archive through gzip.
r – append or update files or directories to the existing archive files.
W – Verify an archive file.
wildcards – Specify patterns in UNIX tar command.
1. Create tar Archive File
The below example command will create a tar archive file nirmal-19-06-21.tar for a directory /home/nirmal in the current working directory. See the example command in action.
# tar -cvf nirmal-19-06-21.tar /home/nirmal/
c – Creates a new .tar archive file.
v – Verbosely show the .tar file progress.
f – Filename type of the archive file.
2. Create tar.gz Archive File
To create a compressed gzip archive file we use the option as z. For example, the below command will create a compressed MyImages-19-06-21.tar.gz file for the directory /home/MyImages. (Note: tar.gz and tgz both are similar).
# tar cvzf nirmal-19-06-21.tar.gz /home/nirmal
OR
# tar cvzf nirmal-19-06-21.tgz /home/nirmal
3. To Create tar.bz2 Archive File
# tar cvfj Php.tar.bz2 /home/php
OR
# tar cvfj Php.tar.tbz /home/php
OR
# tar cvfj Php.tar.tb2 /home/php
4. To Untar tar Archive File
To untar or extract a tar file, just issue the following command using option x (extract). For example, the below command will untar the file html-19-06-21.tar in the present working directory. If you want to untar in a different directory then use option as -C (specified directory).
## Untar files in Current Directory ##
# tar -xvf html-19-06-21.tar
## Untar files in specified Directory ##
# tar -xvf html-19-06-21.tar -C /home/html/data/
5. To Uncompress tar.gz Archive File
To Uncompress tar.gz archive file, just run the following command. If we would like to untar in different directories, just use option -C and the directory path, as shown in the above example.
# tar -xvf image-19-06-21.tar.gz
6. To Uncompress tar.bz2 Archive File
To Uncompress the highly compressed tar.bz2 file, just use the following command. The below example command will untar all the .flv files from the archive file.
# tar -xvf nirmal-19-06-21.tar.bz2
7. To List Content of tar Archive File
To list the contents of the tar archive file, just run the following command with option t (list content). The below command will list the content of the upload.tar file.
# tar -tvf upload.tar
-rw-r--r-- nirmal/apache 1433 2011-08-15 18:51:10 package.xml
8. List Content tar.gz Archive File
Use the following command to list the content of the tar.gz file.
# tar -tvf staging.nirmal.com.tar.gz
-rw-r--r-- root/root 0 2012-08-30 04:03:57 staging.nirmal.com-access_log
-rw-r--r-- root/root 587 2012-08-29 18:35:12
9. List Content tar.bz2 Archive File
To list the content of the tar.bz2 file, issue the following command.
# tar -tvf Php.tar.bz2
drwxr-xr-x root/root 0 2012-09-15 03:06:08 /home/php/
-rw-r--r-- root/root 1751 2012-09-15 03:06:08 /home/php/index.php
-rw-r--r-- root/root 1273 2012-09-15 03:06:08 /home/php/index.html
10. Untar Single file from tar File
To extract a single file called cleanfiles.sh from cleanfiles.sh.tar use the following command.
# tar -xvf myfile.sh.tar myfile.sh
OR
# tar --extract --file=myfile.sh.tar myfile.sh
myfile.sh
11. Untar Single file from tar.gz File
To extract a single file nirmalbackup.xml from the nirmalbackup.tar.gz archive file, use the command as follows.
# tar -zxvf nirmalbackup.tar.gz nirmalbackup.xml
OR
# tar --extract --file=nirmalbackup.tar.gz nirmalbackup.xml
nirmalbackup.xml
12. Untar Single file from tar.bz2 File
To extract a single file called index.php from the file Phpfiles-org.tar.bz2 use the following option.
# tar -jxvf myfile.tar.bz2 home/php/index.php
OR
# tar --extract --file=myfile.tar.bz2 /home/php/index.php
/home/php/index.php
13. Untar Multiple files from tar, tar.gz, and tar.bz2 File
To extract or untar multiple files from the tar, tar.gz, and tar.bz2 archive file. For example, the below command will extract “file 1” “file 2” from the archive files.
# tar -xvf nirmal-19-06-21.tar "file1" "file2"
# tar -zxvf myfile-19-06-21.tar.gz "file1" "file2"
# tar -jxvf myfile.tar.bz2 "file1" "file2"
14. Extract Group of Files using Wildcard
To extract a group of files we use wildcard-based extracting. For example, to extract a group of all files whose pattern begins with .php from a tar, tar.gz, and tar.bz2 archive file.
# tar -xvf Php.tar --wildcards '*.php'
# tar -zxvf Php.tar.gz --wildcards '*.php'
# tar -jxvf Php.tar.bz2 --wildcards '*.php'
/home/php/rss.php
/home/php/index.php
/home/php/video.php
15. To Add Files or Directories to tar Archive File
To add files or directories to the existing tar archive files we use the option r (append). For example, we add file xyz.txt and directory php to the existing nirmal-19-06-21.tar archive file.
# tar -rvf nirmal-19-06-21.tar xyz.txt
# tar -rvf nirmal-19-06-21.tar php
drwxr-xr-x root/root 0 2012-09-15 02:24:21 home/nirmal/
-rw-r--r-- root/root 21063680 2012-09-15 02:24:21 home/nirmal/nirmal-19-06-21.tar
Wow so helpful.
ReplyDeleteVery well organised ..like it.thanks
ReplyDelete