Cracking IT Interview



Creating Archive


Making an archival is nothing but creating a unit or bundle of group of files. Creating archival works excellent with compression of files. It is very useful when we store files in system or it is shared over communication channels. Archived files are frequently used for packaging software for distribution and for other various important stuff.


tar command


This is also referred to as "tape-archive" or "tarball".  We use tar command to make an archival of group of files. We generally give the file extension as '.tar' for this archived file.


Syntax:

tar [options] target_file.tar [source_file_list separated by space]


Example:

$tar -cvf arch1.tar file1.txt file2.txt file3.txt
a file1.txt
a file2.txt
a file3.txt
$ls -l arch1.tar
-rw-r--r--  1 pinku  guest  3072 Jan  7 05:05 arch1.tar
$

 






Options with tar command:


  • -c :Create a new archive containing the specified items 


  • -v  :verbose mode, list each file which is written to the archive 


  • -f  : file, read the archive from or write the archive to the specified file 


  • -x  : extract files, extract to disk from an archive 


  • -t  : display all the files in standard output 


  • -r  : new entries are appended to the archive


  • -u  : new entries are added in the archive only if they have a modification date newer than the corresponding entry


  • -z  : compress the resulting archive with gzip command 

  • -Z  : compress the resulting archive with compress command 



Examples:


Creating tarball: 


$tar -cvf arch1.tar file1.txt file2.txt file3.txt
a file1.txt
a file2.txt
a file3.txt
$ls -l arch1.tar
-rw-r--r--  1 pinku  guest  3072 Jan  7 05:05 arch1.tar
$

$file arch1.tar
arch1.tar: POSIX tar archive
$


Creating tarball as well as compression using gzip:


​$tar -cvzf arch2.tar file1.txt file2.txt file3.txt
a file1.txt
a file2.txt
a file3.txt
$ls -l arch2.tar
-rw-r--r--  1 pinku  guest  187 Jan  7 05:24 arch2.tar
$








Creating tarball as well as compression using compress: we can see all the archive file size and compare the degree of compression.


$tar -cvZf arch3.tar file1.txt file2.txt file3.txt
a file1.txt
a file2.txt
a file3.txt
$ls -l arch3.tar
-rw-r--r--  1 pinku  guest  375 Jan  7 05:31 arch3.tar
$


To display / list out the files inside tarball:


$tar -tvf arch1.tar
-rwxr-xr-x  0 pinku  guest       0 Jan  6 03:40 file1.txt
-rwxr-xr-x  0 pinku  guest       0 Dec 24 03:06 file2.txt
-rw-r--r--  0 pinku  guest      18 Jan  1 01:18 file3.txt
$


Adding / Appending some files inside tarball:


​$tar -rvf arch1.tar file4.txt
a file4.txt
$tar -tvf arch1.tar
-rwxr-xr-x  0 pinku  guest       0 Jan  6 03:40 file1.txt
-rwxr-xr-x  0 pinku  guest       0 Dec 24 03:06 file2.txt
-rw-r--r--  0 pinku  guest      18 Jan  1 01:18 file3.txt
-rw-r--r--  0 pinku  guest      77 Jan  1 13:24 file4.txt
$


new entries are added in the archive only if they have a modification date newer than the corresponding entry


$tar -uvf arch1.tar file5
a file5
$tar -tvf arch1.tar
-rwxr-xr-x  0 pinku  guest       0 Jan  6 03:40 file1.txt
-rwxr-xr-x  0 pinku  guest       0 Dec 24 03:06 file2.txt
-rw-r--r--  0 pinku  guest      18 Jan  1 01:18 file3.txt
-rw-r--r--  0 pinku  guest      77 Jan  1 13:24 file4.txt
-rw-r--r--  0 pinku  guest      52 Dec 25 00:10 file5
$


​Extracting files from tarball:


$tar -xvf arch3.tar
x file1.txt
x file2.txt
x file3.txt
$


NEXT->