BEST TAR COMMAND ( TAR ARCHIVE ) WITH EXAMPLES IN LINUX
Introduction
Tar command is a very useful command utility stands for Tape Archive is also referred as Tarball is used to take backup of the data in archived format. It creates output file with extension .tar for example data.tar also called as Tar archive. Tar command is very simple to use and very convienent way to backup the data, It comes with so many features like take backup of data in archived format, compress the data by using gzip or bzip compression tools and so on.
Syntax to create a Tar file is :
tar <Options> <Tar File Name> <Source File/Directory Path>
Some Common Options :
c – Create a New Tar Archive File
x – Extract a Tar file
z – Compress a Tar file with gzip Compression
j – Compress a Tar file with bzip2 Compression
C – Extract a Tar Archive in different directory
t – List the content of the Tar file
r – Add/Append file ot directory to Tar archive
W – Verify a Tar file
For more tar related information and options follow the below command.
[root@localhost ~]# tar --help # For more Tar command Help
Let’s have a look at some most important tar command with examples explained below.
Create a Tar Archive File using Tar Command
To create a tar archive file we can use tar command with option -c. Refer the below command.
[root@localhost ~]# tar -cvf file.txt.tar file.txt # Create a Tar Archive of a File
file.txt
Where :
- c – create a tar archive file
- v – for Verbose
- f – for tar archive filename
To create a Tar archive of a directory follow the below command.
[root@localhost ~]# tar -cvf data.tar data/ # Create a Tar archive of a Directory
data/
data/file3.txt
data/file2.txt
data/file4.txt
data/file1.txt
data/file5.txt
Extract a Tar Archive file using Tar Command
You can extract a tar archive file using tar command with option -x, Refer the command below.
[root@localhost ~]# tar -xvf data.tar # Extract a Tar Archive File
data/
data/file3.txt
data/file2.txt
data/file4.txt
data/file1.txt
data/file5.txt
Where :
- x – Extract a Tar Archive
Also Read :
- BEST YUM COMMAND WITH EXAMPLES A PACKAGE MANAGER IN RHEL/CENTOS/FEDORA
- BEST ZIP COMMAND WITH EXAMPLES IN LINUX
- BEST RPM COMMAND ( REDHAT PACKAGE MANAGER ) WITH EXAMPLES IN LINUX
Create a Tar file with GZIP Compression
We can create a tarball file with GZIP compression using tar command with option -z.
[root@localhost ~]# tar -czvf data.tar.gz data/ # Create a Tar archive file with Gzip Compression (tar.gz)
data/
data/file3.txt
data/file2.txt
data/file4.txt
data/file1.txt
data/file5.txt
Where :
z – Create a gzip compressed tar archive
Extract a GZIP compressed tar file
Follow the below command to extract a Gzip compressed Tar file.
[root@localhost ~]# tar -xzvf data.tar.gz # Extract a Gzip Compressed Tar Archive (tar.gz)
data/
data/file3.txt
data/file2.txt
data/file4.txt
data/file1.txt
data/file5.txt
Create a Tar file with BZIP2 Compresion
To create a Tar file with Bzip2 compression use tar command with -j option.
[root@localhost ~]# tar -cjvf data.tar.bz2 data/ # Create a Tar Archive file with Bzip2 Compression (Tar.bz2)
data/
data/file3.txt
data/file2.txt
data/file4.txt
data/file1.txt
data/file5.txt
Where :
j – Create a Bzip2 compressed tar archive
Extract a BZIP2 Compressed File
To extract a Bzip2 compressed tar file refer the below command.
[root@localhost ~]# tar -xjvf data.tar.bz2 # Extract a Bzip2 Compressed Tar Archive (tar.bz2)
data/
data/file3.txt
data/file2.txt
data/file4.txt
data/file1.txt
data/file5.txt
Extract a Tar Archive File in a Different Directory
You can extract a tar file in a different directory using tar command with option -C.
[root@localhost ~]# tar -xvf data.tar.bz2 -C /test/ # Extract a Tar archive in Different Directory
data/
data/file3.txt
data/file2.txt
data/file4.txt
data/file1.txt
data/file5.txt
Where :
C – Extract a Tar archive in different directory
List the Content of a Tar File
To list the content of the a tar file we can use tar command with option -t.
[root@localhost ~]# tar -tvf data.tar.bz2 # List the Content of a Tar Archive
drwxr-xr-x root/root 0 2017-04-11 02:56 data/
-rw-r--r-- root/root 0 2017-04-11 02:56 data/file3.txt
-rw-r--r-- root/root 0 2017-04-11 02:56 data/file2.txt
-rw-r--r-- root/root 0 2017-04-11 02:56 data/file4.txt
-rw-r--r-- root/root 0 2017-04-11 02:56 data/file1.txt
-rw-r--r-- root/root 0 2017-04-11 02:56 data/file5.txt
Extract a Particular File form a Tar Archive
To extract a Particular file from tar archive refer the below command.
[root@localhost ~]# tar -xvf data.tar data/file3.txt # Extract a Particular file from Tar Archive
data/file3.txt
Add/Append a Directory to a Tar File
To add a file to a existing Tar file we can use tar command with option -r.
[root@localhost ~]# tar -rvf data.tar Desktop/ # Add/Append a Directory in Tar Archive Desktop/ [root@localhost ~]# tar -tvf data.tar drwxr-xr-x root/root 0 2017-04-11 04:25 data/ -rw-r--r-- root/root 0 2017-04-11 04:25 data/test4.txt -rw-r--r-- root/root 0 2017-04-11 04:25 data/test1.txt -rw-r--r-- root/root 0 2017-04-11 04:25 data/test5.txt -rw-r--r-- root/root 0 2017-04-11 04:25 data/test3.txt -rw-r--r-- root/root 0 2017-04-11 04:25 data/test2.txt drwxr-xr-x root/root 0 2017-04-08 03:20 Desktop/
Add/Append a File to a Tar Archive
To add a directory to a existing Tar archive follow the below command.
[root@localhost ~]# tar -rvf data.tar elinuxbook.txt # Add/Append a File in Tar Archive elinuxbook.txt [root@localhost ~]# tar -tvf data.tar drwxr-xr-x root/root 0 2017-04-11 04:25 data/ -rw-r--r-- root/root 0 2017-04-11 04:25 data/test4.txt -rw-r--r-- root/root 0 2017-04-11 04:25 data/test1.txt -rw-r--r-- root/root 0 2017-04-11 04:25 data/test5.txt -rw-r--r-- root/root 0 2017-04-11 04:25 data/test3.txt -rw-r--r-- root/root 0 2017-04-11 04:25 data/test2.txt drwxr-xr-x root/root 0 2017-04-08 03:20 Desktop/ -rw-r--r-- root/root 0 2017-04-11 04:29 elinuxbook.txt
Also Read :
- MOST USEFUL SSH COMMAND AND SCP COMMAND WITH EXAMPLES
- HOW TO CREATE SYMLINK (SYMBOLIC LINK) AND HARDLINK IN LINUX
Delete a Particular File from a Tar Archive
To delete a particular file from a tar archive we can use tar command with option – -delete, Here I am deleting a file “test4.txt” from data.tar. Refer the command below.
[root@localhost ~]# tar -tvf data.tar # List the content of the Tar file drwxr-xr-x root/root 0 2017-04-11 04:25 data/ -rw-r--r-- root/root 0 2017-04-11 04:25 data/test4.txt # I am going to delete this file -rw-r--r-- root/root 0 2017-04-11 04:25 data/test1.txt -rw-r--r-- root/root 0 2017-04-11 04:25 data/test5.txt -rw-r--r-- root/root 0 2017-04-11 04:25 data/test3.txt -rw-r--r-- root/root 0 2017-04-11 04:25 data/test2.txt drwxr-xr-x root/root 0 2017-04-08 03:20 Desktop/ -rw-r--r-- root/root 0 2017-04-11 04:29 elinuxbook.txt [root@localhost ~]# tar --delete --file=data.tar data/test4.txt # Delete a Particular file from Tar Archive File # As you can see below now we dont have test4.txt file in data.tar [root@localhost ~]# tar -tvf data.tar drwxr-xr-x root/root 0 2017-04-11 04:25 data/ -rw-r--r-- root/root 0 2017-04-11 04:25 data/test1.txt -rw-r--r-- root/root 0 2017-04-11 04:25 data/test5.txt -rw-r--r-- root/root 0 2017-04-11 04:25 data/test3.txt -rw-r--r-- root/root 0 2017-04-11 04:25 data/test2.txt drwxr-xr-x root/root 0 2017-04-08 03:20 Desktop/ -rw-r--r-- root/root 0 2017-04-11 04:29 elinuxbook.txt
Delete a Particular Directory From a Tar File
To delete a particular directory from a tar archive we can use tar command with option – -delete, Here I am deleting a file “Desktop” from data.tar. Refer the command below.
[root@localhost ~]# tar -tvf data.tar drwxr-xr-x root/root 0 2017-04-11 04:25 data/ -rw-r--r-- root/root 0 2017-04-11 04:25 data/test1.txt -rw-r--r-- root/root 0 2017-04-11 04:25 data/test5.txt -rw-r--r-- root/root 0 2017-04-11 04:25 data/test3.txt -rw-r--r-- root/root 0 2017-04-11 04:25 data/test2.txt drwxr-xr-x root/root 0 2017-04-08 03:20 Desktop/ # Now I am going to delete this directory from Tar Archive -rw-r--r-- root/root 0 2017-04-11 04:29 elinuxbook.txt [root@localhost ~]# tar --delete --file=data.tar Desktop/ # Deleting a Directory from Tar file # As you can see below now we don't have Desktop directory in data.tar file. [root@localhost ~]# tar -tvf data.tar drwxr-xr-x root/root 0 2017-04-11 04:25 data/ -rw-r--r-- root/root 0 2017-04-11 04:25 data/test1.txt -rw-r--r-- root/root 0 2017-04-11 04:25 data/test5.txt -rw-r--r-- root/root 0 2017-04-11 04:25 data/test3.txt -rw-r--r-- root/root 0 2017-04-11 04:25 data/test2.txt -rw-r--r-- root/root 0 2017-04-11 04:29 elinuxbook.txt
Verify a Tar Archive File
To verify if Tar file is valid or not we can use tar command with option -W.
[root@localhost ~]# tar -tvWf data.tar # Verify a Tar Archve File
tar: This does not look like a tar archive
tar: Skipping to next header
Verify ---------- 0/0 0 1969-12-31 16:00 data/test3.txt
data/test3.txt: Mode differs
data/test3.txt: Mod time differs
Verify ---------- 0/0 0 1969-12-31 16:00 data/test2.txt
data/test2.txt: Mode differs
data/test2.txt: Mod time differs
tar: Exiting with failure status due to previous errors
The purpose to verify the tar file is some one can create a tar file by just renaming the file for example I have elinuxbook.txt file now I can rename this file to elinuxbook.tar by using mv command.
[root@localhost ~]# mv elinuxbook.txt elinuxbook.tar # Renaming the .txt file to .tar file
[root@localhost ~]# tar -tvWf elinuxbook.tar
tar: This does not look like a tar archive
tar: Skipping to next header
Aborted (core dumped)
We got above message because we have not created the file using tar command, hence tar signatures are not available in that file.
Extract from a Tar Archive File using Wildcard
You can use – -wildcard option to extract multiple files from tar archive. For example here I have a data.tar file which contains some files with extension “.txt“, So to extract all “.txt” file we can use below command.
[root@localhost ~]# tar -xvf data.tar --wildcards '*.txt' # Extract Particular files from Tar by using Wildcard
data/test4.txt
data/test1.txt
data/test5.txt
data/test3.txt
data/test2.txt
Search from a Tar Archive File
To search for some files or directory in tar file refer the below command.
[root@localhost ~]# tar -tvf data.tar | grep test3.txt # Search from Tar Archive
-rw-r--r-- root/root 0 2017-04-11 04:25 data/test3.txt
We tried to include as many as tar command with examples we can, If any commands got missed the Please update us on Comment box below.
If you found this Article useful then Like it, Share it, Subscribe our Site for more Linux Tutorials OR If you have any thing to say then feel free to Comment on Comment Box below.