4

I would like to create an alias for the move command -

trash='mv <some files> /home/$USER/.local/share/Trash/files'

How do I make this work?

I want the destination to always be the same. But I want to be able to pass the files to be moved.

  • 4
    There is already a command-line interface to the trash: in 18.04 it's gio trash (in earlier versions of Ubuntu, gvfs-trash) i.e. you can just type gio trash <some files>. If that's really too long then you can alias it alias trash='gio trash'. – steeldriver Apr 21 at 13:27
  • 1
    @steeldriver I thought of voting to close this as a duplicate of Can I pass arguments to an alias command? but I think your comment is really the best answer to the question here (and so it's not a duplicate of that). Would you consider posting it as an answer? – Eliah Kagan Aug 15 at 12:21
10

Use function instead of alias, defined in .bashrc

nano ~/.bashrc 

# put inside .bashrc:
trash() { 
  for item in "$@" ; do
    echo "Trashing: $item" 
    mv "$item" /home/$USER/.local/share/Trash/files 
  done
}

Then in shell prompt you can use:

$ trash file1 file2
  • thanks! This worked perfectly. – charsi Apr 21 at 10:16
  • Don't forget to close shell and open again to make this work – LeonidMew Apr 21 at 10:16
  • source ~/.bashrc works too – charsi Apr 21 at 10:17
  • 5
    You don't need a loop: trash() { mv "$@" destination; } – glenn jackman Apr 21 at 14:34
5

You can only append arguments to an alias. Fortunately, mv allows you to do this, with the -t option

alias trash='mv -t ~/.local/share/Trash/files'
0

You can also create a bash script and run that script with an alias.

trash.sh:

#!/bin/sh

for arg in $*; do
    mv $arg /home/$USER/.local/share/Trash/files
done

exit 0

.bashrc:

alias trash="/path/to/script/trash.sh"

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.