Find command in Linux: search files and directories

Introduction

The Find command is one of the most powerful and versatile tools offered by the Linux shell to locate files and directories according to various criteria. Unlike simpler options like ls or locate, find allows you to apply filters of name, type, size, date of modification, permissions and much more, in addition to running actions on the results found.

Basic syntax

The general form of the command is: find [route] [expression]. The route indicates the starting point of the search; if omitted, find assumes the current directory. The expression is composed of tests (test) and actions (action) that are evaluated from left to right. For example, find / home -name*.txt 'search all files with .txt extension inside / home and their subdirectories.

Search by name

One of the most used tests is -name, which accepts a pattern with shell comodines as * And? To make the search insensitive to capital and lower capital is used -iname. For example, find / var / log -iname*error*'locates any file that contains the word error no matter if it is in capital letters or lower letters.

Search by type

The -type test allows to filter according to the file type. The most common values are f for regular files, d for directories, l for symbolic links, b for block devices and c for character devices. A typical example is find / etc -type d -name '*. conf 'to find directories whose name ends in .conf, although normally the directories have no extension, this example shows the combination of tests.

Search by size

With -size you can locate files whose size meets a condition. The number may be followed by suffix: c (bytes), w (two bytes words), k (kilobytes), M (megabytes), G (gigabytes). The + sign indicates greater than, - indicates less than and without a sign indicates exactly that size. For example, find / home -size + 100M finds files greater than 100 megabytes, while find / tmp -size -10k locates those that are less than 10 kilobytes.

Search by modification date

The -mtime, -atime and -ctimme tests measure time in days from the last modification, access or state change, respectively. Using -mtime -7 finds modified files in the last seven days, -mtime + 30 those modified more than 30 days ago. They can also be used -mmin and -amin to work with minutes instead of days.

Use logical operators

Find allows to combine tests with the AND (-a, implied by default), OR (-o) and NOT (-not or!) operators. For example, find / opt -type f (-name '*.log '-o -name'*.bak ') -size + 1M locates regular files that are entweder .log or .bak and exceed the megabyte. The parentheses must escape or be locked in quotes so that the shell does not interpret them.

Run shares with -exec

One of the most useful features is the ability to launch a command over each result by -exec. The syntax is -exec command {}; where {} is replaced by the path of the found file and; ends the instruction. For example, find. -name '*.tmp '-exec rm {}; removes all temporary files in the current directory and its subdirectories. If you prefer to confirm before deleting, you can use -ok instead of -exec.

Combined examples

Suppose we want to locate all PDF files greater than 5 MB that have been modified in the last ten days and compress them with gzip. The command would be: find / documents -type f -name '*.pdf '-size + 5M -mtime -10 -exec gzip {};. Another common situation is to look for broken symbolic links: find / -type l -! -exec test -e {}; -print.

Tips and tricks

To prevent find from running file systems that we do not want, you can use -xdev to stay within the same file system. When the output is long, redirect it to a file or pay it with less legibility: find / usr -type f -name '*. conf '2 > / dev / null | less. In addition, combining find with xargs allows to process file lots more efficiently: find. -type f -name '*.csv '-print0 | xargs -0 wc -l.

Conclusion

Dominating the Find command provides the system manager and advanced user with precise control over the location and handling of files in Linux. Its flexibility allows from simple searches to complex scripts that automate maintenance, audit and backup tasks. Practice with different tests and operators is the best way to incorporate this tool into the daily workflow.

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

EnglishenEnglishEnglish