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.

Zanna
  • 72,312
serge
  • 63

1 Answers1

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.

zx485
  • 2,865