Skip to content

Instantly share code, notes, and snippets.

@soeirosantos
Last active June 14, 2020 00:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save soeirosantos/1f18cd03e5a19d6c0a2620b03ee19cdc to your computer and use it in GitHub Desktop.
Save soeirosantos/1f18cd03e5a19d6c0a2620b03ee19cdc to your computer and use it in GitHub Desktop.

Linux tips for developers: find

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

Common arguments

  • -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 level n for the search (where n is 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.

Examples

Suppose you are in a Kafka Broker instance, find the Kafka configuration file

find / -name server.properties

Find all files modified in the last 24 hours in 3 levels, starting from the current directory

find . -maxdepth 3 -type f -mtime 0

Find all log files modified in the last 2 days

find / *.log -f -type f -mtime -2

Find all node_modules directories bigger than 100 Kilobytes, starting from the $HOME dir

find $HOME -name node_modules -type d -size +100k

Execute an action

A 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 {} \;

Final thoughts

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment