How To Tar Linux Files Into .tgz Compressed Extension

April 3, 2013 | By in HOWTOS, LINUX COMMANDS
| Reply More

The program tar (originally for tape archive) is useful for archiving and transmitting files. For example, you may want to 'tar up' all your work and save it to your own computer's disk drive so you don't run into quota problems. You might also want to submit an entire directory at once rather than the individual files in the directory. The tar program is useful for these and other tasks and is simple to use. Lets have some basic tar examples to build the conversation here.

More Linux Commands

To tar all .cc and .h files into a tar file named foo.tgz use

# tar cvzf foo.tgz *.cc *.h

This creates (c) a compressed (z) tar file named foo.tgz (f) and shows the files being stored into the tar file (v). The .tgz suffix is a convention for gzipped tar files, it's useful to use the convention since you'll know to use z to restore/extract.

It's often more useful to tar a directory (which tars all files and subdirectories recursively unless you specify otherwise). The nice part about tarring a directory is that it is untarred as a directory rather than as individual files.

# tar cvzf foo.tgz cps100

will tar the directory cps100 (and its files/subdirectories) into a tar file named foo.tgz.

To see a tar file's table of contents use

# tar tzf foo.tgz

To extract the contents of a tar file use:

# tar xvzf foo.tgz

This untars/extracts (x) into the directory from which the command is invoked, and prints the files being extracted (v). If you want to untar into a specified directory, change into that directory and then use tar. For example, to untar into a directory named newdir.

mkdir newdir
cd newdir
tar xvzf ../foo.tgz

Now sometimes, we also face certain predictable errors which are caused due to different types of typo. In out first example, we can look at the script here.

TODAY=$(date "+%Y%m%d")
tar cjvf /tmp/twiki$ /var/www/html/twiki/data
cp /tmp/twiki$ /mnt/backups/twiki$
echo "all done"

In the above script, the run will give the error like.

"tar: error exit delayed from previous errors”

Reason : Tar dont like spaces, so fixing the above issue will be modifed as

TODAY=$(date "+%Y%m%d")

Thanks to tar reporting framework, it really helps us in understand certain basic mistakes.


Share This :

Free Linux Ebook to Download

Leave a Reply

Commenting Policy:
Promotion of your products ? Comment gets deleted.
All comments are subject to moderation.