Move files to directories based on extension

Two ways:

  1. find . -name '*mp3' -or -name '*ogg' -print | xargs -J% mv % ../../Music
  2. find . -name '*mp3' -or -name '*ogg' -exec mv {} ../Music \;

The first uses a pipe and may run out of argument space; while the second may use too many forks and be slower. But, both will work.


Another way is:

mv -v {*.mp3,*.ogg,*.wav} ../Music
mv -v {*.mp4,*.flv} ../Videos

PS: option -v shows what is going on (verbose).


There is no trigger for when a file is added to a directory. If the file is uploaded via a webpage, you might be able to make the webpage do it.

You can put a script in crontab to do this, on unix machines (or task schedular in windows). Google crontab for a how-to.

As for combining your commands, use the following:

mv *.mp3 *.ogg ../../Music

You can include as many different "globs" (filenames with wildcards) as you like. The last thing should be the target directory.

Tags:

Linux

Shell