Lesson 1.6: Archive, compress, unpack, and uncompress files using tar, gzip, and bzip2
Non Compressed Archive ( Plain Archive ) / TAR
tar [option] <files/dir to be archived>
- [option]
- -c : create archive
- -v : show details (verbose)
- -f :
archive filename
- -t : list contents of an archive
- -r : add new files/dirs into an archive
- -x : extract files/dirs from an archive
- -z : create a gzip compressed archive (.tar.gz or .tgz)
- -j : create a bzip2 compressed archive (.tar.bz2 or .tbz)
Creating compressed archives with tar
Suppose you want to bundle all the files inside the etc directory with conf extension ( ls /etc/*.conf ).In such cases you can do this by:
tar -cvf <tar_filename> <source> <destination>
[sanjeeb@server ~]$ tar -cvf conffile.tar /etc/*.conf /home/sanjeeb/ [sanjeeb@server ~]$ ls conffile.tar Documents image.png Music Public Templates Videos Desktop Downloads Marketing Pictures Scripts test
Listing the content of the Tar file
tar -tvf <tar_filename>
[sanjeeb@server ~]$ tar -tvf conffile.tar -rw-r--r-- sanjeeb/sanjeeb 1525 2024-03-15 18:56 home/sanjeeb/image.png -rw------- sanjeeb/sanjeeb 20 2024-04-06 15:35 home/sanjeeb/.lesshst -rw------- sanjeeb/sanjeeb 10752 2024-04-06 15:32 home/sanjeeb/.viminfo ...
Appending new file to the existing Tar file
tar -tvf <tar_filename> <source>
[sanjeeb@server ~]$ tar -rvf conffile.tar image.png
Compressed Archive
A compressed archive is a single file or data structure that contains one or more files or directories that have been reduced in size using compression algorithms. The compression process removes redundant or unnecessary information from the files, reducing their size and conserving storage space. Compressed archives are commonly used for storing and transferring large amounts of data efficiently.
gzip
Compressing a tar file using gzip
[sanjeeb@server ~]$ gzip conffile.tar [sanjeeb@server ~]$ ls conffile.tar.gz Documents --help Marketing Pictures Scripts test Desktop Downloads image.png Music Public Templates Videos
De-Compressing a tar file using gunzip
[sanjeeb@server ~]$ gunzip conffile.tar.gz [sanjeeb@server ~]$ ls conffile.tar Documents --help Marketing Pictures Scripts test Desktop Downloads image.png Music Public Templates Videos
To create archive as well as compress archive in one command using gzip
tar -zcvf <tar.gz filename> <source> <destination>
[sanjeeb@server ~]$ tar -zcvf confile.tar.gz /etc/*.conf /home/sanjeeb/
bzip2
To create archive as well as compress archive in one command using bzip2
[sanjeeb@server ~]$ tar -zcvf confile.tar.tbz /etc/*.conf /home/sanjeeb/ [sanjeeb@server ~]$ ls -lh total 558M -rw-r--r--. 1 sanjeeb sanjeeb 76M Apr 6 18:07 confile.tar.gz -rw-r--r--. 1 sanjeeb sanjeeb 227M Apr 6 18:11 confile.tar.tbz
Compressing Tools
GZIP
- Compression Command: gzip
- Decompression Counterpart: gunzip
- Standard
.gz
[sanjeeb@assignmentserver class]$ ls -lh -rw-r--r--. 1 sanjeeb sanjeeb 817 Sep 19 17:00 xattr.conf [sanjeeb@assignmentserver class]$ gzip xattr.conf [sanjeeb@assignmentserver class]$ ls -lh -rw-r--r--. 1 sanjeeb sanjeeb 423 Sep 19 17:00 xattr.conf.gz [sanjeeb@assignmentserver class]$ gunzip xattr.conf.gz
BZIP2
- Compression Command: bzip
- Decompression Counterpart: bunzip
- Standard
.bz2
[sanjeeb@assignmentserver class]$ bzip2 xattr.conf [sanjeeb@assignmentserver class]$ ls -lh | grep .bz2 -rw-r--r--. 1 sanjeeb sanjeeb 490 Sep 19 17:00 xattr.conf.bz2 [sanjeeb@assignmentserver class]$ bunzip2 xattr.conf.bz2 [sanjeeb@assignmentserver class]$ ls -lh -rw-r--r--. 1 sanjeeb sanjeeb 817 Sep 19 17:00 xattr.conf # To provide full information using -v [sanjeeb@assignmentserver class]$ bzip2 -v xattr.conf xattr.conf: 1.667:1, 4.798 bits/byte, 40.02% saved, 817 in, 490 out.
ZIP
- Compression Command: zip
- Decompression Counterpart: unzip
- Standard
.zip
( Windows Compatible )
Generating Compressed Counterpart of the zip file without compressing the file itself.
# Using Bzip [sanjeeb@assignmentserver class]$ gzip -v -c vconsole.conf > vconsole.conf.gz vconsole.conf: -7.1% -- replaced with stdout [sanjeeb@assignmentserver class]$ ls -lh -rw-r--r--. 1 sanjeeb sanjeeb 28 Sep 19 17:00 vconsole.conf # Original File -rw-r--r--. 1 sanjeeb sanjeeb 62 Sep 19 18:26 vconsole.conf.gz # Compressed Counterpart # Using Zip [sanjeeb@assignmentserver class]$ zip -o dracut.conf.zip dracut.conf adding: dracut.conf (deflated 12%) [sanjeeb@assignmentserver class]$ ls | grep dracut dracut.conf dracut.conf.zip