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


When you’re trying to move a large block of files, its often useful to do so in one command and to be able to close your terminal window (or allow it to time out). If you run a command under normal circumstances, losing the connection can cause your command to terminate prematurely, this is where nohup (No HangUP – a utility which allows a process to continue even after a connection is lost) comes in.

Let’s say we have a large directory to backup, which we want to first tar, then gzip; keeping the command non-dependent on the continuity of the terminal session.

Here’s an example, which shows the directory to be zipped, then runs the tar command 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 description of the option flags used for the tar command 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 operation and extract the archive in one operation, unzipping and untarring the files, you simply issue the following command, 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 *