Suse & OpenSuse : Tips, Tricks, Tutorials, How Tos and Troubleshooting

Short and quick tutorial for the command “tar”

We have recently posted about tarball and the use of tar coupled with compression programs to create it.  Here, we will go through three (3) short tutorial on using tar.

To compress a list of files

To compress (the c in the options) a list of files into a specific file (the f in the options) whilst having the tar program to provide progress of it (verbose mode, the v in the option), we use


# tar cvf <tarfile>.tar <list of files>

Leading / will be removed from the archived listing as this will prevent accidental replacement of system files if the file is uncompressed by root.

To uncompress a file

Similarly, we use the same command options where we replace the c with x (for eXtract).

# tar xvf tarfile.tar

This will extract all the files from the archive and recreate the directories and files in the location where the user is in.  Note that, it is good practice to create a new directory, change directory into it before you extract from a tar archive.  This is to prevent overwriting of existing files, unless that is the intention.

To view content of a file

Sometimes, we just want to check if we have a specific file in an archive.  For example, if we are accessing a tape drive (at /dev/st0), we can list (the t in the option) the content using

# tar tvf /dev/st0

More often than not, the listing will be too long for us.  We can pipe the output to grep.

# tar tvf /dev/st0 | grep <string-to-match>

Hope this is short enough.  Leave a comment if you want to ask questions pertaining to tar.

RSS feed | Trackback URI

Comments »

No comments yet.

Name (required)
E-mail (required - never shown publicly)
URI
Your Comment (smaller size | larger size)
You may use <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> in your comment.