Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Emulating rm command

Status
Not open for further replies.

Actionboy

Technical User
May 21, 2003
3
EG
Can anyone give me some code for writing a safe version of rm that will move stuff to a specified directory?

The first bit is easy I know but sorting out the options and checking whether the files/directories exists is causing me problems. Help! This is quite urgent. I'm at a unix seminar & trying to get a job! Thanks.
 
Since you ultimately have to cope with the problem of deleting the same file twice, then I would do this

- create a subdirectory with the name yyyymmddhhmmss, which is the date-time string at the moment the command is invoked.
- move each file to be deleted into a subdir of the above, which contains the full path of the original file.

So if you want to delete
/path/to/my/file.txt

You would move it to
safe/yyyymmddhhmmss/path/to/my/file.txt

That way, it won't conflict with trying to delete another file.txt in another directory, and trying to delete path/to/my/file.txt sometime later will be ok, since you'll have a different yyyymmddhhmmss

With that, you can always tell when you deleted something, and where it was originally.





 
Thanks for replying, but what is confusing me is how to handle it if someone used options with the command, like saferm -iv or something, what kind of loop contruct would I have to use to check for each option? Also, how do I check to see if the file actually exists?
 
well file exists can be done with :
Code:
if [ -L $filename ] ; then
    echo file is a symbolic link
elif [ -d $filename ] ; then
    echo file is a directory
elif [ -f $filename ] ; then
    echo file is a regular file
fi

for handling parameters : 'more `which filep`' will give an example of parameters, not quite as clever as you want, but i'm sure you can work on it.
 
btw for safeties sake, to keep permissions on the files when rm'ing them use something like the tar command before deleting

tar cvf ~/saferm/`date +"%Y%m%d%H%M%S"`.saferm $filename

if you have gtar installed, you could do a zcvf on the file to save space.

 
and just if you're interested, in the general unix discussion there was a thread thread80-492846 that did a recycle-bin type thing
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top