## 1. The `which` command

The `which` command and the `whereis` command below both search for executable files, which are usually binary files.

Commands can be divided into built-in commands and external commands. Built-in commands are part of the shell feature, and their executable code is stored in `/bin/bash`. External commands have their own corresponding executable program files. We can use `type` to differentiate:

![Linux type command output showing built-in vs external command differences](https://img.upweb.dev/content/92947270-800x385.jpg)

The `which` command looks for commands from the `$PATH` environment variable:

```bash
→ echo $PATH | tr : '\n'
/usr/local/bin
/usr/bin
/usr/local/sbin
/usr/sbin
```

Let’s try it out:

![Linux which command example demonstrating executable file search in PATH environment variable](https://img.upweb.dev/content/e453cb2a-800x482.jpg)

## 2. The `whereis` command

`whereis` can search not only executable files, but also related documentation, configuration files, source files, etc. So the scope of its search is not only `$PATH`, but also the directory where the man page is located, and the directory where src is located. We can use `whereis -l` to view:

```bash
→ whereis -l
bin: /usr/bin
bin: /usr/sbin
bin: /usr/lib
man: /usr/share/man/man7
man: /usr/share/man/man0p
man: /usr/share/man/man1
src: /usr/src/debug
src: /usr/src/kernels
...
```

Its common options are:

```html
-b Search for binaries. -m Search for manuals. -s Search for sources. -u Only
show the command names that have unusual entries.
```

Let’s try it out:

![Linux whereis command example showing binary, manual, and source file search results](https://img.upweb.dev/content/603a61a0-800x475.jpg)

## 3. The `locate` command

The `locate` command is mainly used to quickly find files or directories. It is based on the data file to search, because the scope is small, so the speed is faster. We can see which databases it uses with `locate -S`:

```html
→ locate -S Database /var/lib/mlocate/mlocate.db: 16,847 directories 160,905
files 8,598,623 bytes in file names 4,038,841 bytes used to store database
```

But be aware that this database file is usually updated once a day (update frequency may vary by Linux distribution), so if you create a new file, it may not be searched. But we can use the `updatedb` command to actively update the database file and then use `locate`:

![Linux locate command with updatedb demonstrating database file search optimization](https://img.upweb.dev/content/09794e87-457x329.jpg)

To be more specific, when calling `updatedb`, it will search for files in the file system according to the configuration in `/etc/updatedb.conf`, and update the results to the database file.

![Linux updatedb.conf configuration file showing file system search exclusions and pruning settings](https://img.upweb.dev/content/f2a79109-800x177.jpg)

Check `/etc/updatedb.conf`, where `PRUNE_BIND_MOUNTS` is whether to enable restrictions, and `yes` indicates that the following configuration takes effect. The following `PRUNEFS` indicates file systems that do not need to be searched, `PRUNENAMES` indicates file names that do not need to be searched, and `PRUNEPATHS` indicates directories that do not need to be searched.

Let’s try it out:

```html
→ locate passwd /etc/passwd /etc/passwd- /etc/pam.d/passwd /etc/security/opasswd
/usr/bin/gpasswd ...
```

It will list all files that contain `passwd` in their filenames. Here are also some commonly used options:

```html
-l Exit successfully after finding LIMIT entries. -i Ignore case distinctions
when matching patterns. -c Instead of writing file names on standard output,
write the number of matching entries only. -r Search for a basic regexp REGEXP.
```

## 4. The `find` command

The `find` command is the most powerful search command, it searches the file system directly, and with so many options supported, you're sure to find what you're looking for. But correspondingly, if your disk performance is poor, it may take more time.

The usual syntax for `find` is `find [directory] [options] [expression]`. Here are a few simple examples:

List all directories, files, and subfiles in the `/home` directory:

```bash
find /home
```

Search the `/home` directory for all files larger than `10MB`:

```bash
find /home -size +10M
```

List all files in the `/home` directory that has been modified in the last 7 days:

```bash
find /home -mtime +7
```

Search the system for all files ending with `.txt`:

```bash
sudo find / -name *.txt
```

Search the system for all files ending in `.mp3` and delete all found files:

```bash
sudo find / -name "*.mp3" -exec rm -rf {} \;
```

List all empty files in the current directory:

```bash
find . -empty
```

_If you found this helpful, consider [subscribing to my newsletter](https://upweb.dev) for weekly web development insights and updates. Thanks for reading!_
