Idea Digital Imaging Ltd

Scanning, retouching, page layout, proofing

  • Increase font size
  • Default font size
  • Decrease font size
Home Obessions Unix Terminal - the Find command

Terminal - the Find command

For simple searches you need provide nothing more than a directory to start searching:

find $HOME

This is a bit basic :-) it finds everything in your Home directory. But we can be a bit more selective:

find $HOME -name "*.tif"

This command searches your Home directory for any files whose names end with ".tif". The "*" is a wildcard. If you also want to find files that end with ".TIF" then you can use -iname instead of -name.

If you want to find all the files that don't end with ".tif", you can do this:

find $HOME \! -iname "*.tif"

There are too many modifiers to the find command to cover here. If you need more information you should type:

man find

The real advantages of using find is the fact that we can add modifiers to make our search more specific. Let's say we want to find every ".tif" file that has been modified in the last 2 days:

find $HOME -iname "*.tif" -mtime -2

We can also move or copy the files to another location by invoking another command:

find $HOME -iname "*.tif" -mtime -2 -exec cp {} /Users/myName/Desktop/archive \;

So, here we've found all the files that end with ".tif" (with or without caps) that have been modified in the last 48 hours and copied them to a folder on the desktop.

Hopefully you can see how powerful this single line could be if you use it as a backup strategy or a way of retrieving data without having to open and search loads of folders by hand.

You can also use it to copy the music files that are hidden on your iPod back to your hard disk:

find . -iname "*.mp3" -exec cp {} /Users/myName/Desktop/music \;

Note the "." after the find command. This means that the search starts from the current location. So all you need to do is type "cd " in the Terminal, then drag and drop your iPod on to the Terminal window and then hit return to get to the right place to run the command. Make a new folder to copy your music to and then drag and drop it on the Terminal window after the "{} " and the Terminal will complete the paths without you having to waste time typing it all in.

As with all Terminal commands, please be careful with what you type. Powerful commands can lead to big problems if you type the wrong stuff in accidentally.
Last Updated on Monday, 18 May 2009 20:55  

Related Items


Newsflash

New Web Site -- we've upgraded to Joomla 1.5.