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
  • 2
  • 7
  • 2
    Syntax is `mv SOURCE DESTINATION` and not `mv DESTINATION SOURCE`. See `man mv` and `help alias`. Use a function. See `help function`. – Cyrus Sep 22 '17 at 05:48
  • Investigate [positional parameters](https://www.gnu.org/software/bash/manual/html_node/Positional-Parameters.html#Positional-Parameters). Your script wants a `$1` between the mv and the destination folder to take the 'foo' you provide when you run the script. – mcalex Sep 22 '17 at 05:50
  • 7
    Workaround: `alias myrm='mv -t /home/user/Trash'` – Cyrus Sep 22 '17 at 05:52
  • 1
    @Cyrus It's not a workaround, it's *the right way*. – Kamil Maciorowski Sep 22 '17 at 05:56
  • Thanks @Cyrus and others for comments. The alias myrm='mv -t /home/user/Trash/} solved the problem. – phenomenon Sep 22 '17 at 06:10
  • Moving to ~/Trash won't allow you to recover the file using the desktop applications. See @deshmukh's solution for How To Not Reinvent The Wheel. – xenoid Sep 22 '17 at 07:19

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.

7

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
  • 293
  • 1
  • 3
  • 7
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
  • 3
  • 10
  • The answer is in the links, I don't like to copy other people's work – chloesoe Sep 22 '17 at 06:31
  • I saw this answer when it was the only one here. If it had explicit solution, I would have upvoted it right away. I hesitated but upvoted eventually because the links are useful, they give insight and stay within Stack Exchange. Main problem with link-only answers is the linked target may disappear, but I expect if these targets suddenly disappear from Internet, so will this entire question. – Kamil Maciorowski Sep 22 '17 at 06:48
  • added the solution and left the most fitting link, the other link was https://stackoverflow.com/q/4438147/7311363 qft – chloesoe Sep 22 '17 at 06:54
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,230
  • 8
  • 10
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
  • 113
  • 1
  • 6
  • Is this any better or worse than the `trash-cli` package? This requires installing gvfs-bin which is also not installed on my system. I had never encountered gvfs before and just read up on it a bit at https://en.wikipedia.org/wiki/GVfs . I'm still not sure what its raison d'être is, but that's another question. – Joe Sep 25 '17 at 22:29
  • 1
    On all the system (Ubuntu mostly) I used it was already installed, that why I proposed it; But since you're telling me it isn't mandatory, `trash-cli` may not be any better or worse. I will update my response accordingly – PTRK Sep 26 '17 at 06:54
0

You need to do

mv "$1" /home/user/Trash/
or
mv -t /home/user/Trash/
instead of
mv /home/user/Trash/
Manpage here
  • The first one (`mv "$1" /home/user/Trash/`) won’t work in an alias (which is what the question is asking about) and is of limited value in a function (as I said in [my answer](https://superuser.com/q/1252676/354511#1252685), ``mv "$@" /home/user/Trash`` is better).  The second one (`mv -t /home/user/Trash/`) was already given in my answer and [Cyrus’s comment](https://superuser.com/q/1252676/354511#comment1840104_1252676) before that. – G-Man Says 'Reinstate Monica' Oct 12 '17 at 23:45