6:00 PMArchiving 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.
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
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.
Lets’ say we have a file named listOffiles.lst which contains 10 files or directories with their absolute path.
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;
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:
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:
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 :)
You may also like to see:
|An example to understand bash exit code $? in linux.|
|Password-less Login to Linux/Unix Servers in Putty|
|Useful tips and tricks while working in Linux.|
|Some useful examples of netstat command in Linux for network statistics|
|Guide for working with zipped and archived files in Linux/AIX|
|Total comments: 0|