Friday, November 14, 2008

Finding Files with Directories in Path in Linux/OS X

When trying to find files in Linux/OS X/*nix with certain/specific directory names in the path/pathname, be sure to define that path as the first argument to find, and not part of the "-name". The correct usage is:
find pathpattern -name filenamepattern
For example, if you want to find all log4j.properties files using FileAppender instead of RollingFileAppender (more often than not, you'll want logs to roll), but you only want to find them under trunk directories, you might use:
find **/trunk/** -name log4j.properties -exec grep -H FileAppender {} \;
Also it is smart to put quotes around the filename pattern (but not the path pattern because that doesn't work). For example:
find **/log*/** -name "*.log*" -exec grep -H ERROR {} \;

No comments: