In Linux operating systems, archiving and compression are two separate terms. Archiving means creating a file containing multiple files and directories to make the copy operation much more accessible.
Compression means compressing data to take up less space. In Linux, the tar command is used for archiving, and for compressing gzip or bzip2. The gzip command uses Lempel-Ziv encoding (LZ77), and the bzip2 uses the Burrows-Wheeler compression algorithm and Huffman encoding, which provides better compression than with LZ77 coding.
1. tape (tape archive) – represents the main archive command on Linux. Tar does not compress (in default) but only archiving.
tar options files/dirs
The tar command will necessarily have one of the options:
1.-c = create – create an archive
2. -x = extract – extract files from the archive
3.-t = table of content (content view)
4.-r = add to the end of the archive
5.-u = adds only new files to the archive
Using the -x option can overwrite the files that exist on the hard drive with the ones that are unzipped.
Other options: -f = file (specify the archive file that is created) -v = verbose – atime-preserve = no change for added files
a. archive.tar is created in the current directory in the directory
b. Viewing the contents of an archive
tar -tf arhive.tar
c. add file a.txt to archive.tar
tar -rf arhive.tar a.txt
d. Add new files to the archive. These are added, old versions of the archive are not replaced.
tar -uf arhive.tar f *
Keep in mind for adding files you need to use
tar – ame – preserve – cvf arhive.tar dir1 /
f. Extract only a file from the archive
tar -xzvf arhiva.tar a.txt
2. BZIP2, GZIP
The tar program can also be used to compress files. Specific options are used which have the effect of passing files through a compression program before archiving.
Component Target Compression Options:
-j = create bzip2 (the archive file is passed through the bzip2 compression filter)
In the current directory create the archive of the /home /user/dir1 directory called archivefile.tar.bz2compressed with bzip2
tar-cvjf archivefile.tar.bz2 /home/user/dir1
tar-cvf – /home/user/dir1 | bzip2> archivefile.tar.bz2
Extract archive bz2:
tar -xjvf archive.tar.bz2
bunzip2 arhive.tar.bz2 – archive.tar file results
tar -xvf archive.tar
-z = create gzip archives
tar-czvf arhive.tar.gz dir1 /
tar -xzvf arhive.tar.gz
There are also zip, unzip, gzip and gunzip commands, but they are less used because in general the tar command is used together with compression options.
Backup to the /etc directory in /root: tar-user/root/etc – $ (data +% F) .tar.bz2/etc
Why four different versions of compression programs?
compress was the standard compression program inherited from Linux. gzip has been used by almost two generations of Unix users, compresses better than compress and everyone has been delighted with it. Then someone needed something better and created bzip2, which has significantly improved compression rates. In any case, bzip2 works faster, and may be significantly slower when working with large files. Of course, zip is only used for compatibility with Windows users.