0

I am trying to create an alias for the rm command in the /root/.bashrc file on a VirtualBox Redhat VM (running RHEL 9). I cannot get it to work properly. This is an excerpt of my .bashrc file:

alias rm='printf "rm: cannot remove %s: Permission denied\n" "$@" &&  w >> /tmp/logfile_20090204_001.log'
sudo() {
    if [[ "$1" == "rm" ]]; then
        for file in "${@:2}"; do
            printf "rm: cannot remove %s: Permission denied\n" "$file"
        done
        w >> /tmp/logfile_20190204_002.log
    else
        command sudo "$@"
    fi
}

I reload it with . /root/.bashrc. The result I get when using rm is rm: cannot remove : Permission denied (no file name). I have tried replacing printf with echo, switching the single and double quotes and modifying the bash_aliases file instead of the .bashrc file, and nothing seems to work. How can I get this alias to print my file name (with and without sudo)?

Thank you in advance for your help!

0

1 Answer 1

0

Aliases are not functions, they don't take arguments. They're just a simple replacement.

You'll have to rewrite your alias as a bash function, e.g.:

rm() {
  printf "rm: cannot remove %s: Permission denied\n" "$@" && 
    w >> /tmp/logfile_20090204_001.log'
}

PS: what does w do? is it a program, alias, or another function?


Also, wrapping rm with an alias or a function is easily over-ridden. All the user has to do is run \rm or command rm instead of rm and they're running the original rm binary rather than your alias/function.

If you really need to prevent users from deleting files, you'll have to do it with permissions (e.g. no write permission on the directory) or with ACLs.

3
  • I would assume w is this command. Commented Jul 10 at 14:25
  • could be. i assumed that it was custom alias or something - and that particularw was long-forgotten and only used by old people like me. Commented Jul 10 at 14:56
  • Thanks for your help! And yes, w is what Admineral described. Commented Jul 11 at 7:41

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.