Compress and Archive Part 2
Welcome to part2 of Linux file compression and archiving. In this tutorial we will learn how to archive file.You can archive files and directories with the tool tar. This is very useful utility for *nix administrator to backup their files and easily move from one location to another.We will learn these commands in this tutorial tar –cf,tar –tf,tar –xf,tar –cjf,tar –tjf,tar –xjf,tar –zcf,tar –ztf,tar –zxf.
tar
tar stand for tape archive. This utility is use for collecting all files or directories in one file. This is good way to create backups/archives.
To create a tar file we use command tar.
Syntax tar –cf filename.tar file1 file2 dir1 dir2.
You can tar multiple files and directories at the same time.
-c option is used to create a new archive file
-f option is used to specify filename
-v option is for showing the progress/details of files being archived.
[fahmed@faraz ~]$ ls 123 Documents Music Public test2file Downloads news Templates upload.txt newstring.file Desktop Faraz Pictures test1 Videos [fahmed@faraz ~]$ tar -cf newtarfile.tar newstring.file test1 upload.txt [fahmed@faraz ~]$ ls 123 Documents Music Pictures test1 Videos Downloads Public test2file newstring.file Desktop Faraz newtarfile.tar Templates upload.txt [fahmed@faraz ~]$
[fahmed@faraz ~]$ tar -vcf newtarfile1.tar newstring.file test1 upload.txt Faraz newstring.file test1 upload.txt [fahmed@faraz ~]$
To list the contents of tar file we use command tar –tf filename.tar.
[fahmed@faraz ~]$ tar -tf newtarfile1.tar newstring.file test1 upload.txt Faraz/ [fahmed@faraz ~]$ tar -vtf newtarfile1.tar -rw-rw-r-- fahmed/fahmed 76 2016-02-08 10:56 newstring.file -rw-r--r-- fahmed/fahmed 15470 2016-01-28 16:14 test1 -rw-rw-r-- fahmed/fahmed 217 2016-02-08 10:55 upload.txt drwxr-xr-x root/root 0 2016-01-21 15:26 Faraz/ [fahmed@faraz ~]$
If we want to extract the contents of tar file we use –x option.
Syntax tar –xf filename.tar
First I am going to delete files which we have archived into tar (newtarfile.tar)
[fahmed@faraz ~]$ rm newstring.file test1 test2file [fahmed@faraz ~]$ ls 123 Documents Music Pictures upload.txt allfiles.zip Downloads Public newtarfile.tar Desktop Faraz Templates Videos [fahmed@faraz ~]$ tar -xf newtarfile.tar [fahmed@faraz ~]$ ls 123 Documents Music newtarfile.tar Templates Downloads newstring.file Pictures test1 Videos Desktop Faraz Public upload.txt [fahmed@faraz ~]$
The tar does not compress the file, to create the tar and bzip2 compressed file we sue –j option.
Syntax tar –cjf tarbzfile.tbz file1 file2.
[fahmed@faraz ~]$ tar -cjf tarbzfile.tbz newtarfile.tar test1 [fahmed@faraz ~]$ ls 123 Downloads tarbzfile.tbz newtarfile.tar Faraz Templates Videos upload.txt Desktop Music Pictures test1 Documents newstring.file Public [fahmed@faraz ~]$
To view contents of the tar bizp file we use –tjf options.
Syntax tar –tjf filename.tbz
The file format/extension is .tbz or .tar.bz2.
[fahmed@faraz ~]$ tar -tjf tarbzfile.tbz newtarfile.tar test1 [fahmed@faraz ~]$
You can expand or unarchive the bzip tar file by using -xjf options.
Syntax tar –xjf filename.tbz.
[fahmed@faraz ~]$ rm newtarfile.tar test1 [fahmed@faraz ~]$ ls 123 Documents Music Pictures Templates Downloads newstring.file Public upload.txt Videos Desktop Faraz tarbzfile.tbz [fahmed@faraz ~]$ tar -vxjf tarbzfile.tbz newtarfile.tar test1 [fahmed@faraz ~]$ ls 123 Downloads tarbzfile.tbz upload.txt Faraz newtarfile.tar Templates Videos Desktop Music Pictures test1 Documents newstring.file Public [fahmed@faraz ~]$
To create a tar with gzip compressed file use –z option.
Syntax tar –zcf filename.tgz file1 file2.
The file format/extension will be in .tgz or .tar.gz.
[fahmed@faraz ~]$ tar -zcf targzip.tgz upload.txt test1 123 [fahmed@faraz ~]$ ls 123 Downloads upload.txt Templates Faraz targzip.tgz Videos test1 Desktop Music Pictures Documents newstring.file Public [fahmed@faraz ~]$
To list conetnts of tar gzip file use –tzf option.
Syntax tar –ztf filename.tgz.
[fahmed@faraz ~]$ tar -ztf targzip.tgz upload.txt test1 123 [fahmed@faraz ~]$
To unarchive tar gzip file use –zxf options.
Syntax tar –zxf filename.tgz.
[fahmed@faraz ~]$ rm upload.txt 123 test1 [fahmed@faraz ~]$ ls Downloads newstring.file Pictures targzip.tgz Videos Desktop Faraz Public Templates Documents Music [fahmed@faraz ~]$ tar -vzxf targzip.tgz upload.txt test1 123 [fahmed@faraz ~]$ ls 123 Downloads upload.txt Templates Faraz targzip.tgz Pictures Videos Desktop Music Documents newstring.file Public test1 [fahmed@faraz ~]$
This brings end to our part 2 tutorial. In this tutorial you have learnt archiving and unarchiving of file with gzip and bzip2 compress techniques.We learnt commands like tar –cf,tar –tf,tar –xf,tar –cjf,tar –tjf,tar –xjf,tar –zcf,tar –ztf,tar –zxf. Now I hope you can confidently compress/decompress and archive and extract files.you can subscribe free to get latest updates regarding our new tutorial. Feel free to subscribe
Thanks