Matthew Orndoff

Matthew Orndoff

Web Developer, Cold Brew afficiando. Just tryna keep it 💯

find

Life is hard. Don’t make it harder by not knowing how to find things.
Let’s use find to make our lives easier.

Another option to help us find files is locate.
It’s much faster than find, but it can get out of date if you don’t
update its database with sudo udatedb.

filters

find has a bunch of filters you can use to slice up your queries.

find . -name STRING       # Find file STRING in current dir
find . -type [TYPE] # Where TYPE is f:file, d:dir, l:symlink
find . -mtime NUM
find . -exec COMMAND
find . -path
find . -print ACTION
find . -delete ACTION
find . -maxdepth NUM # Find all files to max depth NUM

examples

# find [dirToSearch] [fileFilter] [ActionOnFiles]
find . -type f
find . -name "newest file"
find /etc -name "*mail*" # Finds everything with 'mail' in filename
find . -name "*.txt"
find . -size +2048
find . -mtime -1 # (less than a day ago)
find . -mtime +1 # (more than a day
find . -mtime +60 # Finds files that were modified more than 60 days ago in current dir.
find . -mtime -2 # Finds files modified within last two days

find . -ctime # (created time)

find . -iname "*.txt" -or -iname "*.doc" # returns all txt and or doc files.
find . -iname "*.txt" -or -iname "aDir" -prune

find / -type f -size +100M # Find files greater than 100Mb
find / -type f -name *.tar.gz -size +100M -exec ls -l { }; # Find tar.gz files larger than 100Mb
find / -type f -name *.tar.gz -size +100M -exec rm -f { }; # Delete them

find -ls | sort -k 11 # Finds files, sorts them by the 11th column (shnazzy!)
find -newer server.c # Find files more reently modified than server.c
find type -d [(directories) -f (files) -l (symbolic links)] # Search by type
find -delete # Delete matching files

find -exec # Run command on each file, replacing {} wiht appropriate filename and terminated by \;
find -name '*pl' -exec perl -c {} \;

find -exec file {} \; # list all filestypes of files in dir.
find -empty # Find all empty files in the home directory

# Archive file not modded in last x number of days.
find /home/user -type f -mtime +60 | xargs tar -cvf /tmp/`date '+%d%m%Y'_archive.tar`