Search and Shift

03Feb10

I’ll start by declaring the Ubuntu Forums is a brilliant place. The people are usually great, and the support is top class. And every now and then you come across a gem. The forum archives are full of them, and they keep cropping up on Google. Here‘s a handy little one I found today.

I found it whilst in a bit of a predicamint, which I’ll talk about in another blog. I needed to copy every file of a certain type in a directory. Only problem is, those files were in hundreds of sub directories. It would have taken hours to trawl through all of that.

But as usual, there was an easier way. In the command line, where else? πŸ™‚ The might of the terminal knows no bounds. In reality, its a simple, but brilliant command.

 find ~/Music -name '*.mp3' -exec mv {} ~/Desktop/folder \;
In reality, its quite a simple command, in this case finds every file with .mp3 in the folder /home/user/Music and moves to a folder on their Desktop. The \; at the end ‘escapes’ the mv command, so that it knows where to stop. {} simply means the resulting files from the find command.
Just to make it clear how you can change it for your own use, here’s another example.
find ~/Documents -name '*.doc' -exec cp {} ~/Desktop/folder \; 
Here it finds every file with .doc in its name in the ‘Documents’ folder, and copies it to the same folder on the desktop.
Quite a useful script, and one which is definitely going to come in handy. Don’t forget to man find to learn more πŸ™‚