Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question
add comment

2 Answers

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.

share|improve this answer
    
I'm not using multiple computers. –  Andrew Grimm Nov 10 '09 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. –  DaveParillo Nov 11 '09 at 2:06
add comment

I use this script for my own use:

#!/bin/ksh
#
# @(#)$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;;
esac

for d in ${*:-`pwd`}
do
    (
    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 .
    )
done

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.