rm non-permanent delete


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users rm non-permanent delete
# 1  
Old 12-09-2010
rm non-permanent delete

I read this article as a way to do a non-permanent of something.

I saw 2 problems. The first that my rm is located at /bin/rm. I would assume I would change the location to /bin/rm. The second my rm is a executable file and not a text file. So will replacing my rm file with the shellscript below work? I would think they would need to be the same type of files.

Quote:
As far as how to make stuff go to the trash instead of deleted, run this as root:
Code:
mv /usr/bin/rm /usr/bin/rm.bak

Quote:
Now copy this script and save it as /usr/bin/rm:
Code:
#!/bin/bash

mkdir ~/.Trash &> /dev/null

while [ ! -z "$1" ]; do
    mv "$1" ~/.Trash/
    shift
done

Quote:
This will, instead of deleting files, move them to the .Trash directory in your home directory, and you can delete them for real later.
Linux Forums - View Single Post - is it not possible to recover files deleted using rm

Has anyone ever done something like this? Would doing this as a alias be easier. One my friends I talked to said it would be easier and safer to do this as a alias. What does everyone think?
# 2  
Old 12-09-2010
No. No.
Do not replace anything in /bin. Plus /bin is a link to /usr/bin. You will break zillions of things you had no idea about. So do not do that. The system could fail on shutdown or reboot due to a problem in your code. Then you have a dead box. Or a doorstop if you need one.

Create an alias in /etc/profile
Code:
if [ $(group -g) -gt 99 ] ; then
  alias rm=/usr/local/bin/rm
fi

Put your code in /usr/local/bin, this will prevent system users from using the altered rm.
/usr/local/bin is meant for this kind of thing - non-standard applications available to all users. If you put other command utilities into /usr/local/bin, add it that directory to your PATH variable.
# 3  
Old 12-16-2010
Better yet, don't change the meaning of "rm" at all... "rm" should do what "rm" does: remove files from the directory tree structure. If you want a user-friendly "trash" command you should call it something else. Benefits:

  • You won't try to delete something and wind up merely trashing it instead
  • You won't try to trash something and wind up permanently deleting it instead (if PATH is changed to not include your script, etc.)
  • No risk of breaking existing scripts, etc. that use rm to mean what it's supposed to mean (and possibly expect the rm command to provide various options that your script doesn't...)
  • running "rm" will actually recover usable disk space, as it should.
  • "rm" remains a relatively fast operation (since you're just unlinking data, rather than possibly moving it from one filesystem to another.)
I mentioned various "rm" options your script doesn't handle: basically, that would be "--recursive" (-r) and "--force" (-f). You could handle these in the "trash" script but things quickly get more complicated. (You can "force" move, but to move recursively across filesystems you have to copy recursively and then remove the source... And then you have to worry about whether you're correctly handling cases where the copy succeeded but the remove failed for one or more files...)

IIRC the trash system on Mac OS X avoids the cross-filesystem issue by creating a trash bin on each filesystem, so that would be another possibility. But it seems to me that to do all this right is not entirely a trivial problem.
# 4  
Old 12-17-2010
Yes, replacing rm is a severely bad idea. Lots of things use rm and unless you perfectly emulate every possible option rm has you'll probably break something important. You might also get your fancy undelete folder rapidly filling with thousands of useless temporary files that really should have been rm-ed.

What I've seen done sometimes is:

Code:
alias "rm=myfancybackupscript.sh"

...in a user's profile.

This has the advantage that myfancybackupscript.sh will be only ever be used in interactive shells. Never in scripts, never in cron jobs, never in system utilities, never in anything built around the real rm -- it only gets run when a human types rm .... It also means no broad changes are made to your system. It also means people can still get the real RM if they want it, by calling it as /bin/rm.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Solaris

how to make IP address permanent.

Greetings, I am using solaris10 x86 OS. I configured IP address using the command. >ifconfig e1000g0 plumb >ifconfig e1000g0 200.200.0.1 up How to make this configured IP as permanent.. to solaris os. (2 Replies)
Discussion started by: bhargav90
2 Replies

2. UNIX for Dummies Questions & Answers

Permanent Dynamic “View”

Is there a way to make a permanent pseudo-file, whose contents may dynamically change? I'm thinking of something like an SQL view here. I've been trying to do this with pipes, but I haven't been able to crack it. For example, I have two files, “half1” and “half2”, which are subject to change... (3 Replies)
Discussion started by: devoll
3 Replies

3. UNIX for Dummies Questions & Answers

Inode: is it both unique and 'permanent'?

I try to understand the meaning of an inode. I wonder whether an inode is unique (I'm pretty sure it is) and whether it remains the same inode regardless of whatever happens to the file, dir or whatever? I read somewhere that an inode stores info about the file, size... so changing the... (4 Replies)
Discussion started by: dakke
4 Replies

4. UNIX for Dummies Questions & Answers

permanent change in file

Hi! i want to replace ; by ok in a file as below test1(filename) containt:- Hi i am kaushlesh; i am new to Unix. i want permanent change in the file like below:- Hi i am kaushlesh ok i am new to unix How i will complite this..? (2 Replies)
Discussion started by: kaushelsh168
2 Replies

5. UNIX for Dummies Questions & Answers

Making an alias permanent

Hi mates, I want to make an alias permanent for a KShell, does someone knows how to do that? Thanks! (4 Replies)
Discussion started by: agasamapetilon
4 Replies

6. Solaris

Permanent changes to PATH

Hi guys, I'm running Solars 8 on a V100 server at home for testing. If I switch user to root and do: # echo $PATH This is the output: /usr/sbin:/usr/bin I'm using rsync over ssh and need to add /usr/local/bin and /user/local/sbin. I do this by running the line: #... (3 Replies)
Discussion started by: Stin
3 Replies

7. UNIX for Dummies Questions & Answers

Permanent Alias

On AIX 5.2 as root, installed Seamonkey and have to type #/seakey/seamonkey/seamonkey to get it to run, which it does okay. To set up a permanent alias, I did the following (1) In a text editor alias seamk='/seakey/seamonkey/seamonkey' and saved it to /home/alias_file (2) In a text editor... (7 Replies)
Discussion started by: farl
7 Replies

8. Solaris

permanent route

How do I make a route permanent, other than default route on a Solaris server? (1 Reply)
Discussion started by: jontom
1 Replies

9. Shell Programming and Scripting

How do I set permanent setenv !!!

Hello, I just want to know ow I can set permanent pathes or whatever using setenv command. I'm using c shell . regards, me (1 Reply)
Discussion started by: geoquest
1 Replies

10. IP Networking

Permanent ip routing

I am trying to add a permanent route on my server, but whenever i reboot it dissapears. Please does anyone know the correct command to use. route add XXX.XXX.XXX.XXX DDD.DDD.DDD.DDD the above is what i have done. ednut:) using IRIX SGI software. (2 Replies)
Discussion started by: Ednut
2 Replies
Login or Register to Ask a Question