How should I tar/gzip and delete files somewhat safely?

Background: We're running a little low on disk space on a Ubuntu computer (Hardy Heron), and I want to tar/gzip, verify that it worked, and then delete, some directories I'm no longer using. I'm planning on making a tar gzip for each directory I'm getting rid of. (The gzips will be to the hard drive, not a magnetic tape)

I came across a solution that ought to be safe but isn't at unix.com. It isn't safe because in current versions of tar, --remove-files can delete files even if the archive isn't created. (Acknowledged as a bug in this email)

I've also noticed a --compare option, but it says that it "ignores files in the file system that do not have corresponding members in the archive".

Also, does gzipping interfere with tar's ability to check against the existing files?

2 Answers 2


You can configure rsync to --delete-after transferring. It also includes many other options - i.e. you could avoid the tar / gzip step altogether. Use rsync to backup your stuff and delete the local copy. For some detailed instructions see scrounge.org and mikerubel.org and this.

  • I'm not using multiple computers. Nov 10, 2009 at 23:47
  • rsync does not require 2 computers. You can define the source & destination to whatever you want. A local rsync is perfectly legal. Nov 11, 2009 at 2:06

I use this script for my own use:

# @(#)$Id: save.sh,v 2.2 2007/09/01 23:41:23 jleffler Exp $"
# Compress directories to save space

case $(id | sed 's/^uid=\([0-9][0-9]*\)(.*/\1/') in
0)  echo "$(basename $0 .sh): cannot run safely as root!" 1>&2
    exit 1;;

for d in ${*:-`pwd`}
    set -e
    echo $d
    cd $d
    b=`basename $d`
    chmod 755 .
    [ ! -d Safe ] && mkdir Safe
    chmod 755 Safe
    # GNU Tar Only!
    find . -type f -print | grep -v '^./Safe' | tar -czf Safe/$b.tgz -T -
    chmod 444 Safe/$b.tgz
    chmod 555 Safe
    (set +e; rm -fr `pwd` 2>/dev/null ; exit 0)
    chmod 755 Safe
    mv Safe/* .
    rmdir Safe
    chmod 555 .

Note that 'root' can delete directories even when the owner can't - so it refuses to let 'root' run it. Try it out on a copy of your directory before risking it on anything important. I'm happy with it - you may prefer something else.

One day, I'll make it work with 'bzip2' or other compressors.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .