Find

Looking for a file or directory but forgot where it is or if it even exists? Wonder no more. The command in question is **find**. To make the understanding of this command even more enticing, once you know it, you can start working it into your Bash scripts. That’s not only convenience, that’s power!

## How to use

### **Example that always works**

```
sudo find / -type f -name "\*.conf"

```

### Preview

If you issue the command *man* **find** `(find -help)`, you’ll see the syntax of the *find* command is:

```
find [-H] [-L] [-P] [-D debugopts] [-Olevel] [starting-point...] [expression]

```
Naturally, if you’re unfamiliar with how the man pages work, you might be confused about it or overwhelmed by that syntax.

### Simplification / Example

```
find /path option filename

```

### Find by name

By default the command is searching recursively, starting from the current directory unless a specific path is given as argument. If not specified the command will look for everything (files, directories, etc. ...)

#### Name options:

* **name** – case sensitive
* **iname** – case insensitive

#### Example:

If the full name including the extension of a file / directory / etc. is known the quotation marks can be left out, but it is recommended to use them.

##### Find file case sensitive:

```
find / -name "linux.odt"

```

##### Find file case insensitive:

```
find / -iname "linux.odt"

```

##### Find file using only part of the name:

the following example will look for the file above and everything that comes after `**"linu"**`.

```
find / -name "linu\*"

```

### Find by type

What if you’re not so concerned with locating a file by name but would rather locate all files of a certain type? Some of the more common file descriptors are:

* **f** – regular file
* **d** – directory
* **l** – symbolic link
* **c** – character devices
* **b** – block devices

#### Example:

```
find / -type c

```
The above command would result in quite a lot of output (much of it indicating permission denied), but would include output similar to:

```
/dev/hidraw6
/dev/hidraw5
/dev/vboxnetctl
/dev/vboxdrvu
/dev/vboxdrv
/dev/dmmidi2
/dev/midi2
/dev/kvm

```

### Find by type and name:

We can use the previous options to look for configuration files (`"*.conf"`). The command would look like this:

```
find / -type f -name "\*.conf"

```
The above command would traverse the entire directory structure to locate all regular files ending in .conf. If you know most of your configuration files are housed in /etc, you could specify that like so:

```
find /etc -type f -name “*.conf”

```
The above command would list all of your .conf files from /etc

### Save search in file / Output results

One really handy trick is to output the results of the search into a file. When you know the output might be extensive, or if you want to comb through the results later, this can be incredibly helpful. For this, we’ll use the same example as above and pipe the results into a file called *conf\_search*. This new command would look like this:

```
find /etc -type f -name “*.conf” > conf_search

```
A file (`"*conf\_search"*`) that contains all of the results from the find command issued can be found in the current directory.

### Find file by size

Now we get to a moment where the find command becomes incredibly helpful. I’ve had instances where desktops or servers have found their drives mysteriously filled. To quickly make space (or help locate the problem), you can use the find command to locate files of a certain size. Say, for instance, you want to go large and locate files that are over 1000MB. The find command can be issued, with the help of the *-size* option, like so:

```
find / -size +1000MB

```

#### Available size descriptions:

* **c** – bytes
* **k** – Kilobytes
* **M** – Megabytes
* **G** – Gigabytes
* b – 512-byte blocks