find is a handy utility in the Linux user's toolbelt. It can be a very useful tool for diagnostic and debugging when you need to find core files or application-specific configuration files. It is also often used to find and remove temp files and other volatile files/directories that have not been accessed recently.
find searches the directory tree rooted at each given file name by evaluating the given expression from left to right, according to the rules of precedence, until the outcome is known, at which point find moves on to the next file name. - https://linux.die.net/man/1/find
-name- search by file name pattern-iname- same as above but ignore case-type- type of file (for example,d: directory,l: symlink,f: file)-size- file size (for example,c: bytes,k: kilobytes,b: 512 bytes;+n: bigger than,-n: smaller than)-mtime- file was last modified in n days (+n: greater than,-n: less than)-maxdepth- the maximum directory levelnfor the search (wherenis a non-negative number)
These are the most common arguments I usually use. For a complete list of arguments or specifics options for your distro refer to find --help.
Suppose you are in a Kafka Broker instance, find the Kafka configuration file
find / -name server.propertiesFind all files modified in the last 24 hours in 3 levels, starting from the current directory
find . -maxdepth 3 -type f -mtime 0Find all log files modified in the last 2 days
find / *.log -f -type f -mtime -2Find all node_modules directories bigger than 100 Kilobytes, starting from the $HOME dir
find $HOME -name node_modules -type d -size +100kA usual task is to execute an action on a matched file. For that purpose, we add -exec CMD {} \; by the end of the find command.CMD will run with all instances of {} replaced by the filename.
Let's figure out the broker id of a Kafka Broker instance
find / -name server.properties -exec grep 'broker.id' {} \;If you have that old project repository still attached to SVN, you can remove all .svn directories with
find /my/old/project -name .svn -exec rm -rf '{}' \;
⚠️ be very careful while running something like this
In time, there is also the -delete option which can be added to the find command similar to the -exec option and will delete the matched files. (notice thta it wouldn't work with our .svn example because those folders are not empty)
One more useful example, change the permissions of files in your website from 777 to 644
find /home2/le_me/public_html/le_website -name *.php -perm 777 -exec chmod 644 {} \;As usual, this is a practical tip with 20% of the features you'd use 80% of the time. If you are looking for a complete view of the find command you can use the man pages or the --help option. There are also entire book sections dedicated to the find command.