Home AIX 18 Tar Command Examples – Unix/Linux

18 Tar Command Examples – Unix/Linux

0

In Unix- Linux , the name of the tar command is short for tape archiving, the storing of entire file systems onto magnetic tape, which is one use for the command. However, a more common use for tar is to simply combine a few files into a single file, for easy storage and distribution.

The tar command used to rip a collection of files and directories into highly compressed archive file commonly called tarball or tar, gzip and bzip in Unix – Linux. The tar is most widely used command to create compressed archive files and that can be moved easily from one disk to another disk or machine to machine.

1 Create tar Archive File

The below example command will create a tar archive file tecmint-14-09-12.tar for a directory /home/tecmint in current working directory. See the example command in action.

[root@cent emre]# ls -ltr
total 0
-rw-r--r--. 1 root root 0 Mar 21 11:57 emrepics3.jpg
-rw-r--r--. 1 root root 0 Mar 21 11:57 emrepics2.jpg
-rw-r--r--. 1 root root 0 Mar 21 11:57 emrepics1.jpg
-rw-r--r--. 1 root root 0 Mar 21 11:57 emrepics9.jpg
-rw-r--r--. 1 root root 0 Mar 21 11:57 emrepics8.jpg

[root@cent emre]# cd ..
[root@cent ~]# ls -ltr
total 0
drwxr-xr-x. 2 root root 195 Mar 21 11:57 emre
[root@cent ~]# tar -cvf emrepics.tar emre
emre/
emre/emrepics1.jpg
emre/emrepics2.jpg
emre/emrepics3.jpg
emre/emrepics4.jpg
emre/emrepics5.jpg

Let’s discuss each option that we have used in the above command for creating a tar archive file.

  1. c – Creates a new .tar archive file.
  2. v – Verbosely show the .tar file progress.
  3. f – File name 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 emrepic.tar.gz file for the directory /emre. (Note : tar.gz and tgz both are similar).

[root@cent ~]# tar cvzf emrepic.tar.gz emre
emre/
emre/emrepics1.jpg
emre/emrepics2.jpg
emre/emrepics3.jpg
emre/emrepics4.jpg
emre/emrepics5.jpg

[root@cent ~]# ls -ltr
total 4
drwxr-xr-x. 2 root root 195 Mar 21 11:57 emre
-rw-r--r--. 1 root root 213 Mar 21 12:13 emrepic.tar.gz

3 Create tar.bz2 Archive File

The bz2 feature compress and create archive file less than the size of the gzip. The bz2 compression takes more time to compress and decompress files as compared to gzip which takes less time. To create highly compressed tar file we use option as j. The following example command will create a emrepic.tar.bz2 file for a directory /emre . (Note: tar.bz2 and tbz is similar as tb2).

[root@cent ~]# tar cvfj emrepic.tar.bz2 emre
emre/
emre/emrepics1.jpg
emre/emrepics2.jpg
emre/emrepics3.jpg
emre/emrepics4.jpg

[root@cent ~]# ls -l
total 4
drwxr-xr-x. 2 root root 195 Mar 21 11:57 emre
-rw-r--r--. 1 root root 222 Mar 21 12:15 emrepic.tar.bz2

4 Untar tar Archive File

To untar or extract a tar file, just issue following command using option x (extract). For example the below command will untar the file emrepic.tar in present working directory. If you want to untar in a different directory then use option as -C (specified directory).

root@cent ~]# tar -xvf emrepic.tar -C emre2
emre/
emre/emrepics1.jpg
emre/emrepics2.jpg
emre/emrepics3.jpg
emre/emrepics4.jpg
emre/emrepics5.jpg
Untar files in specified Directory ##
[root@cent ~]# tar -cvf emrepic.tar
emre/
emre/emrepics1.jpg
emre/emrepics2.jpg
emre/emrepics3.jpg
emre/emrepics4.jpg

5 Uncompress tar.gz Archive File

To Uncompress tar.gz archive file, just run following command. If would like to untar in different directory just use option -C and the path of the directory,  like we shown in the above example.

[root@cent ~]# tar -xvf emrepics.tar.gz
emre/
emre/emrepics1.jpg
emre/emrepics2.jpg
emre/emrepics3.jpg
emre/emrepics4.jpg
emre/emrepics5.jpg

6 Uncompress tar.bz2 Archive File

To Uncompress highly compressed tar.bz2 file, just use the following command. The below example command will untar all the .jpg files from the archive file.

[root@cent ~]# tar -xvf emrepics.tar.bz2 
emre/
emre/emrepics1.jpg
emre/emrepics2.jpg
emre/emrepics3.jpg
emre/emrepics4.jpg

7 List Content of tar.gz Archive File

Use the following command to list the content of tar.gz file.

[root@cent ~]# tar -tvf emrepics.tar.gz
drwxr-xr-x root/root         0 2018-03-21 11:57 emre/
-rw-r--r-- root/root         0 2018-03-21 11:57 emre/emrepics1.jpg
-rw-r--r-- root/root         0 2018-03-21 11:57 emre/emrepics2.jpg
-rw-r--r-- root/root         0 2018-03-21 11:57 emre/emrepics3.jpg
-rw-r--r-- root/root         0 2018-03-21 11:57 emre/emrepics4.jpg

8 List Content tar.gz Archive File

To list the contents of tar archive file, just run the following command with option t (list content). The below command will list the content of emrepics.tar file.

[root@cent ~]# tar -tvf emrepics.tar
drwxr-xr-x root/root         0 2018-03-21 11:57 emre/
-rw-r--r-- root/root         0 2018-03-21 11:57 emre/emrepics1.jpg
-rw-r--r-- root/root         0 2018-03-21 11:57 emre/emrepics2.jpg
-rw-r--r-- root/root         0 2018-03-21 11:57 emre/emrepics3.jpg

9 List Content tar.bz2 Archive File

To list the content of tar.bz2 file, issue the following command.

[root@cent ~]# tar -tvf emrepics.tar.bz2
drwxr-xr-x root/root         0 2018-03-21 11:57 emre/
-rw-r--r-- root/root         0 2018-03-21 11:57 emre/emrepics1.jpg
-rw-r--r-- root/root         0 2018-03-21 11:57 emre/emrepics2.jpg
-rw-r--r-- root/root         0 2018-03-21 11:57 emre/emrepics3.jpg

10 Untar Single file from tar File

To extract a single file called emrepics9.jpg from emrepics.tar use the following command.

[root@cent ~]# tar --extract --file=emrepics.tar emre/emrepics9.jpg
[root@cent ~]# tar -xvf emrepic.tar emre/emrepics1.jpg
emre/emrepics1.jpg

11 Untar Single file from tar.gz File

To extract a single file emrepics9.jpg from emrepics.tar.gz archive file, use the command as follows.

[root@cent /]# tar -zxvf /root/emrepics.tar.gz emre/emrepics9.jpg
emre/emrepics9.jpg

12 Untar Single file from tar.bz2 File

To extract a single file called emrepics9.jpg from the file emrepics.tar.bz2 use the following option.

[root@cent /]# tar -jxvf /root/emrepics.tar.bz2 emre/emrepics9.jpg
emre/emrepics9.jpg

13 Untar Multiple files from tar, tar.gz and tar.bz2 File

To extract or untar multiple files from the tartar.gz and tar.bz2 archive file. For example the below command will extract “file 1” “file 2” from the archive files.

# tar -xvf emrepics.tar "file 1" "file 2" 
# tar -zxvf emrepics.tar.gz "file 1" "file 2" 
# tar -jxvf emrepics.tar.bz2 "file 1" "file 2"

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 .jpg from a tar, tar.gz and tar.bz2 archive file.

[root@cent /]# tar -xvf /root/emrepics.tar --wildcards '*.jpg'
emre/emrepics1.jpg
emre/emrepics2.jpg
[root@cent /]# tar -zxvf /root/emrepics.tar.gz --wildcards '*.jpg'
emre/emrepics1.jpg
emre/emrepics2.jpg
[root@cent /]# tar -jxvf /root/emrepics.tar.bz2 --wildcards '*.jpg'
emre/emrepics1.jpg
emre/emrepics2.jpg

15 Add Files or Directories to tar Archive File

To add files or directories to existing tar archived file we use the option r (append). For example we add file emrepics10.jpg and directory php to existing tecmint-14-09-12.tar archive file.

# tar -rvf emrepics.tar emrepics10.jpg

16 Add Files or Directories to tar.gz and tar.bz2 files

The tar command don’t have a option to add files or directories to an existing compressed tar.gz and tar.bz2archive file. If we do try will get the following error.

# tar -rvf emrepics.tar.gz emrepics10.jpg
# tar -rvf emrepics.tar.bz2 emrepics10.jpg

17 How To Verify tar, tar.gz and tar.bz2 Archive File

To verfify any tar or compressed archived file we use option as W (verify). To do, just use the following examples of command. (Note : You cannot do verification on a compressed ( *.tar.gz, *.tar.bz2 ) archive file).

# tar tvfW emrepics.tar
tar: This does not look like a tar archive
tar: Skipping to next header
tar: Archive contains obsolescent base-64 headers

18 Check the Size of the tar, tar.gz and tar.bz2 Archive File

To check the size of any tartar.gz and tar.bz2 archive file, use the following command. For example the below command will display the size of archive file in Kilobytes (KB).

[root@cent ~]# tar -czf - emrepic.tar |wc -c
258

NO COMMENTS

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Exit mobile version