0

I try to change the rm command into a command that give me backup of the file and remove the file after.

function move_to_trash () {
        unset rm;
        cp -v "$@" "/home/`whoami`/backup/" &&  rm "$@";
}


alias rm='move_to_trash'

Output:

'file1' -> '/home/dorchester/backup/file1'
'file1' -> '/home/dorchester/backup/file1'
'file1' -> '/home/dorchester/backup/file1'
'file1' -> '/home/dorchester/backup/file1'
'file1' -> '/home/dorchester/backup/file1'
'file1' -> '/home/dorchester/backup/file1'
'file1' -> '/home/dorchester/backup/file1'
'file1' -> '/home/dorchester/backup/file1'

The cp command keep running without starting rm "$@" command, which is something really strange. This seems to be a infinite loop, I would like to know how can this loop happen. Also, I'm aware of existence solution that turn cp command into move to trash command such rmtrash, though I prefer bash scripting method.

2
  • unset rm unsets a variable named rm. It doesn't affect aliases.
    – muru
    Aug 19, 2019 at 4:34
  • thanks for pointing out. I added "command" prior to rm command. It works now. Aug 19, 2019 at 4:37

1 Answer 1

0

Note: unset command doesn't remove aliases.

function move_to_trash () {
        cp -vr "$@" "/home/`whoami`/backup/" && command rm -rf "$@";
}

alias rm='move_to_trash'

This code work for me if I want to remove a file or directory.

Not the answer you're looking for? Browse other questions tagged or ask your own question.