11 Examples How To Use Linux Tar Command And All Options

September 7, 2011 | By
| 2 Replies More

Tar command is the primary archiving utility. The tar command bundles a bunch of files together and creates an archive on the tape, disk drive or floppy disk. To create an archive using tar, use command like this, which bundles all the files in the current directory that end with .tar file.

Using tar command you can create tar archives (with gzip and bzip compression). An archiving program designed to store and extract files from an archive file known as a tarfile. The first argument to tar must be one of the options: a c d r t u x, followed by any optional functions. The final to tar are the names of the files or directories which should be archived. The use of a directory name always implies that the subdirectories below should be included in the archive.

These are the list of common options:

-c option is used to create a new archive.
-v verbosely list files which are processed.
-f following is the archive file name.
-t list the contents o fan archive.
-x extract files from archive.
-z filter the archive through gzip.
-C directory file, performs a chdir operation on directory and performs the c (create) or r (replace operation on file.


tar option(s) archive-name file-name(s)

Example: 1 Let us see how to tar the directory.

Here is a directory named abc. We use the –c option to create an linux tar archive named test.tar.

[root@localhost home]# ls
abc bha bhagyashree push

[root@localhost home]# tar cvf test.tar abc/

Example: 2 How to view the content of the tar file?

Here, we have created test.tar file. We will view the file content by using the –t option. It also gives the additional information like file permission, date, owner, etc.

[root@localhost home]# ls
abc bha bhagyashree push test.tar

[root@localhost home]# tar tvf test.tar
drwxr-xr-x root/root 0 2012-07-21 12:12 abc/
-rw-r--r-- root/root 0 2012-07-21 12:11 abc/1234
-rw-r--r-- root/root 0 2012-07-21 12:12 abc/12345
-rw-r--r-- root/root 0 2012-07-21 12:11 abc/123

Example: 3 Listing the content of the tar file.

[root@mailserver ~]# tar --list --file=backup.tar | more

Example: 4 Listing the content of the archive file in details.

[root@mailserver ~]# tar --list --verbose --file=backup.tar
drwxr-xr-x / 0 2012-05-01 02:39:11 sysstat-10.0.4/
-rw-rw-r-- / 247 2012-03-03 9:31:39 sysstat-10.0.4/version.in
-rw-r--r-- root/root 18504 2012-05-01 02:37:40 sysstat-10.0.4/act_sadf.o
-rw-rw-r-- / 60175 2012-03-03 19:31:39 sysstat-10.0.4/rd_stats.c
-rw-r--r-- root/root 70500 2012-05-01 02:37:44 sysstat-10.0.4/xml_stats.o

Example: 5

Let us know how to extract/restore the tar file in other directory. We will extract test.tar file in xyz directory. –xvf will copy all the files from test.tar file into the xyz directory.

[root@localhost home]# tar -C ./xyz/ -xvf test.tar

[root@localhost home]# cd xyz
[root@localhost xyz]# ls

Note: When restoring, you can specify the filenames that you want to restore by listing one or more pathnames after the device name. It is important to note that the pathname must match the name in the tar archive exactly, or it will not be restored.

Example: 6 Create an tar archive of everything in the current directory starting with an “i”.

[root@mailserver ~]# tar -cvf fullbackup.tar i*

Above command archived only those files, which starts from i.

Example : 7 Append a files or add a new file in existing tar archive.

[root@mailserver ~]# tar --append --file=backup.tar anaconda-ks.cfg

The above command will append an anaconda-ks.cfg file in backup.tar archive.

Example: 8 Extracting the file from the tar archive.

[root@mailserver ~]# tar --extract -vv --occurrence --file=./backup.tar anaconda-ks.cfg
-rw-r--r-- root/root 766 2008-04-12 06:52:42 anaconda-ks.cfg

The above command extract the anaconda-ks.cfg file in the backup.tar archive.

Example: 9 Adding two archive files with concatenate option.

[root@mailserver ~]# tar --concatenate --file=backup.tar fullbackup.tar

The above command add the content of the fullbackup.tar to backup.tar archive.

Example: 10 Tar command to make a backup.

Taking backup of /home directory,

# tar -zcvpf /root/move/home.tar.gz /home

Below tar option should be used to extract,

# tar -zxvf /path/to/location/home.tar.gz

Example: 11 How to preserve symbolic links using tar command?

Use tar -cvhf to preserve my symbolic links when generating a tar archive(Use the option “h” for that).


Tagged With :

Free Linux Ebook to Download

Comments (2)

Trackback URL | Comments RSS Feed

  1. Magesh M says:

    Simple and nice post,

    The below simply automation tar shell script will reduce your work on backup.


Leave a Reply

Commenting Policy:
Promotion of your products ? Comment gets deleted.
All comments are subject to moderation.