Notes to Self

Alex Sokolsky's Notes on Computers and Programming

find cheat sheet

find searches for files.

Search Examples

Find a file called testfile.txt in current and sub-directories:

find . -name testfile.txt

Find all .jpg files in the /home and sub-directories:

find /home -name *.jpg

Find an empty file within the current directory:

find . -type f -empty

Find all .db files (ignoring text case) modified in the last 7 days by a user exampleuser:

find /home -user exampleuser -mtime 7 -iname ".db"

Find all the files that end with conf and have been modified in the last 7 days:

find / -name "*conf" -mtime 7

Filters exampleuser user’s home directory for files with names that end with the characters conf and have been modified in the previous 3 days:

find ~exampleuser/ -name "*conf" -mtime 3

Find and Act

Searches for python files and then runs grep for “future_state”:

find . -type f -name '*.py' -exec grep 'future_state' '{}' +

List files oder than 300 days:

find * -mtime +300 -exec ls -l {} \;

Looks for rc.conf and runs the chmod o+r command to modify file permissions:

find . -name "rc.conf" -exec chmod o+r '{}' \;

Delete all .bak files:

find . -name "*.bak" -delete

Display the files older than 30 days:

find . -mtime +30 -print

Remove all the files from /tmp owned by a.sokolsky:

find /tmp/* -user jdoe -exec rm -fr {} \;

Find and Act Using xargs

find -exec may not be enough.

xargs takes the results of a command (one per line) and calls another command N times, one per line, injecting the line value as an argument. Using xargs.

Calculate LOCs:

find . -name '*.py' | xargs wc -l