Tuesday, April 17, 2007

UNIX/Linux - Find command

To look for files in the current directory tree then run a command on them:
find . -name <file-pattern> -exec <command> {} \;

Examples

To grep for "hello" inside all .txt files:
find . -name *.txt -exec grep -Hn hello {} \;

Put this in a shell script to recursively grep .cpp and .hpp files for the first argument:
#!/bin/bash
find . -name *.[ch]pp -exec grep -Hn "$1" {} \;

No comments: