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.

1 Answers1

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.