|
1 | | -# The `find` command |
| 1 | +# The `find` Command |
2 | 2 |
|
3 | | -The `find` command lets you **search for files in a directory hierarchy** |
| 3 | +The `find` command is one of the most powerful Linux utilities that lets you search for files and directories based on various conditions like name, size, modification time, permissions, and more. |
4 | 4 |
|
5 | | -- Search a file with specific name. |
6 | | -- Search a file with pattern |
7 | | -- Search for empty files and directories. |
| 5 | +## Basic Syntax |
8 | 6 |
|
| 7 | +``` |
| 8 | +find [path] [options] [expression] |
| 9 | +``` |
| 10 | + |
| 11 | +**In simple words:** |
| 12 | +> “Search starting from `[path]`, apply `[options or filters]`, and then perform an `[action]` on the result.” |
9 | 13 |
|
10 | | -### Examples: |
| 14 | +Example: |
11 | 15 |
|
12 | | -1. Search a file with specific name: |
| 16 | +``` |
| 17 | +find /home/user -name "*.log" |
| 18 | +``` |
| 19 | +This searches for all `.log` files under `/home/user`. |
13 | 20 |
|
14 | | -```[linux] |
| 21 | +## Common Use Cases |
| 22 | + |
| 23 | +### 1. Search a File by Exact Name |
| 24 | + |
| 25 | +``` |
15 | 26 | find ./directory1 -name sample.txt |
16 | 27 | ``` |
17 | 28 |
|
18 | | -2. Search a file with pattern: |
| 29 | +Finds `sample.txt` inside `directory1` and all its subdirectories. |
| 30 | + |
| 31 | +Case-insensitive search: |
| 32 | + |
| 33 | +``` |
| 34 | +find ./directory1 -iname "sample.txt" |
| 35 | +``` |
| 36 | + |
| 37 | +### 2. Search Files by Pattern (Wildcard) |
| 38 | + |
| 39 | +Find all `.txt` files under `directory1`. |
19 | 40 |
|
20 | | -```[linux] |
21 | | -find ./directory1 -name '*.txt' |
| 41 | +``` |
| 42 | +find ./directory1 -name "*.txt" |
| 43 | +``` |
| 44 | + |
| 45 | +More examples: |
| 46 | +``` |
| 47 | +find /var/log -name "*.log" |
| 48 | +find /etc -name "conf*" |
22 | 49 | ``` |
23 | 50 |
|
24 | | -3. To find all directories whose name is test in / directory. |
| 51 | +### 3. Find Directories by Name |
25 | 52 |
|
26 | | -```[linux] |
| 53 | +``` |
27 | 54 | find / -type d -name test |
28 | 55 | ``` |
| 56 | +Lists all directories named `test` from the root `/`. |
| 57 | + |
| 58 | +### 4. Find Empty Files and Directories |
| 59 | + |
| 60 | +``` |
| 61 | +find . -empty |
| 62 | +``` |
| 63 | +Finds both empty files and directories in the current folder. |
| 64 | + |
| 65 | +Only empty files: |
| 66 | +``` |
| 67 | +find . -type f -empty |
| 68 | +``` |
| 69 | + |
| 70 | +### 5. Find Files by Modification or Access Time |
| 71 | + |
| 72 | +| Option | Description | |
| 73 | +|--------|--------------| |
| 74 | +| `-mtime n` | Modified *n* days ago | |
| 75 | +| `-atime n` | Accessed *n* days ago | |
| 76 | +| `-ctime n` | Changed *n* days ago | |
| 77 | +| `-mmin n` | Modified *n* minutes ago | |
| 78 | + |
| 79 | +Examples: |
| 80 | + |
| 81 | +Find files modified within the last 2 days. |
| 82 | +``` |
| 83 | +find /var/log -mtime -2 |
| 84 | +``` |
| 85 | + |
| 86 | +Find files modified within the last hour. |
| 87 | +``` |
| 88 | +find . -mmin -60 |
| 89 | +``` |
| 90 | + |
| 91 | +### 6. Find Files by Size |
| 92 | + |
| 93 | +| Expression | Meaning | |
| 94 | +|-------------|----------| |
| 95 | +| `+n` | Greater than n | |
| 96 | +| `-n` | Less than n | |
| 97 | +| `n` | Exactly n | |
| 98 | + |
| 99 | +Examples: |
| 100 | + |
| 101 | +Find files larger than 100 MB. |
| 102 | +``` |
| 103 | +find / -size +100M |
| 104 | +``` |
| 105 | + |
| 106 | +Find files smaller than 10 KB. |
| 107 | +``` |
| 108 | +find . -size -10k |
| 109 | +``` |
| 110 | + |
| 111 | +### 7. Find Files Modified More Recently than Another File |
| 112 | + |
| 113 | +``` |
| 114 | +find . -newer reference.txt |
| 115 | +``` |
| 116 | +Lists files modified after `reference.txt`. |
| 117 | + |
| 118 | +### 8. Find Files by Type |
| 119 | + |
| 120 | +| Type | Description | |
| 121 | +|------|--------------| |
| 122 | +| `f` | Regular file | |
| 123 | +| `d` | Directory | |
| 124 | +| `l` | Symbolic link | |
| 125 | +| `b` | Block device | |
| 126 | +| `c` | Character device | |
| 127 | + |
| 128 | +Example: |
| 129 | + |
| 130 | +Find all block devices. |
| 131 | +``` |
| 132 | +find /dev -type b |
| 133 | +``` |
29 | 134 |
|
30 | | -4. Searching empty files in current directory |
| 135 | +### 9. Find Files by Permission |
| 136 | +Find files with exactly 644 permissions. |
| 137 | +``` |
| 138 | +find /var/www -type f -perm 644 |
| 139 | +``` |
| 140 | + |
| 141 | +Find files executable by anyone. |
| 142 | +``` |
| 143 | +find /usr -type f -perm /111 |
| 144 | +``` |
| 145 | + |
| 146 | +### 10. Execute Commands on Found Files |
| 147 | + |
| 148 | +You can use the `-exec` option to run a command on each file found: |
| 149 | + |
| 150 | +``` |
| 151 | +find . -type f -name "*.log" -exec rm -f {} \; |
| 152 | +``` |
| 153 | +Deletes all `.log` files under the current directory. |
| 154 | + |
| 155 | +**Alternative (faster):** |
31 | 156 |
|
32 | | -```[linux] |
33 | | -find . -size 0k |
| 157 | +``` |
| 158 | +find . -type f -name "*.log" | xargs rm -f |
34 | 159 | ``` |
35 | 160 |
|
36 | | -### Syntax: |
| 161 | +### 11. Combine Multiple Conditions |
| 162 | + |
| 163 | +You can combine filters together: |
37 | 164 |
|
38 | | -```[linux] |
39 | | -find [options] [paths] [expression] |
40 | 165 | ``` |
41 | | -**In Simple words** |
42 | | -```[linux] |
43 | | -find [where to start searching from] |
44 | | - [expression determines what to find] [-options] [what to find] |
| 166 | +find /var/log -name "*.log" -size +50M -mtime -2 |
45 | 167 | ``` |
| 168 | +Finds `.log` files larger than 50 MB and modified within the last 2 days. |
46 | 169 |
|
47 | | -### Additional Flags and their Functionalities: |
| 170 | +### 12. Print Only File Names (Quiet Output) |
| 171 | +``` |
| 172 | +find /etc -type f -name "*.conf" -print |
| 173 | +``` |
| 174 | +The `-print` flag ensures output is displayed (it’s the default in most systems). |
| 175 | + |
| 176 | +## Quick Reference Table |
48 | 177 |
|
49 | | -Commonly-used primaries include: |
50 | | -- `name` pattern - tests whether the file name matches the shell-glob pattern given. |
51 | | -- `type` type - tests whether the file is a given type. Unix file types accepted include: |
| 178 | +| Task | Command | |
| 179 | +|------|----------| |
| 180 | +| Find files with specific name | `find . -name filename.txt` | |
| 181 | +| Case-insensitive search | `find . -iname filename.txt` | |
| 182 | +| Find directories only | `find . -type d` | |
| 183 | +| Find empty files | `find . -type f -empty` | |
| 184 | +| Find files > 1 GB | `find . -size +1G` | |
| 185 | +| Find modified in last day | `find . -mtime -1` | |
| 186 | +| Delete `.tmp` files | `find . -name "*.tmp" -delete` | |
| 187 | +| Search and execute | `find . -name "*.log" -exec gzip {} \;` | |
52 | 188 |
|
53 | | -| **options** | **Description** | |
54 | | -| :------------- | :-------------------------------------------------------------------------------------------------------- | |
55 | | -| `b` | block device (buffered) | |
56 | | -| `d` | directory | |
57 | | -| `f` | regular file | |
58 | | -| `l` | Symbolic link | |
59 | | -| `-print` | always returns true; prints the name of the current file plus a newline to the stdout. | |
60 | | -| `-mtime n` | find's all the files which are modified n days back. | |
61 | | -| `-atime n` | find's all the files which are accessed 50 days back. | |
62 | | -| `-cmin n` | find's all the files which are modified in the last 1 hour.| |
63 | | -| ` -newer file` | find's file was modified more recently than file.| |
64 | | -| `-size n` | File uses n units of space, rounding up.| |
| 189 | +## Getting Help |
65 | 190 |
|
66 | | -### Help Command |
67 | | -Run below command to view the complete guide to `find` command or [click here](https://en.wikipedia.org/wiki/Find_(Unix)). |
68 | | -```[linux] |
| 191 | +To view the complete guide for the `find` command, run: |
| 192 | + |
| 193 | +``` |
69 | 194 | man find |
70 | 195 | ``` |
0 commit comments