6

I have the alias

alias moveit='mkdir $1 && find -name "*$1*" -type f -exec mv -t ~/$1 {} +'

But I'm not quite sure what I'm doing wrong.
Any help would be much appreciated.

I'm trying to make an alias where typing moveit pdf

  1. Creates a directory called pdf in the current working directory.
  2. Searches for all the files that contain pdf in the name within the working directory and all of its subdirectories.
  3. Moves those found files into the new folder called pdf.

I know that pdf will return other files that aren't pdfs. I'm essentially trying to create an automatic sorting script.

I have files called:

greatfile[pdf].rar
greatfile[txt].rar
greatfile[rtf].zip
badfile[pdf].zip
badfile[txt].zip
badfile[rtf].rar
okayfile[pdf].tar.bz
okayfile[txt].zip
okayfile[rtf].rar

And I want to sort these files into folders named pdf, txt and rtf, all in one script, or at least one at a time for now.

1

1 Answer 1

1

To make this work, you have to use a temporary function:

alias moveit='f() { mkdir "$1" && find -name "*$1*" -type f -exec mv -t "$1" {} +; unset -f f; }; f'

It works as expected. For example moveit pdf moves all files that contain the string pdf to a newly created directory pdf.

The definition part f() { mkdir "$1" && find -name "*$1*" -type f -exec mv -t "$1" {} +; unset -f f; }; defines the function and the last part f calls this function.
The last part of the function definition unset -f f;

removes the function definition as the alias is executed so it doesn't hang around afterwards.

For further information, have a look at this "How to pass parameters to an alias?" question.

5
  • Although this will work, it's hard to imagine a real reason to do it. A function can simply be used instead of an alias. If for some reason one still needs or wants an alias, one can just make an alias to that function (though even that is rarely if ever necessary). Aug 24, 2019 at 20:57
  • Thankyou zx485! Great answer! I will have to learn how to do functions obviously, so thank you for pointing me in the right direction Eliah Kagan.
    – serge
    Aug 25, 2019 at 1:58
  • A situation that this would be useful is that if I were using someone else's computer only for a short time, it would be convenient to be able to just add my own .bash_aliases file with all the commands I need, without having to add any scripts to their computer. Probably not a very common situation, but it's nice to know how to do it.
    – serge
    Aug 25, 2019 at 2:22
  • You could also add all of your packages with an alias, and remove all of your packages with an alias. If I had to reinstall ubuntu, I could just add my .bash_aliases file and away I go. No other files needed.
    – serge
    Aug 25, 2019 at 2:32
  • 2
    @serge You can do all this with functions just as easily (and, in this case, much, much more easily). Functions can even be defined directly in .bash_aliases, if you want. It would be confusing to put a function definition in there, but still less confusing than putting an alias in there that is really just a wrapper for a function, where no alias is needed. Functions are most often defined in .bashrc or some file sourced by .bashrc. (.bash_aliases is sourced in .bashrc; you can source other files in it, too.) Aug 25, 2019 at 2:34

Not the answer you're looking for? Browse other questions tagged .