Q:

Adding one variable into multiple parts of an alias? alias nd='mkdir $1' works but cannot add anything after, why?

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

Creates a directory called pdf in the current working directory.
Searches for all the files that contain pdf in the name within the working directory and all of its subdirectories.
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.

A:

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.

