8

I am a newbie. I set an alias in .bashrc file as follow.

alias myrm='mv /home/user/Trash/*'

The purpose is that when I use myrm comment, for example $myrm foo, the file "foo" has to be moved to the Trash folder which is in my home folder (/home/user/Trash).

Then I did

$source ~/.bashrc

After this, when I try to use myrm by typing $myrm foo, I get the following error message.

mv: cannot stat ‘/home/user/Trash/*’: No such file or directory

How this problem can be solved?

phenomenon
  • 215
  • 4
  • 7

6 Answers6

18

First of all, you don’t use $ when you call an alias.  $ is for expanding a variable (and a few other things that aren’t particularly relevant to this question).

But, secondly, aliases do work a little like variables, in the sense that they (at the risk of oversimplifying a little) just expand to a bunch of words.  You say you want to do myrm foo, but that would expand to mv /home/user/Trash/* foo, which doesn’t make sense.

A simple solution would be to define the alias to be mv -t /home/user/Trash, which would work because mv supports the

mv -t destination_dir  file
syntax as an alternative to the

mv filedestination_dir
syntax.

But you can get greater flexibility with a shell function.  These combine the flexibility of scripts with the (low) overhead of aliases.  For example,

myrm() { mv "$@" /home/user/Trash; }

will cause myrm foo to be interpreted as mv foo /home/user/Trash.

8

Probably easier solution is to use trash-cli package. Then you can just do alias myrm=trash and then trash foo to accomplish what you want to. Except that foo will now go to ~/. local/share/Trash

deshmukh
  • 303
4

The problem is, with mv you have to use it like mv source destination.

With your alias it's vice-versa mv destination source.

Also you don't need the asterisk * at the end, because it works with the destination as a folder. Make sure your folder /home/user/Trash exists with mkdir /home/user/Trash.

To solve your alias idea, I would recommend you to have a look at this stackoverflow questions:

This will lead to that solution; please add this to your ~/.bashrc and do a source ~/.bashrc after adding:

myrm() {
 /bin/mv "$@" /home/user/Trash/
}
chloesoe
  • 716
2

As stated in comments, the mv syntax is different you can do :

myrm(){ mv "$@" $HOME/Trash/; }

that can handle multiple files as argument

However this simple alias does not handle file name collision, as well as metadata (from where it has been removed, ...).

For a more complete solution, you can simply use the package trash-cli which is pretty good and come with few tools (eg. empty files thar are in the trash for X days, ...).

vera
  • 1,520
1

There is another useful alias you could consider if you have gvfs-bin package installed in your host:

alias myrm='gvfs-trash`

It moves your files to the bin. Also, the proper files are written in your $TRASH folder (such as info/, files/ ...). So you don't have to worry for that. Example : Going in the bin and clicking restore will perfectly work.

That's redundant with trash-cli but your host may have only one of them already installed, so if you don't have trash-cli, give it a try.

PTRK
  • 123
0

You need to do

mv "$1" /home/user/Trash/
or
mv -t /home/user/Trash/
instead of
mv /home/user/Trash/
Manpage here