Complete guide to use the Find command in Linux

Introduction to the Find command

The commandfindis one of the most powerful and versatile tools in the Linux environment to locate files and directories according to multiple criteria. Its ability to combine tests, actions and operators makes it an indispensable ally for managers, developers and advanced users.

Basic syntax

The simplest way isfind [ruta] [expresión]. If the route is omitted,findpart of the current directory. For example,find /home/usuario -name '*.txt'list all files with .txt extension under the directory indicated.

Search by name

The Preacher-nameaccepts shell-style patterns. To ignore capital and lower capital is used-iname. Example:find /var/log -iname '*error*'returns any file whose name contains the word error, no matter what.

Filter by file type

With-typethe type is specified:ffor regular file,dfor directory,lfor symbolic link,bfor block device,cfor character device,pfor pipe name andsfor socket. Example:find /etc -type f -name '*.conf'locates only the configuration files in / etc.

Limit by size

The Preacher-sizeallows to filter according to weight. The accepted suffixes arec(bytes),k(kilobytes),M(megabytes) andG(gigabytes). To search for files greater than 10 MB:find /home -type f -size +10M. For files between 1 and 5 MB:find /home -type f -size +1M -size -5M.

Search by modification date

The options-mtime, -atimeand-ctimeThey work in days. A positive number indicates «more than n days», negative «less than n days» and zero «exactly n days». For modified files in the last 24 hours:find . -mtime -1. There are also variants in minutes:-mmin, -amin, -cmin.

Combining criteria with logical operators

By default, multiple expressions are involved with AND. Can be used explicitly-a(AND),-o(OR) and!or-notfor denial. Example: search .log or .txt files greater than 5 MB:find /var/log \( -name '*.log' -o -name '*.txt' \) -size +5M.

Run actions on results

The Preacher-execallows to launch a command over each found file. The syntax ends with;or with+to group arguments. Example of safe erasing:find /tmp -type f -name '*.tmp' -mtime +7 -exec rm -f {} \;. Using+the number of invocation is reduced:find /tmp -type f -name '*.tmp' -mtime +7 -exec rm -f {} +.

Safe alternative:-ok

If you want to confirm each action,-okworks the same as-execbut ask for confirmation before running. Useful in destructive operations such as deletion or change of permits.

Management of names with special spaces or characters

To avoid problems with spaces, line jumps or special characters, it is combinedfindwith-print0andxargs -0. Example:find . -type f -name '*.mp3' -print0 | xargs -0 rsync -avz /destino/.

Search for permissions

The Preacher-permallows to locate files according to your permission modes. Octal or symbolic values can be used. To find files with permissions exactly 644:find /var/www -type f -perm 644. To locate those who have at least the execution bit for the owner:find /usr/bin -type f -perm -u=x. The script-before the mode indicates that at least those bits should be fulfilled.

Using regular expressions

In addition-name, findoffers-regexand-iregexfor coincidences with regular expressions. These operators apply to the full route of the file. For example, to find files that end in .log or .txt and whose name contains a date in AAAAA-MM-DD format:find /var/log -type f -regex '.*/[0-9]{4}-[0-9]{2}-[0-9]{2}\.(log|txt)$'. Note that the point and other special characters must be escaped within the regular expression.

Daily practical examples

  • List all empty files:find /home -type f -empty
  • Find the 10 largest files:find / -type f -exec du -h {} + | sort -rh | head -n 10
  • Change permissions to all scripts:find /opt/scripts -type f -name '*.sh' -exec chmod 755 {} +
  • Remove older cache files of 30 days:find /var/cache -type f -mtime +30 -delete
  • Copy all recent PDFs to a backup directory:find /home -type f -name '*.pdf' -mtime -7 -exec cp {} /backup/pdf/ \;

Performance tips

Limit search depth with-maxdepthand-mindepthavoid going through unnecessary directories. For example, look only at the first two levels:find /var/www -maxdepth 2 -type f -name '*.php'. In addition, use-pruneallows for the exclusion of specific subtrees:find / -type d -name 'proc' -prune -o -type f -name '*.log' -printomit the / proc directory.

Conclusion

Domainfindis essential for any advanced Linux administrator or user. Its flexibility allows from simple name searches to complex operations combined with other commands by-execor pipes. Practice the examples presented and consult the manual page (man find) will consolidate knowledge.

This work is under aCreative Commons License Attribution 4.0 International for Francesc Roig francesc @ vivaldi.net.

EnglishenEnglishEnglish