> For the complete documentation index, see [llms.txt](https://riteshs4hu.gitbook.io/infosec-notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://riteshs4hu.gitbook.io/infosec-notes/linux-server-administrator/linux-commands/file-finding.md).

# File Finding

### **`tree`**&#x20;

Displays directories and files in a tree-like structure. It is useful for visualising directory hierarchies.

#### **Syntax**

```
tree [directory_path]
```

#### **Example:**

```
tree /home/user
```

***

### **`find`**&#x20;

Searches for files and directories based on various parameters like name, type, permissions, and more.

#### **Basic Syntax**

```
find [where to search] [what to search]
```

* `where to search` → The directory to start searching from.
* `what to search` → Criteria like file name, type, permissions, etc.

#### **Examples**

```
find /root example
```

* Searches for a file or directory named `example` in the `/root` directory.

#### **`find` Command Options**

The `find` command supports various options (switches) to refine search results.

**1. `-name` (Search by Name)**

Searches for a file or directory by its name.

**Example**

```
find /usr -name user
```

* Searches for any file or directory named `user` inside `/usr`.

***

**2. `-type` (Search by Type)**

Filters results based on file type:

* `f` → Search for files
* `d` → Search for directories

**Examples**

```
find / -name user -type f
```

* Finds files named `user`.

```
find / -name user -type d
```

* Finds directories named `user`.

```
find / -name shadow -type d 2> /dev/null
```

* Searches for directories named `shadow`, while redirecting permission errors to `/dev/null`.

***

**3. `-readable` (Find Readable Files)**

Searches for files that have read permissions.

**Example**

```
find / -name user -type f -readable
```

* Finds all readable files named `user`.

***

**4. `-writable` (Find Writable Files)**

Searches for files that have write permissions.

**Example**

```
find / -name user -type f -writable
```

* Finds all writable files named `user`.

***

**5. `-exec` (Execute Commands on Found Files)**

Runs a specified command on each search result.

**Example**

```
find / -name user -exec pwd \;
```

* Searches for a file named `user` and runs `pwd` (prints working directory) for each result.

***

### **`which`**&#x20;

Locates the executable file associated with a command by searching the directories listed in the `$PATH` environment variable.

#### **Syntax:**

```
which [command]
```

#### **Examples:**

```
which vim
```

* Shows the path to the `vim` executable.

```
which non_existing_command
```

* If the command is not installed, it returns no output.

***

### **`whereis`**&#x20;

Finds the location of binary, source, and manual files related to a command.

#### **Syntax:**

```
whereis [command]
```

#### **Examples:**

* Displays paths to the `vim` binary, source code, and manual files.

```
whereis vim
```

* Returns an empty result if the command is not installed.

```
whereis non_existing_command
```
