Notes to Self

Alex Sokolsky's Notes on Computers and Programming

find cheat sheet

find searches for files.

Use 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"

Note the use of quotes to prevent shell handling the wildcards.

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 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 Print

Display the files older than 30 days:

find . -mtime +30 -print

Find and Delete

Delete all .bak files:

find . -name "*.bak" -delete

Delete empty directories:

find releases/ -type d -empty -delete

Find and delete all the terraform locks:

find . -type f -name .terraform.lock.hcl -delete

Remove all the files from /tmp owned by jdoe:

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

Find and Execute A Command on It

Remove all the directories named .terragrunt-cache:

find . -type d -name .terragrunt-cache -prune -exec rm -fr {} \;

Note that:

Search for python files and then run 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 '{}' \;

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

Example - Move Directories Between File Systems

From https://docs.oracle.com/cd/E36784_01/html/E39021/bkupsavefiles-14144.html#scrolltoc

Copy from /data1 to /data1:

find /data1 -print -depth | cpio -pdm /data2