Find Quick Reference

This is a quick cheat sheet of my favourite find commands.

Find Then Grep

find . -name file.* -exec grep -Hn thingtogrep {} \;

Find By Name


find . -name “MyCProgram.c”


find . -name “proto*”

Find And Copy Somewhere


find . -name -exec cp {} / \;


find . -name wget* -exec cp {} $TEMP/wget \; (confirmed)

Find And Delete


find . -name “.svn” -exec rm -rf {} \;

This finds and deletes svn directories. See link on Cyberciti for more details.

Find And Open A File


find ~ -name “file.xml” -exec vim {} \;

Find And Move Somewhere


find -name “*.java” -exec mv {} /cygdrive/c/temp \;

Find And Replace In Files


find . -type f -print | xargs sed -i ‘s/thing_to_replace/another_thing/g’

Or

find . -type f -exec sed -i ‘s/ugly/beautiful/g’ {} \;

Note the -i is for edit in place, not case-insensitive.

Run A Command On A File


find -exec \;


find -iname “MyCProgram.c” -exec rm {} \;


find -iname “MyCProgram.c” -exec md5sum {} \;

Finding the Top 5 Big Files


find . -type f -exec ls -s {} \; | sort -n -r | head -5


find . -type f -exec ls -s {} \; | sort -n -r | head -10 > ~/largefiles.txt &

Find Files Based on File-Type


find . -type s

Show files which are modified after the specified file


find -newer ordinary_file

Find Files Larger Than The Given Size


find ~ -size +100M

Find Files Whose Content Was Updated Within The Last…


1 hour: find . -mmin -60


1 day: find / -mtime -1

Exclude a Directory


find . -path ‘./excludedir’ -prune -o -name “*” -print

Search All jar Files In A Directory For A Class Name


find . -type f -name ‘*.jar’ -print0 | xargs -n1 -0i sh -c ‘jar tf “{}” | grep -q AQOracleDriver && echo “{}”‘

Avoiding Permission denied messages

Add this:

-perm -a+r -perm /a+w ! -perm /a+x