2022-05-25 08:00:04 +01:00
|
|
|
---
|
2022-09-06 15:44:40 +01:00
|
|
|
categories:
|
|
|
|
- Programming Languages
|
2022-05-25 08:00:04 +01:00
|
|
|
tags:
|
|
|
|
- shell
|
|
|
|
---
|
|
|
|
|
|
|
|
# `find`
|
|
|
|
|
2024-02-02 15:58:13 +00:00
|
|
|
`find` can be used both to locate files and run operations on the files it
|
|
|
|
finds.
|
2022-05-25 08:00:04 +01:00
|
|
|
|
|
|
|
## Main syntax
|
|
|
|
|
|
|
|
### No options
|
2022-09-06 15:44:40 +01:00
|
|
|
|
2024-02-02 15:58:13 +00:00
|
|
|
Without options specified, `find` alone will return a recursive index of all the
|
|
|
|
files in the directory from which it is run.
|
2022-05-25 08:00:04 +01:00
|
|
|
|
|
|
|
### Sub-directory
|
2022-09-06 15:44:40 +01:00
|
|
|
|
2024-02-02 15:58:13 +00:00
|
|
|
If we pass a directory to `find` it will repeat the above process but
|
|
|
|
specifically for that directory.
|
2022-05-25 08:00:04 +01:00
|
|
|
|
|
|
|
```bash
|
|
|
|
$ find i3
|
|
|
|
i3
|
|
|
|
i3/config
|
|
|
|
```
|
|
|
|
|
|
|
|
### Filters
|
2022-09-06 15:44:40 +01:00
|
|
|
|
2022-05-25 08:00:04 +01:00
|
|
|
We can specify flags as filters (known as 'tests' within the program).
|
|
|
|
|
|
|
|
#### Type
|
2022-09-06 15:44:40 +01:00
|
|
|
|
2022-05-25 08:00:04 +01:00
|
|
|
Filter by type: file or directory
|
|
|
|
|
|
|
|
```
|
|
|
|
$ find -type d # return dirs only
|
|
|
|
$ find -type f # return files only
|
|
|
|
```
|
|
|
|
|
|
|
|
Within a specified directory:
|
|
|
|
|
|
|
|
```bash
|
|
|
|
$ find i3 -type f
|
|
|
|
```
|
|
|
|
|
2022-05-25 20:30:04 +01:00
|
|
|
#### Filename
|
2022-09-06 15:44:40 +01:00
|
|
|
|
2022-05-25 08:00:04 +01:00
|
|
|
This is the most frequent use case: filter files by name with globbing.
|
|
|
|
|
|
|
|
```bash
|
|
|
|
$ find -name "config"
|
|
|
|
./.git/config
|
|
|
|
./i3/config
|
|
|
|
```
|
|
|
|
|
|
|
|
```bash
|
|
|
|
$ find -name "*.js"
|
|
|
|
```
|
|
|
|
|
|
|
|
The same, but case insensitive: `iname`
|
|
|
|
|
|
|
|
```bash
|
|
|
|
$ find -iname "*.JS"
|
2022-05-25 20:30:04 +01:00
|
|
|
```
|
|
|
|
|
|
|
|
#### Path
|
2022-09-06 15:44:40 +01:00
|
|
|
|
2024-02-02 15:58:13 +00:00
|
|
|
As above but this time includes directory names in the match. `ipath` is the
|
|
|
|
case-insensitive version.
|
2022-05-25 20:30:04 +01:00
|
|
|
|
|
|
|
```bash
|
|
|
|
$ find -path "utils*"
|
|
|
|
utils.js
|
|
|
|
utils/do-something.js
|
|
|
|
```
|
|
|
|
|
|
|
|
### Operators
|
2022-09-06 15:44:40 +01:00
|
|
|
|
2024-02-02 15:58:13 +00:00
|
|
|
We can combine `find` commands by using logical operators: `-and`, `-or`,
|
|
|
|
`-not`. For example:
|
2022-05-25 20:30:04 +01:00
|
|
|
|
|
|
|
```bash
|
|
|
|
$ find -not -name "*.js" -type f
|
|
|
|
./app/index.html
|
|
|
|
./app/style.css
|
|
|
|
./dist/index.html
|
2022-05-26 07:30:04 +01:00
|
|
|
./dist/style.c
|
2022-12-15 07:30:05 +00:00
|
|
|
```
|
2022-05-26 07:30:04 +01:00
|
|
|
|
2022-12-15 07:30:05 +00:00
|
|
|
Applied to a directory:
|
2022-05-26 07:30:04 +01:00
|
|
|
|
2022-12-15 07:30:05 +00:00
|
|
|
```bash
|
|
|
|
find . -type -f -not -path "./.git/"
|
2022-05-25 20:30:04 +01:00
|
|
|
```
|
|
|
|
|
2024-02-02 15:58:13 +00:00
|
|
|
## Run programs against results
|
2022-09-06 15:44:40 +01:00
|
|
|
|
2024-02-02 15:58:13 +00:00
|
|
|
Using the `exec` keyword we can run a program against the files that are
|
|
|
|
returned from `find`.
|
2022-05-25 20:30:04 +01:00
|
|
|
|
2024-02-02 15:58:13 +00:00
|
|
|
In this syntax we use `{}` as a placeholder for the path of the file that is
|
|
|
|
matched. We use `;` (escaped) to indicate the end of the operation.
|
2022-05-25 20:30:04 +01:00
|
|
|
|
|
|
|
### Examples
|
|
|
|
|
|
|
|
This script deletes the files that match the filter criteria:
|
|
|
|
|
|
|
|
```bash
|
|
|
|
$ find -name "*.js" -exec rm {} \;
|
|
|
|
```
|
2022-09-06 15:44:40 +01:00
|
|
|
|
2024-02-02 15:58:13 +00:00
|
|
|
This script finds all the files with the substring 'config' in their name and
|
|
|
|
writes their file size to a file.
|
2022-09-06 15:44:40 +01:00
|
|
|
|
2022-05-25 20:30:04 +01:00
|
|
|
```bash
|
2022-09-06 15:44:40 +01:00
|
|
|
find -name '*config*' -exec wc -c {} \; > config-sizes
|
|
|
|
```
|