| 193 | The find command in UNIX is a command line utility for walking a file hierarchy. It can be used to find files and directories and perform subsequent operations on them. It supports searching by file, folder, name, creation date, modification date, owner and permissions. By using the ‘-exec’ other UNIX commands can be executed on files or folders found. |
| 194 | |
| 195 | `$ find [where to start searching from]` |
| 196 | |
| 197 | 1. Search a file with specific name. |
| 198 | |
| 199 | `$ find ./folder -name sample.txt ` |
| 200 | |
| 201 | 2. Search a file with pattern. |
| 202 | |
| 203 | `$ find ./folder -name *.txt ` |
| 204 | |
| 205 | It will give all files which have ‘.txt’ at the end. |
| 206 | |
| 207 | 3. How to find and delete a file with confirmation. |
| 208 | |
| 209 | `$ find ./folder -name sample.txt -exec rm -i {} \; ` |
| 210 | |
| 211 | 4. Search for empty files and directories. |
| 212 | |
| 213 | `$ find ./folder -empty` |
| 214 | |
| 215 | 5. Search for file with entered permissions. |
| 216 | |
| 217 | `$ find ./folder -perm 664` |
| 218 | |
| 219 | 6. Search text within multiple files. |
| 220 | |
| 221 | `$ find ./ -type f -name "*.txt" -exec grep 'Geek' {} \;` |
| 222 | |
| 223 | |