6:00 PM Archiving and compression of files in Linux |
Let’s see some archiving and compression tool used in Linux to manage filesystem space. This is free necessaary to recover and free the filesystem where lots of files are generated every day. TAR: A GNU archiving utility. Format: tar [options] archive filesToArchive Mostly used Options: -r, --append: append files to the end of an archive -v, --verbose: verbosely list files processed -f, --file=[HOSTNAME:]F use archive file or device F (default /dev/rmt0) -t, --list list the contents of an archive -c, --create create a new archive -x, --extract, --get extract files from an archive -k, --keep-old-files keep existing files; don't overwrite them from archive Example1: tar -rvf testFile.tar testFile The above command will create an archive of file testFile into testFile.tar. But, we generally don’t create tar archive of a single file. The tar tool is used to create a single archive of several files or directory. The main advantage is that one can create an archive of hundreds of files in one go with the same file access and permissions. Later one can send it another machine and un-tar it to have the same file structure. For example: Lets’ say we have a file named listOffiles.lst which contains 10 files or directories with their absolute path. cat listOffiles.lst /home/user1/test/file1 /home/user1/test/file2 /home/user1/test/testDir ------------------------------ /home/user1/test/file10 Now we will create an archive of all these files, we can do it using a “for loop” which will traverse all the files and create a single archive for it. for i in $(cat listOffiles.lst ); do ##The below command will create an archive of all files listed in listOffiles.lst with name oneArchive.tar tar –rvf oneArchive.tar $i; done If you want to see the content of the archive, you can use below command: tar –tvf oneArchive.tar Now we have the archive ready. We would like to compress this before transferring it to somewhere else. So now we will see how to compress this archive. gzip tool: used to compress or expand files GZIP reduces the size of the named files using Lempel-Ziv coding (LZ77). Whenever possible, each file is replaced by one with the extension .gz, while keeping the same ownership modes, access and modification times. Format: gzip [options] fileToCompress Mostly used options: -c --stdout --to-stdout Writes output on standard output; keep original files unchanged. If there are several input files, the output consists of a sequence of independently compressed members. -d --decompress --uncompress Decompress. -f --force : Forces compression or decompression even if the file has multiple links or the corresponding file already exists, or if the compressed data is read from or written to a terminal. -l --list For each compressed file, list the following fields: compressed size: size of the compressed file uncompressed size: size of the uncompressed file ratio: compression ratio (0.0% if unknown) uncompressed_name: name of the uncompressed file
-v --verbose : Verbose. Display the name and percentage reduction for each file compressed or decompressed. Example: To create a zip file i.e. to compress a file, we do following:
gzip fileToCompress But above command will create a zip file with name fileToCompress.gz and will delete the original file. So here we can use –c options to create zip file and send to console while keeping original file unchanged. Later we can redirect the console output to a file named *.gz.
gzip –c fileToCompress>zipfile.gz
You can see the difference of size between compressed file and original file.
To see the content of zip file, we can use below command: tar -xvf zipfile.gz The output is: /home/user1/test/file1 /home/user1/test/file2 /home/user1/test/file3 ------------------------------ /home/user1/test/file10 Now if you want to de-compress it again, you can do it using below command: gzip -d zipfile.gz If you like it, share it :) |
|
Related blogs
You may also like to see:
[2014-11-25] | [Open System-Linux] |
DMESG command in Linux : to display or control kernel ring buffer |
[2014-12-05] | [Open System-Linux] |
PIDSTAT command for debugging and analysis of Performance of Linux process |
[2014-12-22] | [Open System-Linux] |
Key mapping inside VI editor |
[2016-03-17] | [Open System-Linux] |
Some amazing command shortcuts in Linux |
[2014-05-22] | [Open System-Linux] |
Archiving and compression of files in Linux |
Total comments: 0 | |