//Tips tagged tar
Latest tips by RSS
Click here to subscribe
Follow Shell-Fu on Twitter
Click here to follow
Follow Shell-Fu on identi.ca
Click here to follow
From dotfiles.org; original author unknown:

###   Handy Extract Program

extract () {
    if [ -f $1 ] ; then
        case $1 in
            *.tar.bz2)   tar xvjf $1        ;;
            *.tar.gz)    tar xvzf $1     ;;
            *.bz2)       bunzip2 $1       ;;
            *.rar)       unrar x $1     ;;
            *.gz)        gunzip $1     ;;
            *.tar)       tar xvf $1        ;;
            *.tbz2)      tar xvjf $1      ;;
            *.tgz)       tar xvzf $1       ;;
            *.zip)       unzip $1     ;;
            *.Z)         uncompress $1  ;;
            *.7z)        7z x $1    ;;
            *)           echo "'$1' cannot be extracted via >extract<" ;;
        esac
    else
        echo "'$1' is not a valid file"
    fi
}


View Comments »



You can use ssh in conjunction with tar to pull an entire directory tree from a remote machine into your current directory:

ssh <username@sourcehost> tar cf - -C <sourcedir> . | tar xvf -

For example, let's say you have a "bsmith" account on a host called "apple". You want to copy those files into your "bobsmith" account on a host called "pear". You'd log into your "bobsmith@pear" account and type the following:

ssh bsmith@apple tar cf - -C /home/bsmith . | tar xvf -

This technique is useful when you have insufficient disk space on the source machine to make an intermediate tarball.


View Comments »

You can use find with the '-newer' flag in conjunction with tar to create a patch file:
tar -czvf patch-20070321.tar `find /path/to/project/ -newer /path/to/project/last-archive.tgz -print`


In this example 'last-archive.tgz' is the last tarball for the given project. -newer finds files newer that than last-archive.tgz, this way you can tar up only the changed files.


View Comments »

If you work on remote boxes a lot, you ll probably need a to backup a directory from there onto your machine sooner or later.. This can easilly be done using ssh & tar; all in 1 line of (script friendly) code:

ssh user@server "cd /foo && tar cvz * --exclude=bar*" > backup.$( date +%y%m%d ).tgz


Substitute user & server by the relevant info; /foo by the map you need and bar with the stuff you dont need; execute the command; login to the server when asked & Blamo! You ll have a nice backup.date.tgz file on your machine :)


View Comments »

Why not use cp or mv to move /usr/home to /storage/export/home? Weird things happen to hard and softlinks when you mv or cp. Try it and remember that a mv between different filesystems is actually a copy and delete.

Try this instead:
# tar -C /usr -cf - home | tar -C /storage/export -xvf -

Or, to copy to a remote machine:
# tar -C /usr -cf - home | ssh user@somemachine tar -C /storage/export -xvf -

You may want to add the -z switch to the tar commands. It will add compression but it depends on the type of data and your connection speed if it really improves transfer speeds.


View Comments »

If you want to select specifically the files to add to an archive you can pipe the output from find (or any command that gives a list of files) to cpio:

$ find ./dir/ | cpio -o --format=tar > archive.tar
or
$ find ./dir/ | cpio -o --format=tar -F test.tar


View Comments »

GNU tar comes with native support for gzip, bzip2, and compress (adaptive LZ, LZW). However, many other useful compression algorithms exist, but most implementations of them don't support all the file system metadata that tar does. There are two general methods to using tar with arbitrary compression programs: via an option in tar itself and via piping. The first:
tar -cf foo.tar.ext --use-compress-program generic_compression_program file1 [file2] [file3] [...]

Note that this method is not very configurable and only works with programs that compress by default and will decompress files when invoked with the "-d" option.

The second method:
tar -c file1 [file2] [file3] [...] |generic_compression_program >archive.tar.ext

This method is more versatile, but it requires a bit more shell voo-doo to work (and may not require use of stdout redirection). For instance, to make a .tar.7z archive:
tar -c list of files and directories to compress |7z a -si outfile.tar.7z

To uncompress said archive:
7z e -so infile.tar.7z |tar -x


View Comments »

tar.gz
for file in `ls *.tar.gz`;do tar -xvf $file;done


tar.bz2
for file in `ls *.tar.bz2`;do tar -xvjf $file;done


View Comments »

nice ssh username@remoteservername "tar cjf - -C /from/basedir/ dirtocopy" | tar xjvf - -C /to/dir/ ; sleep 120 ; shutdown -P now


Copy the remote directory dirtocopy from the remote server to dir of local machine. Transfer the contents using bzip2 compression. When it's done (even if it fails) wait 120 seconds and power off the machine.


View Comments »

The tar command can be used to make a quick incremental backup as follows:

tar -Pvuf backup.tar /home/user/username

This will backup the directory /home/user/username. If any changes are made to the files in this directory and the above command is run again, the files will be added/changed in the tar file. Deleted files are not removed from the archive.

An alternative method is to use find to get a list of files newer than the backup and add these to the tar using the '-r' (append) option.

find /home/user/username ! -type d -newer backup.tar -exec tar -rvf backup.tar {} \;


View Comments »

CREATE THE TARBALL

tar cf - sourceFiles | gzip -c > fileToCreate.tgz


EXTRACTION

zip -d -c fileToExtract.tgz | tar xf -


View Comments »




Home Latest Browse Top 25 Random Hall Of Fame Contact Submit