# Unix and Linux Guide: How to Use the Find Command

The `find` command in Unix is a powerful utility used for searching files and directories based on various criteria such as name, type, size, permissions, modification time, and more. It recursively descends into directory trees and matches conditions specified by the user. The basic syntax is:

```bash
find [path] [expression]
```

* **\[path\]**: The directory where the search begins. If omitted, it defaults to the current directory (`.`).
    
* **\[expression\]**: Criteria for searching (e.g., name, type, size, etc.).
    

<div data-node-type="callout">
<div data-node-type="callout-emoji">💡</div>
<div data-node-type="callout-text">The default path of the find command is the current directory. The default expression is the print</div>
</div>

## **Hands-on Exercise Overview**

This hands-on exercise shows how to use `find` command and its different options to search for files and directories on Ubuntu 22.04. It focuses on basic file searching, advanced searching techniques, and executing commands on found files.

## **Hands-on Exercise**

1. To search for a file with a specific ***name***, use the `-name` option:
    
    ```bash
    sudo find / -name "*fish*"
    ```
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719702961139/da4e652d-bb32-44d4-afe6-3408eef551d9.png align="center")
    
    This command searches the entire filesystem (`/`) for files and directories whose names contain the substring "fish".
    
    `sudo` provides root access, ensuring no permission issues, and produces a clean output stream of matching file names. `head` receives a continuous and uninterrupted stream of matching filenames correctly displaying the first 5 lines from this stream.
    
    <div data-node-type="callout">
    <div data-node-type="callout-emoji">💡</div>
    <div data-node-type="callout-text">The search with <code>-name</code> option is case-sensitive. To make the search case-insensitive, use the <code>-iname</code> option instead.</div>
    </div>
    
2. To search for files of a specific ***type***, use the `-type` option:
    
    ```bash
    sudo find /snap name "cat" -type f
    ```
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719730540096/f19122af-1c3f-4852-a486-fbc0f02535ed.png align="center")
    
    To search for *both* files and directories simultaneously, use `-o` operator which stands for OR operator:
    
    ```bash
    sudo find / -name "vagrant" \( -type f -o -type d\) -exec ls -ld {} \;
    ```
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719732504912/825182d9-7e74-4c0a-b613-579dffa7bab5.png align="center")
    
3. To find files of a specific ***size***, use the `-size` option:
    
    ```bash
    sudo find /var/log -size 100c
    sudo find /var/log -size -1K
    sudo find /var/log -size +100M
    sudo find /var/log -size 2G
    ```
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719842107986/a3877318-6d3d-478b-9123-a59170ee229f.png align="center")
    
4. To search for files based on their modification, access, and change times use `-mtime`, `-atime`, and `-ctime` options:
    
    ```bash
    find /path/to/search -mtime +n
    find /path/to/search -atime -n
    find /path/to/search -ctime n
    ```
    
    * `-mtime` - Modification time, when the file content was last modified;
        
    * `-atime` - Access time, when the file was last accessed;
        
    * `-ctime` - Change time, when the file's metadata or content was last changed;
        
    * `+n` - more than `n` days ago;
        
    * `-n` - in the last `n` days;
        
    * `n` - exactly `n` days ago;
        
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719845638270/5f257499-d141-4158-81a4-2f2e6fdffa3b.png align="center")
    
5. To find all `.conf` files in `/etc` and copy them to `~/backup/etc`, use a new option `-exec` which executes a command against all files found in the result.
    
    ```bash
    sudo find /etc -type f -name "*.conf" -exec cp {} ~/backup/etc/ \;
    ```
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719848187060/e45aa2e9-2042-488d-9cd1-9c61a8e4740a.png align="center")
    

* `-exec` executes the `cp` command against every item in the result;
    
* `{}` - a placeholder for the actual item itself. So every time a `find` command finds something, it puts the result here;
    
* `\;` terminates the `find` command. You can also use `+` instead of `\;` sign as a terminator. You should terminate the `find` command anytime you use the `-exec` option.
    

6. To change the permissions of several files to be readable and writable simultaneously, type:
    
    ```bash
    find ~ -type f -name "*.txt" -exec chmod 600 {} \;
    find ~ -name "*.txt" -exec ls -ld {} +
    ```
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719852048206/6c0c5810-bd9f-407f-b41a-71235dcec0f1.png align="center")
    

## Best Practices

1. **Use Quotes for Patterns**: Always use quotes around patterns to avoid shell interpretation.
    
2. **Combine Expressions**: Use multiple expressions to narrow down your search.
    
3. **Test Before Executing**: Use `-print` to test your `find` command before adding `-exec`.
    

## References:

1. [How to use FIND in Linux](https://opensource.com/article/18/4/how-use-find-linux)
    
2. [find(1) — Linux manual page](https://www.man7.org/linux/man-pages/man1/find.1.html)
    
3. [10 ways to use the Linux find command](https://www.redhat.com/sysadmin/linux-find-command)
    
4. [Linux Crash Course - The find command](https://www.youtube.com/watch?v=skTiK_6DdqU)\-
