Three Simple Examples of the Find Command
Tags: linux, shell
Published: 2020-07-17 18:27:27

# 1. Overview

Looking for files is a common operation when we work in the Linux command-line. The find command is a handy utility to find files.

In this short tutorial, we'll have a look at three simple but practical examples of the find [command](https://www.baeldung.com/linux/find-command).

# 2. Find Files by a Filename Pattern

Searching for files matching a particular name or name pattern could be the most common use-case of the find command. Using the find command, we can pass the -name option and a filename pattern to locate the files.

For example, we can use the -name option to find all json files under our Python installation directory /usr/lib/python3.8:

```
$ find /usr/lib/python3.8 -name '*.json' 
/usr/lib/python3.8/site-packages/glances/outputs/static/package.json
/usr/lib/python3.8/site-packages/glances/outputs/static/package-lock.json
/usr/lib/python3.8/site-packages/compose/config/config_schema_v3.5.json
/usr/lib/python3.8/site-packages/compose/config/config_schema_v3.8.json
/usr/lib/python3.8/site-packages/compose/config/config_schema_v2.0.json
...
/usr/lib/python3.8/site-packages/jsonschema/schemas/draft3.json
/usr/lib/python3.8/site-packages/jsonschema/schemas/draft6.json
/usr/lib/python3.8/site-packages/zmq/utils/config.json
/usr/lib/python3.8/site-packages/zmq/utils/compiler.json
/usr/lib/python3.8/site-packages/guzzle_sphinx_theme/guzzle_sphinx_theme/static/fonts/source-serif-pro/bower.json
```

The command is pretty straightforward. But it's worthwhile to mention that when we use the -name option with a pattern, the find command will match against basenames only — that is, the leading directories will be ignored.

For example, if we want to find the two json files under the */utils/ directory, this command will fail:
```
$ find /usr/lib/python3.8 -name '*/utils/*.json'
find: warning: ‘-name’ matches against basenames only, but the given pattern contains a directory separator (‘/’),
thus the expression will evaluate to false all the time.  Did you mean ‘-wholename’?
```

As the warning message suggested, we can use the option -wholename to do the search with leading directories:
```
$ find /usr/lib/python3.8 -wholename '*/utils/*.json'                                    
/usr/lib/python3.8/site-packages/zmq/utils/config.json
/usr/lib/python3.8/site-packages/zmq/utils/compiler.json
```
It works! The expected result is printed.

However, it's worth noting that the -wholename option is GNU-specific. This option is not supported by HP-UX find and not a part of the POSIX 2008 standard.

A more portable way to match leading directories and basenames is to use the -path option:
```
$ find /usr/lib/python3.8 -path '*/utils/*.json'
/usr/lib/python3.8/site-packages/zmq/utils/config.json
/usr/lib/python3.8/site-packages/zmq/utils/compiler.json
```

# 3. Find Files by Size

Sometimes, we want to find files that are larger or smaller than a certain size. We can use the -size option of the find command to do exactly that.

Let's find all files larger than 5MB under the Python installation directory:
```
$ find /usr/lib/python3.8 -size '+5M'
/usr/lib/python3.8/config-3.8-x86_64-linux-gnu/libpython3.8.a
/usr/lib/python3.8/site-packages/PyQt5/QtWidgets.abi3.so
```

The -size option requires a "+/-n[UNIT]" parameter to do the search.

The "+" stands for greater than, while the "-" means less than. The --size  option supports six units:

b: 512-byte blocks
c: bytes
w: two-byte words
k: kilobytes
M: megabytes
G: gigabytes

# 4. Find Files with the Same Filename

In the last example, let's find out the files with the same filename.

We'll find all files matching the "README.*" pattern under the Python installation directory, grouping together those with the same filename:
```
$ find /usr/lib/python3.8 -name 'README.*' | awk -F'/' 'a[$NF]++{print $NF" -> "$0}' | sort
README.md -> /usr/lib/python3.8/site-packages/dulwich/contrib/README.md
README.md -> /usr/lib/python3.8/site-packages/guzzle_sphinx_theme/guzzle_sphinx_theme/static/fonts/source-serif-pro/README.md
README.txt -> /usr/lib/python3.8/idlelib/Icons/README.txt
...
README.txt -> /usr/lib/python3.8/site-packages/zc/lockfile/README.txt
```
In this example, we passed the output of the find command to an awk one-liner. The awk one-liner is responsible for finding the files with duplicate names. Finally, the sort command groups the results.

Using the find command, not only can we find files that have the same names, but we can also find [duplicate files](https://www.baeldung.com/linux/finding-duplicate-files) as well. 

# 5. Conclusion

The find command is a common Linux command-line utility. In this short article, we had a look at three simple examples of this command.