Linux – Archiving and Compressing Files

Now you need to master gzip and tar.

gzip

The program  gzip  (GNU Zip) is one of the current standard Unix compression programs. A file that ends with .gz  is a GNU Zip archive. Use  gunzip  file.gz  to uncompress  <file>.gz  and remove the suffix; to compress it again, use gzip file.

tar

To create an archive, use tar

$ tar cvf archive.tar file1 file2 …

Archives created by tar  usually have a .tar  suffix (this is by convention; it isn’t required). For example, in the command above,  file1,  file2, and so on are the names of the files and directories that you wish to archive in <archive>.tar. The c flag activates create mode. The r and f flags have more specific roles.

The v flag activates verbose diagnostic output, causing  tar to print the names of the files and directories in the  archive  when  it  encounters  them.  Adding  another  v  causes  tar  to  print  details  such  as  file  size  and permissions. If you don’t want tar to tell you what it’s doing, omit the v flag.

The f  flag denotes the file option. The next argument on the command line after the f  flag must be the archive file for tar to create (in the preceding example, it is <archive>.tar). You must use this option followed by a filename at all times, except with tape drives. To use standard input or output, enter a dash (-) instead of the filename.

Unpacking tar files

To unpack a .tar file with tar use the x flag:

$ tar xvf archive.tar

In this command, the  x  flag puts  tar  into  extract  (unpack)  mode. You can extract individual parts of the archive by entering the names of the parts at the end of the command line, but you must know their exact names.

NOTE: When using extract mode, remember that tar does not remove the archived .tar file after extracting its contents.

Table-of-Contents Mode

Before unpacking, it’s usually a good idea to check the contents of a .tar file with the table-of-contents modeby using the t flag instead of the x flag. This mode verifies the archive’s basic integrity and prints the names of all files inside. If you don’t test an archive before unpacking it, you can end up dumping a huge mess of files into the current directory, which can be really difficult to clean up.

When unpacking, consider using the p  option to preserve permissions. Use this in extract mode to override your umask and get the exact permissions specified in the archive. The p option is the default when working as the superuser.

Compressed Archives (.tar.gz)

Many  beginners  find  it  confusing  that  archives  are  normally  found  compressed,  with  filenames  ending

in .tar.gz. To unpack a compressed archive, work from the right side to the left; get rid of the .gz first and then worry about the .tar. For example, these two commands decompress and unpack <file>.tar.gz:

$ gunzip file.tar.gz

$ tar xvf file.tar

When starting out, you can do this one step at a time, first running gunzip  to decompress and then  tar  to verify and unpack. To create a compressed archive, do the reverse; run  tar  first and  gzip  second.

zcat

A better way is to combine archival and compression functions with a pipeline. For example, this command pipeline unpacks <file>.tar.gz:

$ zcat file.tar.gz | tar xvf –

The zcat command is the same as gunzip -dc. The -d option decompresses and the -c option sends the result to standard output (in this case, to the tar command).

Because it’s so common to use zcat, the version of tar that comes with Linux has a shortcut. You can use z  as an option to automatically invoke gzip  on the archive; this works both for extracting an archive (with the x  or t  modes in tar) and creating one (with  c). For example, use the following to verify a compressed archive:

$ tar ztvf file.tar.gz

However, you should try to master the longer form before taking the shortcut.

Reference: How Linux Works: What Every Superuser Should Know by Brian Ward


Discover more from Embedded for All

Subscribe to get the latest posts sent to your email.

Leave a comment

Discover more from Embedded for All

Subscribe now to keep reading and get access to the full archive.

Continue reading

Design a site like this with WordPress.com
Get started