Monday, September 06, 2010

tar Magic

Tar is commonly used for packaging files on Linux. Tar uses many command-line options like following:

-f    To use a tarfile
-c   To create a new tarfile
-x   To extract files from a tarfile.

You also can compress the resulting tarfile via two methods. 
-j    To use bzip2
-z    To use gzip

You can tar up a directory and all of its subdirectories by using:
tar cf archive.tar dir

Then, extract it in another directory with:
tar xf archive.tar

When creating a tarfile, you can assign a volume name with the option -V . You can move an entire directory structure with tar by executing:
tar cf - dir1 | (cd dir2; tar xf -)

You can move an entire directory structure over the network by executing:
tar cf - dir1 | ssh remote_host "( cd /path/to/dir2; tar xf - )"

If you want to get a dump of your current filesystem to a secondary hard drive, use (as root):
tar -cvzf /dev/hdd /

If you are writing your tarfile to a device that is too small, you can tell tar to do a multivolume archive with the -M option.

You can back up your home directory to a series of floppy disks by executing:
tar -cvMf /dev/fd0 $HOME

If you are doing backups, you may want to preserve the file permissions. You can do this with the -p option. If you have symlinked files on your filesystem, you can dereference the symlinks with the -h option. This tells tar actually to dump the file that the symlink points to, not just the symlink.

Along the same lines, if you have several filesystems mounted, you can tell tar to stick to only one filesystem with the option -l.

No comments:

Post a Comment