7

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?

  • 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 5:48
  • Investigate 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 5:50
  • 7
    Workaround: alias myrm='mv -t /home/user/Trash' – Cyrus Sep 22 '17 at 5:52
  • 1
    @Cyrus It's not a workaround, it's the right way. – Kamil Maciorowski Sep 22 '17 at 5:56
  • Thanks @Cyrus and others for comments. The alias myrm='mv -t /home/user/Trash/} solved the problem. – phenomenon Sep 22 '17 at 6:10
17

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.

  • 9
    I think the $ is from his shell, normally all the lines in a terminal start with $ to say it's a non-root shell – Ferrybig Sep 22 '17 at 16:03
  • Yes, that could be what he means. – G-Man Sep 22 '17 at 17:57
  • @G-Man for transparent positions, I flag your action and go usefully Thank you! – It Wasn't Me May 12 at 6:37
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

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/
}
  • The answer is in the links, I don't like to copy other people's work – chloesoe Sep 22 '17 at 6: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 6:48
  • added the solution and left the most fitting link, the other link was stackoverflow.com/q/4438147/7311363 qft – chloesoe Sep 22 '17 at 6: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, ...).

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.

  • 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 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 6: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, mv "$@" /home/user/Trash is better).  The second one (mv -t /home/user/Trash/) was already given in my answer and Cyrus’s comment before that. – G-Man Oct 12 '17 at 23:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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