Skip to content
Advertisement

Run script when a command is executed irrespective of the command’s arguments

rm -rf
rm -r
rm -f
rm

When any of these commands are run I want them to run a certain script.

Something like alias ?='some_script.sh'

(the question mark in this case means the rm command with any arguments.)

How can this be done? I don’t HAVE to use aliases, anything that works is fine.

Advertisement

Answer

Don’t use an alias; define a function:

rm () {
    some_script.sh
    command rm "$@"
}

The standard disclaimer when modifying the behavior of rm in any way applies. Don’t do this is some_script.sh is intended to be a safety net of some kind. You may become reliant on it, and be unpleasantly surprised if you run rm on a machine without this safety net installed.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement