April 2024
M T W T F S S
1234567
891011121314
15161718192021
22232425262728
2930  

Categories

April 2024
M T W T F S S
1234567
891011121314
15161718192021
22232425262728
2930  

GNU tar (Tape ARchive)

GNU tar (Tape ARchive) saves multiple files together into a single tape or disk archive, and can restore individual files from the archive. Here are some Linux tar Command with Useful Practical Examples

Some useful command line switches are given below, which are used in this article.

c => create an archive file.
v => show the detailed output ( or porgress ) of command.
x => extract an archive file.
f => filename of archive file.
z => Use archive through gzip.
t => viewing content of archive file.
j => Use archive through bzip2.
C => Use of define destination directory.
W => Verify a archive file.
Example 1: Create a .tar Archive File.

Below command will create an compressed ( tar ) file using all files and directories ( with subdirectories ) of /var/log directory.

# tar cvf rmohan-16-apr-2013.tar /var/log
Example 2: Create a .tar.gz Archive File.

Below command will create an gzip compressed ( tar.gz ) file using all files and directories ( with subdirectories ) of /var/log directory. I will create more compressed file than .tar file.

# tar czvf rmohan-16-apr-2013.tar.gz /var/log
Example 3: Create a .tar.bz2 Archive File.

This example is used for creating bz2 files. bz2 files are higher compressed than .tar and .tar.gz files. Its also take more time to compress than others.

# tar cjvf rmohan-16-apr-2013.tar.bz2 /var/log
Check difference between .tar , .tar.gz and .tar.bz2 file sizes.

# ls -l rmohan-16-apr-2013.tar*
-rw-r–r–. 1 root root 26818560 Apr 12 22:25 rmohan-16-apr-2013.tar
-rw-r–r–. 1 root root 697802 Apr 13 00:25 rmohan-16-apr-2013.tar.bz2
-rw-r–r–. 1 root root 1091399 Apr 13 00:26 rmohan-16-apr-2013.tar.gz
As per above output .tar.bz2 is higher compressed and .tar is lower compressed file.

Example 4: List content of .tar, .tar.gz, .tar.bz2 Archive File.

Some times we requried to list content of archive file without extracting them. Below example will help you to do it.

# tar -tvf rmohan-16-apr-2013.tar
# tar -tvf rmohan-16-apr-2013.tar.gz
# tar -tvf rmohan-16-apr-2013.tar.bz2
Example 5: Uncompress .tar Archive File.

To uncompress a .tar file use below command. It will extract all files and directories ( with subdirectories ) in current path. To uncompress in different location check Example 8.

# tar -xvf rmohan-16-apr-2013.tar
Example 6: Uncompress .tar.gz Archive File.

To uncompress a .tar.gz ( tar + gzip ) file use below command. It will extract all files and directories in current path.

# tar -xvf rmohan-16-apr-2013.tar
Example 7: Uncompress .tar.bz2 Archive File.

Use below example to uncompress a .tar.bz2 ( tar + bz2 ) file. It will extract all files and directories in current path.

# tar -xvf rmohan-16-apr-2013.tar
Example 8: Extract Archive in different location.

Use C command line switch to specify a location for extracting an archive file.

# tar xf rmohan-16-apr-2013.tar -C /Output/Dir/
Example 9: Verify .tar Archive Files.

To verify .tar file use ‘W’ switch with ‘t’. For you information .tar.gz and .tar.bz2 can not be verified using this. Use below command to verify your tar file.

# tar tvWf /root/rmohan-16-apr-2013.tar
Sample Output:

tar: This does not look like a tar archive
tar: Skipping to next header
tar: VERIFY FAILURE: 13 invalid headers detected
Verify -rw——- root/root 0 2013-03-31 03:50 var/log/spooler-20130407
Verify -rw——- root/utmp 384 2013-04-12 21:31 var/log/btmp
Verify -rw-r–r– root/root 211614 2013-03-18 19:20 var/log/dracut.log
Verify -rw——- root/root 5793 2013-04-12 22:20 var/log/secure
Verify -rw-r–r– root/root 4952 2013-04-01 14:40 var/log/krb5kdc.log-20130401
Verify drwxr-xr-x root/root 0 2011-12-08 06:00 var/log/spice-vdagentd/
Verify drwx—— root/root 0 2013-04-12 14:57 var/log/samba/
Verify -rw-r–r– root/root 0 2013-04-09 01:58 var/log/samba/log.192.168.1.199
Verify -rw-r–r– root/root 1972 2013-04-12 14:37 var/log/samba/log.nmbd
var/log/samba/log.nmbd: Mod time differs
var/log/samba/log.nmbd: Size differs
Verify -rw——- root/utmp 384 2013-03-18 20:26 var/log/btmp-20130401
Verify -rw-r–r– root/root 1382 2013-04-11 13:53 var/log/kadmind.log
Verify drwxrwx–T root/gdm 0 2013-04-11 13:53 var/log/gdm/
Verify -rw-r–r– root/root 0 2013-04-11 13:53 var/log/gdm/:0-slave.log
Verify -rw-r–r– gdm/gdm 1387 2013-04-05 17:07 var/log/gdm/:0-greeter.log.1
Verify -rw-r–r– root/root 18983 2013-03-25 14:10 var/log/gdm/:0.log.3
Verify -rw-r–r– root/root 19028 2013-04-05 17:06 var/log/gdm/:0.log.1
Verify -rw-r–r– root/root 18983 2013-03-22 15:42 var/log/gdm/:0.log.4
Verify -rw-r–r– root/root 98 2013-04-05 17:07 var/log/gdm/:0-slave.log.1
Verify -rw-r–r– root/root 18983 2013-04-11 13:53 var/log/gdm/:0.log
tar: Exiting with failure status due to previous errors
Due to long list of output, some of output has been hidden in above command output.

 

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>