cmd:tar & cmd:gzip 

Usage 

short form 
tar -cvzf tfile files
tar -xvzf tfile
traditional form 

And if your tar lacks support of the "z" flag, I strongly recommend using

gzip -d -c file.tar.gz | tar xvf -
tar cvf - files | gzip >file.tgz

instead of the two commands you suggest above --- both for performance and diskusage reasons.

for i in *gz ; do  gzip -d -c $i | tar xvf - ; done

Help 

Main operation mode:
 -t, --list              list the contents of an archive
 -x, --extract, --get    extract files from an archive
 -c, --create            create a new archive
 -d, --diff, --compare   find differences between archive and file system
 -r, --append            append files to the end of an archive
 -u, --update            only append files newer than copy in archive
 -A, --catenate          append tar files to an archive
Operation modifiers:
 -k, --keep-old-files       don't overwrite existing files when extracting
 -U, --unlink-first         remove each file prior to extracting over it
     --recursive-unlink     empty hierarchies prior to extracting directory
 -S, --sparse               handle sparse files efficiently
 -O, --to-stdout            extract files to standard output
Device selection and switching:
 -f, --file=ARCHIVE             use archive file or device ARCHIVE
Archive format selection:
 -z, --gzip, --ungzip               filter the archive through gzip
 -Z, --compress, --uncompress       filter the archive through compress
     --use-compress-program=PROG    filter through PROG (must accept -d)
Local file selection:
 -h, --dereference            dump instead the files symlinks point to
     --no-recursion           avoid descending automatically in directories
 -N, --newer=DATE             only store files newer than DATE
     --newer-mtime            compare date and time when data changed only
Informative output:
 -v, --verbose         verbosely list files processed
 -w, --interactive     ask for confirmation for every action

Help 

taken from the Debian User's Guide:

Large-Scale Copying

tar -cSpf - /usr/local | tar -xvSpf - -C /destination

The first tar command will archive the existing directory and pipe it to the second. The second command will unpack the archive into the location you specify with -C.

Update 

Once you've performed a full backup of your files with tar, you can perform an incremental backup to keep the backup archive up-to-date.

Suppose you performed the full backup on Feb. 20, 2000, and a week later you wanted to add to the archive all the files that were created or altered since then. Use tar's -u option for this purpose. For example:

tar -uvf /home/bryan/backup/full-backup.tar /home/bryan

This command adds all the files that aren't present within the archive or that were altered since the creation of the backup archive (full-backup.tar).

Backing up your data with tar? Fine, but forget compression 

It's possible to set up a system for backing up your data with the tar utility, but don't be tempted to use tar's built-in compression commands. Sure, you can create a tar archive that's compressed with gzip (to do so, type tar -czf followed by a space, the name of the archive you want to create, and the files you want to archive). But tar's update (-u) option won't work with compressed archives, so you won't be able to implement a backup system that makes periodic updates of only those files that have been created or changed since the last update.

Is this a horrendous flaw? Not really. Data security experts agree that compression shouldn't be used for backing up your data; compression increases the chance that the data will contain errors or become corrupted. For mission-critical data, it's not worth the risk.

Exclude files from tar 

Newsgroups: comp.unix.solaris,comp.unix.programmer,comp.unix.shell,comp.unix.questions

> Could someone out there please tell me how to exclude
> files and/or directories from that tar file? I have
> read the man page, but for some reason it does not seem
> to work. I know that you are supposed to make an exclude
> file and list the files/directories to be excluded in it?

The syntax for tar takes a bit of getting used to. You have to tell tar what to do by first giving it a string of one or more “function letters and function modifiers” and then the appropriate aguments. In this case `c' to _create_ an archive and `f' to specify that the archive is a _file_, then `X' which means you are supplying an _exclude-file_

tar cfX t.tar xf file1 file2 file3 *.log anotherfile

xf is the file which contains a listing of files and/or directories to exclude, 1 per line, with no extra spaces.

> One other thing, is it possible to use wildards in the
> exclusion, like not take a tar of */netscape/Cache*/*

No, AFAIK.

But you can of course use wildcards to produce the list. E.g.

ls *.tmp *~ > xf
ls -d .netscape/cache/[01]*/ >> xf

Manfred Bartz

documented on: 2000.06.10 Sat 21:37:47

How do I exclude a file from a tar ? 

>Hi, I am trying to setup a cron that will tar all files within a certain
>directory except one. I got it to tar all the files but can't figure out how
>to exclude one of them. Here is the line that I was told would do it but it
>includes whatever file I list as "somefile.txt".
>
>tar -cvf /home/httpd/hyperseek-dir.tar \ --exclude
>/home/httpd/cgi-bin/search/data/hyperseek/somefile.txt \
>/home/httpd/cgi-bin/search/data/hyperseek/*

The —exclude option takes the name of a file that *contains* the list of files to exclude. So if somefile.txt is the file that you don't want archived, do:

echo /home/httpd/cgi-bin/search/data/hyperseek/somefile.txt > /tmp/exclude-file
tar -cvf /home/httpd/hyperseek-dir.tar \ --exclude /tmp/exclude-file \
    /home/httpd/cgi-bin/search/data/hyperseek/*

Barry Margolin

documented on: 1999.11.16 Tue 10:10:52

GNU tar and hidden directories 

Newsgroups: comp.os.linux.misc

I think I've found the answer to my own question. It appears that I had extra whitespace in the form of newlines at the end of my exclude files. Apparently, tar interprets this as meaning that all files should be excluded. In any case, I took out the extra whitespace, and it's working fine now.

>I'm trying to use tar to back up the home directories of my users.  I
>use the following command
>
>tar -cvf user.tar -X user.exclude /home/user
>
>where user.exclude contains the following
>
>/home/user/.gnome/*
>/home/user/.kde/*
>
>etc.
>
>When I run the tar command, it doesn't back up anything in the user's
>home directory.

Backing up whole disk 

Newsgroups:  gmane.linux.debian.user
Date:        Mon, 28 Aug 2006 20:48:34 -0400
> I tried to backup my whole partition with tar:
>
>   tar -cvzf /mnt/.../cache11.tgz .
>
> but when testing the archive result with
>
>  tar -tzf /mnt/.../cache11.tgz
>
> It ends with the following error, without explaining what's wrong:
>
> gzip: stdin: unexpected end of file
> tar: Unexpected EOF in archive
> tar: Error is not recoverable: exiting now
>
> I thought about it and think the reason might be that I'm backing up to
> a vfat volume, which might have some serious limits on file size (but
> unfortunately, that's my only backup option now).
>
> If so, how can I backup my nearly 20G partition using vfat volume?
>
> rar can do multi-volume but can it understand all the special Unix
> files, symlink, hard links, named pipes, dev, etc?
>
> tar can do multi-volume too, but "You should only type 'y' after you
> have changed the tape; otherwise tar will write over the volume it just
> finished.".

I've done multivolume backups onto disk with tar. There's an option to specify the output file — you need to specify this multiple times, once for each output file, and then it sequences through them. Make sure you have enough of them, though.

One trouble, though. When using multiple files like this, and specifying a max output size per file, it will not allow compression. At least it didn't lst time I tried it.

hendrik

How to remove a file from a tar archive 

Newsgroups: comp.unix.questions
>remove. How do you remove an entry in a tar file? Thanks.

If you have GNU tar:

$ tar --help | grep -i delete
      --delete            delete from the archive (not on mag tapes!)
$ info --index-search delete tar
[snip]

If you're an emacs fan, another option is to use the "tar-mode" of emacs.

And if all else fails, there's always:

mkdir tmp$$; cd tmp$$
tar xf ../tarfile; rm badfile; tar cf ../tarfile .
cd ..; rm -rf tmp$$

Ken Pizzini

documented on: 2000.05.27 Sat 15:06:18

Expand archive into directories 

Date:          Thurs, Oct 23 2003 8:43 pm
Groups:        comp.os.linux.misc
>> I found that the 'tar' command has the following limitation:
>> Say my tar archive contains the following directory structure:
>> dir1/dir11
>> dir1/dir12
>> dir1/dir13
>> And, the dir1/dir12 is very big. Bigger than what the current volume can
>> hold. As a way to workaround, I created the above directory structure
>> manually, and the dir1/dir12 is actually a symbol link into a bigger
>> volume that can hold the content.
>> However, when I expend my tar archive into my created directory
>> structure, the symbol link dir1/dir12 get overwritten, and recreated as
>> a directory.
>> Is there any way for tar, or any other commands (e.g., cpio, rsync) to
>> preserve my created directory structure, expand/copy files into it, not
>> trying to recreate every directory even it is there?
>> This is only a simple example, for illustration purpose only. If tar can
>> do so, it would be greatly enhance its versatility.
> Are you preserving permissions when you pack and unpack the tar file?
> tar cpvf  filename.tar directory
> tar xpvf  filename.tar

hmm, no I didn't. I thought tar preserves permissions by default. Anyway, I gave it a try, but preserving permissions doesn't help:

$ find
./dir1
./dir1/dir11
./dir1/dir12
./dir1/dir12/aa
./dir1/dir13
$ tar cpvf test.tar dir1
dir1/
dir1/dir11/
dir1/dir12/
dir1/dir12/aa
dir1/dir13/

Now recreate dir1/dir12 as a symbol link!

rm -rf ./dir1/dir12
mkdir dirnew
ln -s ../dirnew ./dir1/dir12
$ ls -l ./dir1/dir12
lrwxrwxrwx    1 tong     tong   9 Oct 24 09:55 ./dir1/dir12 -> ../dirnew

Hmmm, yeah, it is there. Now, expand the tar.

$ tar xpvf test.tar
dir1/
dir1/dir11/
dir1/dir12/
dir1/dir12/aa
dir1/dir13/
$ ls -ld ./dir1/dir12
drwxrwx--x    2 tong     tong         4096 Oct 24 09:54 ./dir1/dir12

See, the previously manually created directory dir1/dir12 (a symbol link) is gone. Instead, it has changed back to a normal directory, which defeats what I was trying to do.

Any other suggestion? Thanks

Expand archive into directories 

NB, problem solved as of GNU tar 1.14:

$ mkdir -p dir1/dir11 dir1/dir12 dir1/dir13
$ touch dir1/dir12/aa
$ tar cvf test.tar dir1
dir1/
dir1/dir11/
dir1/dir12/
dir1/dir12/aa
dir1/dir13/
$ rm -rf ./dir1/dir12
$ mkdir dirnew
$ ln -s ../dirnew ./dir1/dir12
$ dir !$
lrwxrwxrwx  1 tong tong 9 03-26 16:10 ./dir1/dir12 -> ../dirnew/
$ tar xpvf test.tar
dir1/
dir1/dir11/
dir1/dir12/
dir1/dir12/aa
dir1/dir13/
$ ls -ld ./dir1/dir12
lrwxrwxrwx  1 tong tong 9 03-26 16:10 ./dir1/dir12 -> ../dirnew
# Now, dir link is preserved!
$ tar --version
tar (GNU tar) 1.14

Preserve file links 

$ echo abc > dir1/dir11/forg
$ ln -s ../dir11/forg dir1/dir13/flink
$ dir dir1/dir13/flink
lrwxrwxrwx  1 tong tong 13 03-26 16:27 dir1/dir13/flink -> ../dir11/forg
$ cat dir1/dir13/flink
abc
# Follow symbolic links
$ tar -h -cf t2.tar dir1/dir13
$ tar -tvf t2.tar
drwxrwx--x tong/tong         0 2006-03-26 16:27:14 dir1/dir13/
-rw-rw---- tong/tong         4 2006-03-26 16:26:30 dir1/dir13/flink
$ echo new > dir1/dir11/forg
$ tar xvf test.tar
dir1/dir13/
dir1/dir13/flink
$ dir dir1/dir13/flink
-rw-rw----  1 tong tong 4 03-26 16:26 dir1/dir13/flink
# file link is not preserved.

Preserve file links 

>> See, the previously manually created directory dir1/dir12 (a symbol link)
>> is gone. Instead, it has changed back to a normal directory, which
>> defeats what I was trying to do.
>> Any other suggestion? Thanks
> It works for me. You might try something more like this:
> Which would shown something on the order of this.

Ahh, David, based on what you showed me, I have an impression that you did not fully understand what I requested. I have no problem preserving symbol links in tar archives. I want to preserve previously manually created symbol link directory from overwriting by tar.

I am thinking that if you follow my previous post step by step, you'll get the same result. That's why I posted each step clearly. Try it, you'll understand what I meant.

If you still think it work for you by then, please post your steps clearly, your distro, and your version of tar.

Thanks.

PS. Also thanks to people who pointed out the -k switch. It is not exactly what I am looking for. It keep existing files. I do want to overwrite files, just need to preserve the directory structure.

Well, I think that it, tar can't do it. FYI, I've also invested in cpio, rsync, none of them can do what I wanted.

Preserve file links 

> If you still think it work for you by then, please post your steps
> clearly, your distro, and your version of tar.

I see what your doing. Your making a tarfile then changing the original directory structure. Then when you unpack the tarfile it is replacing the new directory structure with original that is in the tarfile. You want it to untar the archive into the new directory structure without making changes to the new directory structure.

That is something I've never tried to do.

David @netscape.net

Preserving symbol link dirs 

> > I found that the 'tar' command has the following limitation:
> >
> > Say my tar archive contains the following directory structure:
> >
> > dir1/dir11
> > dir1/dir12
> > dir1/dir13
> >
> > And, the dir1/dir12 is very big. Bigger than what the current volume can
> > hold. As a way to workaround, I created the above directory structure
> > manually, and the dir1/dir12 is actually a symbol link into a bigger
> > volume that can hold the content.
> >
> > However, when I expend my tar archive into my created directory
> > structure, the symbol link dir1/dir12 get overwritten, and recreated as
> > a directory.
> >
> > Is there any way for tar, or any other commands (e.g., cpio, rsync) to
> > preserve my created directory structure, expand/copy files into it, not
> > trying to recreate every directory even it is there?
> >
> > This is only a simple example, for illustration purpose only. If tar can
> > do so, it would be greatly enhance its versatility.
>
> Are you preserving permissions when you pack and unpack the tar file?
>
> tar cpvf  filename.tar directory
> tar xpvf  filename.tar

hmm, no I didn't. I thought tar preserves permissions by default. Anyway, I gave it a try, but preserving permissions doesn't help:

$ find
./dir1
./dir1/dir11
./dir1/dir12
./dir1/dir12/aa
./dir1/dir13
$ tar cpvf test.tar dir1
dir1/
dir1/dir11/
dir1/dir12/
dir1/dir12/aa
dir1/dir13/

Now recreate dir1/dir12 as a symbol link!

rm -rf ./dir1/dir12
mkdir dirnew
ln -s ../dirnew ./dir1/dir12
$ ls -l ./dir1/dir12
lrwxrwxrwx    1 tong     tong   9 Oct 24 09:55 ./dir1/dir12 -> ../dirnew

Hmmm, yeah, it is there. Now, expand the tar.

$ tar xpvf test.tar
dir1/
dir1/dir11/
dir1/dir12/
dir1/dir12/aa
dir1/dir13/
$ ls -ld ./dir1/dir12
drwxrwx--x    2 tong     tong         4096 Oct 24 09:54 ./dir1/dir12

See, the previously manually created directory dir1/dir12 (a symbol link) is gone. Instead, it has changed back to a normal directory, which defeats what I was trying to do.

Any other suggestion? Thanks

Preserving symbol link dirs 

rsync is not a solution either. Even using the most basic -r swith instead of full fledged -a swith will cause the symbol link directory be overwritten as a normal directory.

Preserving symbol link dirs 

cpio is also nok:

mkdir -p dir1/dir1{1,2,3}
touch ./dir1/dir12/aa
 find
tar cpvf test.tar dir1
rm -rf ./dir1
mkdir -p dirnew/dir1{1,2}
ln -s dirnew/dir11 dir1
ln -s ../../dirnew/dir12 ./dir1
lrwxrwxrwx    1 tong     tong  18 Oct 24 10:41 dir1/dir12 -> ../../dirnew/dir12/
lrwxrwxrwx    1 tong     tong  12 Oct 24 10:48 dir1 -> dirnew/dir11/
mkdir dirs
cd dirs
tar xvf ../test.tar
find dir1
$ find dir1 | cpio  -vpdm ../
cpio: ..//dir1 not created: newer or same age version exists
..//dir1/dir11
cpio: ..//dir1/dir12 not created: newer or same age version exists
..//dir1/dir12/aa
..//dir1/dir13
0 blocks
$ find dir1 | cpio  -vpdmu ../
..//dir1
..//dir1/dir11
..//dir1/dir12
..//dir1/dir12/aa
..//dir1/dir13
0 blocks
-rw-rw----    1 tong     tong            0 Oct 24 09:54 dir1/dir12/aa
drwxrwx--x    2 tong     tong         4096 Oct 24 10:46 dir1/dir12/
drwxrwx--x    5 tong     tong         4096 Oct 24 10:46 dir1/