Tar/GZip Files in One Operation, Unattached to the Terminal Session

When you’re try­ing to move a large block of files, its often use­ful to do so in one com­mand and to be able to close your ter­mi­nal win­dow (or allow it to time out). If you run a com­mand under nor­mal cir­cum­stances, los­ing the con­nec­tion can cause your com­mand to ter­mi­nate pre­ma­ture­ly, this is where nohup (No HangUP — a util­i­ty which allows a process to con­tin­ue even after a con­nec­tion is lost) comes in.

Let’s say we have a large direc­to­ry to back­up, which we want to first tar, then gzip; keep­ing the com­mand non-dependent on the con­ti­nu­ity of the ter­mi­nal ses­sion.

Here’s an exam­ple, which shows the direc­to­ry to be zipped, then runs the tar com­mand via nohup.

ccase@midas ~]$ ls -l
total 10915700
drwxrwxr-x 27 ccase ccase        4096 2012-03-28 11:30 backup
 
ccase@midas ~]$ nohup tar -zcvf backup.tgz backup/

Here is a descrip­tion of the option flags used for the tar com­mand above.

-Z
     `tar' will use the `compress' program when reading or writing the
     archive.  This allows you to directly act on archives while saving
     space.

--verbose
-v
     Be more verbose about the operation.
     This option can be specified multiple times (for some operations)
     to increase the amount of information displayed.

-c
--create
     Create a new archive (or truncate an old one) and write
     the named files to it.

--file=ARCHIVE
-f ARCHIVE
     `tar' will use the file ARCHIVE as the `tar' archive it performs
     operations on, rather than `tar''s compilation dependent default.

-x
     --extract
     --get         Extract files from an archive.  The owner, modification
                   time, and file permissions are restored, if possible.  If
                   no file arguments are given, extract all the files in the
                   archive.  If a filename argument matches the name of a
                   directory on the tape, that directory and its contents are
                   extracted (as well as all directories under that direc-
                   tory).  If the archive contains multiple entries corre-
                   sponding to the same file (see the --append command
                   above), the last one extracted will overwrite all earlier
                   versions.

To reverse this oper­a­tion and extract the archive in one oper­a­tion, unzip­ping and untar­ring the files, you sim­ply issue the fol­low­ing com­mand, using the –x flag instead of the –c flag, to extract.

ccase@midas ~]$ tar -zxvf backup.tgz

Leave a Reply

Your email address will not be published. Required fields are marked *