Archiving and Compressing Files in Linux


When talking about archiving and compressing files under Linux, we’ll need to discuss about tar and gzip commands.
gzip or GNU Zip is the standard used in today’s distributions for compressing files and folders. To compress a file simply use gzip file_name and to decompress it use gunzip file_name.gz. Note that compressed files have the .gz extension. Use the gzip -r directory option to recursively compress files within a directory:

Compress files within a directory
Use gunzip -r directory to recursively decompress files within a directory:
gunzip command
gzip does not have any additional functionality, it’s only used to compress/decompress files.
To achieve further results like archiving you will need to use tar tool. To create an archive from multiple files use the tar cvf archive_name.tar file1 file2:
tar command
 -c, –create               create a new archive
 
 -v, –verbose              verbosely list files processed
 
-f, –file=ARCHIVE         use archive file or device ARCHIVE
      –force-local          archive file is local even if it has a colon
As you can see from the output of this command, archives have .tar extension. To unpack an archive use tar -xvf archive_name.tar command.
-x, –extract, –get       extract files from an archive
To view the contents of an archive use the following command: tar tvf archive_name.tar:
View archive content using tar
 -t, –list                 list the contents of an archive
      –test-label           test the archive volume label and exit
Other important options that can be used with this command, are the following:
-d, –diff, –compare      find differences between archive and file system
      –delete               delete from the archive (not on mag tapes!)
 
-r, –append               append files to the end of an archive
 
-u, –update               only append files newer than copy in archive
 
 -p, –preserve-permissions, –same-permissions
                             extract information about file permissions
                             (default for superuser)
      –preserve             same as both -p and -s
      –same-owner           try extracting files with the same ownership as
                             exists in the archive (default for superuser)
Note that sometimes you may stumble upon .tar.gz files which means that an archive file was compressed. In the following example I’ve created an archive from three folders and then compressed it using gzip. The result is an compressed archive with a lower size than the original file:
How to compress and archive in Linux
To decompress and extract the archive you will need to use the two commands in the following order:
Decompress an archive in linux
There is a faster way to achieve the same results by sending gunzip command to standard output which in this case is targunzip -dc files.tar.gz | tar xvf – 
– d is used to descompress files and -c sends the result to standard output
Another method for achieving the similar results is by using the zcat files.tar.gz | tar xvf – command:
decompress and extract archives in Linux
You can invoke the gzip tool directly in tar command when extracting or verifying an archive by adding the –z parameter:
Linux tar command
The same option is used when creating an archive using tar that invokes the gzip command. Below is a comparison between two executions of tar command, one with the z option and the other one without (check out the size of the files):
tar -czvf slow.log.tar.ggz slow.log slow1.log slow2.log
Archiving and Compressing Files in Linux
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s