Learn tar command and know about its precaution

Learn tar command and know about its precaution

Tar(Tap Archive) command is used in Unix like operating system for archiving the files.
Archive is a file which has number of files and these files can be extracted to its original format with the help of extraction
programmes.
These archived files are called tarballs. If you have ever downloaded any source files of programmes,
they are generally you can get in tar format for eg. packag-name.tar.gz i.e called tarball .

Note: tar does not perform compression but these archives file can be compressed by using compression utilities for eg. gzip(z),
bzip2(bj2),compress(Z) etc

How to tar

These are the flags which are used in tar command

-c = create archive
-v = verbose
-f = use file archive
-x = extract

Use the below given command

tar give-tar-name.tar file1 file2 file3

or

tar give-tar-name.tar directory-name

or

tar give-tar-name.tar file1 file2 file3 directory-name

eg.

linux@tuxworld:/tmp$ tar -cvf sharad.tar file1 file2 test
file1
file2
test/
test/dddd
linux@tuxworld:/tmp$ tar -tvf sharad.tar 
-rw-rw-r-- linux/linux       8 2013-07-10 07:02 file1
-rw-rw-r-- linux/linux       0 2013-07-10 07:02 file2
drwxrwxr-x linux/linux       0 2013-07-10 09:58 test/
-rw-rw-r-- linux/linux       0 2013-07-10 09:58 test/dddd
linux@tuxworld:/tmp$ 

Using compression with tar

Many compression programs are available with tar, such as gzip, bzip2, xz, lzip, lzma, or compress,
Here we are taking only two compression utilities which are generally used.

These are the flags which represent the compresion utilities:

-z = gzip (tar.gz)
-j = bzip2 (tar.bj2)

Note: Use these flags alongwith -c (create) and -x (extract) repective of compression.

Below are eg. of creating tarball with compression

gzip:
	tar -cvzf test.tar.gz file1 file2
        tar -cvzf test.tar.gz directory-name
bzip2:
	tar -cvjf test.tar.bj2 file1 file2
	tar -cvjf test.tar.bj2 directory-name

Extracting tarball

Use -x option for extract. In case if there is any compression use the compression flag with -x
for eg. -xz (for gzip) , -xj (for bzip2)


gzip:
	tar -xvjf test.tar.gz 
bzip2:
	tar -xvjf test.tar.bj2
	

How to list files and directory from tarball without extracting

Use -t flag with tar command.

tar -tvf package-name.tar
tar -tvf package-name.tar.gz
tar -tvf package-name.tar.bj2

See the below example.


linux@tuxworld:/tmp$ tar -tvf sharad.tar
-rw-rw-r-- linux/linux       8 2013-07-10 07:02 file1
-rw-rw-r-- linux/linux       0 2013-07-10 07:02 file2
drwxrwxr-x linux/linux       0 2013-07-10 09:58 test/
-rw-rw-r-- linux/linux       0 2013-07-10 09:58 test/dddd
linux@tuxworld:/tmp$ 

linux@tuxworld:/tmp$ tar -tvf sharad.tar.bj2 
-rw-rw-r-- linux/linux       8 2013-07-10 07:02 file1
-rw-rw-r-- linux/linux       0 2013-07-10 07:02 file2
drwxrwxr-x linux/linux       0 2013-07-10 09:58 test/
-rw-rw-r-- linux/linux       0 2013-07-10 09:58 test/dddd
linux@tuxworld:/tmp$ 

linux@tuxworld:/tmp$ tar -tvf sharad.tar.gz 
-rw-rw-r-- linux/linux       8 2013-07-10 07:02 file1
-rw-rw-r-- linux/linux       0 2013-07-10 07:02 file2
drwxrwxr-x linux/linux       0 2013-07-10 09:58 test/
-rw-rw-r-- linux/linux       0 2013-07-10 09:58 test/dddd
linux@tuxworld:/tmp$

How to extract tarball to other location

For eg. when you use command tar -xvzf package-name.tar.gz it extract the files in current directory. If you want to extract
to some other path ,use flag -C (Capital C)

See the below eg.


	tar -xvf test.tar -C /opt

	tar -xvjf test.tar.gz  -C /opt

	tar -xvjf test.tar.bj2 -C /opt

Add file or directory into existing tarball

Use -r option for adding file or directory into existing tarball

linux@tuxworld:/tmp$ tar -rvf sharad.tar newfile 
newfile
linux@tuxworld:/tmp$ tar -tvf sharad.tar
-rw-rw-r-- linux/linux       8 2013-07-10 07:02 file1
-rw-rw-r-- linux/linux       0 2013-07-10 07:02 file2
drwxrwxr-x linux/linux       0 2013-07-10 09:58 test/
-rw-rw-r-- linux/linux       0 2013-07-10 09:58 test/dddd
-rw-rw-r-- linux/linux       9 2013-07-10 11:42 newfile
linux@tuxworld:/tmp$

linux@tuxworld:/tmp$ mkdir test-dir
linux@tuxworld:/tmp$ tar -rvf sharad.tar test-dir
test-dir/
linux@tuxworld:/tmp$ tar -tvf sharad.tar
-rw-rw-r-- linux/linux       8 2013-07-10 07:02 file1
-rw-rw-r-- linux/linux       0 2013-07-10 07:02 file2
drwxrwxr-x linux/linux       0 2013-07-10 09:58 test/
-rw-rw-r-- linux/linux       0 2013-07-10 09:58 test/dddd
-rw-rw-r-- linux/linux       9 2013-07-10 11:42 newfile
drwxrwxr-x linux/linux       0 2013-07-10 11:50 test-dir/
linux@tuxworld:/tmp$ 


Note : You can not add new file or directory in compressed archived tarball. You will get the following error.

linux@tuxworld:/tmp$ tar -rvzf sharad.tar.gz newfile 
tar: Cannot update compressed archives
Try `tar --help' or `tar --usage' for more information.
linux@tuxworld:/tmp$ 

Extract particular file or directory from tar, tar.gz and tar.bj2

Use the below given format

tar -xvf archive-file-name.tar /path/of/fileORdir

Or

tar -xvzf archive-file-name.tar.gz /path/of/fileORdir

Or

tar -xvjf archive-file-name.tar.bj2 /path/of/fileORdir

eg.

linux@tuxworld:/tmp/workshop$ tar -tvf sharad.tar 
-rw-rw-r-- linux/linux       8 2013-07-10 07:02 file1
-rw-rw-r-- linux/linux       0 2013-07-10 07:02 file2
drwxrwxr-x linux/linux       0 2013-07-10 09:58 test/
-rw-rw-r-- linux/linux       0 2013-07-10 09:58 test/dddd
-rw-rw-r-- linux/linux       9 2013-07-10 11:42 newfile
drwxrwxr-x linux/linux       0 2013-07-10 11:50 test-dir/
linux@tuxworld:/tmp/workshop$ tar -xvf sharad.tar file2
file2
linux@tuxworld:/tmp/workshop$ ls -la
total 20
drwxrwxr-x  2 linux linux  4096 Jul 10 11:55 .
drwxrwxrwt 16 root  root   4096 Jul 10 11:54 ..
-rw-rw-r--  1 linux linux     0 Jul 10 07:02 file2
-rw-rw-r--  1 linux linux 10240 Jul 10 11:50 sharad.tar
linux@tuxworld:/tmp/workshop$ 

Using wildcard for multiple file or directory extraction from tarball

If you want to extract multiple files or directory with wildcard * (Asterisk), you have to use the switch called –wildcards.
The case is applicable to tar,tar.gz,tar.bj2
See below given eg.

linux@tuxworld:/tmp/workshop$ tar -xvf sharad.tar  --wildcards file*
file1
file2
linux@tuxworld:/tmp/workshop$ ls -la
total 24
drwxrwxr-x  2 linux linux  4096 Jul 10 12:01 .
drwxrwxrwt 16 root  root   4096 Jul 10 11:54 ..
-rw-rw-r--  1 linux linux     8 Jul 10 07:02 file1
-rw-rw-r--  1 linux linux     0 Jul 10 07:02 file2
-rw-rw-r--  1 linux linux 10240 Jul 10 11:50 sharad.tar
linux@tuxworld:/tmp/workshop$

Precautions to take ,while using Tar command:

Because after extracting the tarball it may be possible the tarball has same name of file as you have file names in
current working directory. It will overwrite the file and which may cause the loss of data or any damage in filesystem etc.

for eg. in /opt I have some important files with name data.txt,list.txt,file1,important-file. I got the tarball from another machine
and extracted out in /opt . But in this case tarball already has same name of some file like data.txt and important-file. Here it
will overwrite these two files then and I will loss my data.
This kind of behaviour is also called tarbomb.

Always follow these steps while working with tar command.

(1) Before extracting the file always list the files inside the tarball by using -t option.
eg. tar -tvf tarbal-name.tar , tar -tvzf tarbal-name.tar.gz ,tar -tvjf tarbal-name.tar.bj2

(2) Always create new directory and move the tarball to new directory and then extract there.

   mkdir workshop
    mv tarbal-name.tar.gz worksop
    cd workshop
    tar -xvzf tarbal-name.tar.gz
    

(3) Or use -C flag which will extract the files to its directed path.
for eg.

    tar -xvzf tarbal-name.tar.gz -C /tmp
    

Leave a Comment

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