[TAG] deleted file recovery

Ben Okopnik ben at linuxgazette.net
Fri Oct 17 17:06:32 MSD 2008


On Fri, Oct 17, 2008 at 12:58:07AM +0100, Francis Daly wrote:
> On Thu, Oct 16, 2008 at 06:52:27PM -0400, Ben Okopnik wrote:
> 
> > Here's something that should address both issues (I have not done any
> > significant testing on this, and would certainly welcome comments):
> 
> Even minor nitpicky ones? :-)

Sure. As long as they're at least interesting. :)
 
> > ``
> > #!/bin/bash
> > # Created by Ben Okopnik on Thu Oct 16 18:17:50 EDT 2008
> > 
> > [ -z "$1" ] && { printf "Usage: ${0##*/} <file_to_safely_delete>\n"; exit; }
> > 
> > ################ User-defined variables ###########################
> > savedir=${HOME}/.Trash	# Directory in which files are backed up  #
> > keeptime=30				# Delete backups after this many days     #
> > ###################################################################
> > 
> > # Create $savedir if it doesn't exist
> > [ -d "$savedir" ] || mkdir "$savedir"
> 
> I tend to "mkdir -p". Presumably they have similar failure modes if
> $savedir exists as a non-directory. Possibly I'm sacrificing pre-POSIX
> portability for less typing.

[blink] In what case, pray tell, would *${HOME}* not exist? I suppose
that '-p' would do no harm - frankly, using it in scripts is my own
first inclination - but I couldn't see any situation in which it would
be applicable. Anyone who actually edits the script and changes the
savedir to something bizarre would also theoretically know enough to
create it - or would suffer the consequences.
 
> > # Generate unique, time-stamped filename
> > savename="${1##*/}:`/bin/date '+%s%N'`"
> > 
> > # Delete file *only* if hardlink creation succeeded
> > /bin/ln "$1" "$savedir/$savename" && /bin/rm "$1"
> 
> I'm not sure I see the difference between this and just "mv" -- apart
> from the "cross-filesystem" thing. Maybe that's exactly it. 

Try it on a, say, 10GB file. You'll see the difference immediately.
(Hint: it takes a while to "mv" something that size.)

> Can't use
> this script to delete from a non-$HOME filesystem (on typical
> implementations).

You have a point; copy-and-delete would cover more stuff, including
other filesystems. That's a pretty easy change.
 
> > # Delete any files in $savedir that are older than $keeptime
> > /usr/bin/find $savedir -mtime +$keeptime -delete
> 
> ctime is more likely to be useful than mtime here, I think.

I'd considered it when I wrote it, but still can't see a reason that it
would be more useful. Reasoning, please?


-- 
* Ben Okopnik * Editor-in-Chief, Linux Gazette * http://LinuxGazette.NET *




More information about the TAG mailing list