Locate Files with Unix ‘find’ Command

Posted on March 7, 2007

1


I have been working with Unix for some years and have managed to learn a little bit of the find command, but I have always forget the syntax. That is why I am writing these lines to serve as a reminder as well as help for beginners.

Example 1: List all Python .pyc files in my current directory, including sub directories:

find . -name “*.pyc” -print

Notes:

  1. The wildcards after the -name flag must be wrapped in double quotes and
  2. Users who run BSD systems (including Mac OS X) can omit the -print flag

Example 2: Remove all .tmp files from my USB key (which is mounted at /Volumes/hai_usbkey), including those in sub directories:

find /Volumes/hai_usbkey -name ‘*.tmp’ -delete

Notes:

  1. We can wrap wildcards with single quotes
  2. The -delete flag tells the find command to remove the files. Please use this command with care since you cannot undo deletion
About these ads
Posted in: OSX, Tips & Tricks, Unix