Wednesday, September 3, 2014

[Quick Tips] : Learn tar command and know about its precaution

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.
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
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)
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.
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.
Add file or directory into existing tarball
Use -r option for adding file or directory into existing tarball
Note : You can not add new file or directory in compressed archived tarball. You will get the following error.
Extract particular file or directory from tar, tar.gz and tar.bj2
Use the below given format
eg.
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.

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.
(3) Or use -C flag which will extract the files to its directed path.
for eg.

No comments: