11 Examples How To Use Linux Tar Command And All Options

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 of an 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.

[[email protected] home]# ls
abc bha bhagyashree push

[[email protected] 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.

[[email protected] home]# ls
abc bha bhagyashree push test.tar

[[email protected] 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.

[[email protected] ~]# tar --list --file=backup.tar | more

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

[[email protected] ~]# 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.

[[email protected] home]# tar -C ./xyz/ -xvf test.tar

[[email protected] home]# cd xyz
[[email protected] 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”.

[[email protected] ~]# 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.

[[email protected] ~]# 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.

[[email protected] ~]# 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.

[[email protected] ~]# 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).

Bobbin Zachariah 7:57 pm

About Bobbin Zachariah

Founder of LinOxide, passionate lover of Linux and technology writer. Started his career in Linux / Opensource from 2000. Love traveling, blogging and listening music. Reach Bobbin Zachariah about me page and google plus page.

Author Archive Page

Have anything to say?

Your email address will not be published. Required fields are marked *

All comments are subject to moderation.