# Cheatsheet & Examples: find

The `find` command is used to search for files and directories within a directory hierarchy, supporting various conditions and actions based on file attributes, metadata, or content.

## Search for files by name  
Example Usage:  
`find /path/to/search -name "filename.txt"`  

What it does:  
Locates files named "filename.txt" starting from the specified directory.  

Command-line Arguments Explained:  
- `/path/to/search`: The directory where the search begins.  
- `-name "filename.txt"`: Matches files with the exact name provided.  

## Search for directories by name  
Example Usage:  
`find /path/to/search -type d -name "foldername"`  

What it does:  
Locates directories named "foldername" starting from the specified directory.  

Command-line Arguments Explained:  
- `-type d`: Filters results to directories only.  
- `-name "foldername"`: Matches directories with the exact name provided.  

## Search for recently modified files (last 24 hours)  
Example Usage:  
`find /path/to/search -mtime -1`  

What it does:  
Finds files modified within the last 24 hours.  

Command-line Arguments Explained:  
- `-mtime -1`: Matches files modified less than 1 day ago.  
- `-1` indicates "within the last day"; use `+1` for "more than 1 day ago."  

## Search for files larger than a specific size  
Example Usage:  
`find /path/to/search -size +1M`  

What it does:  
Finds files larger than 1 megabyte.  

Command-line Arguments Explained:  
- `-size +1M`: Matches files larger than 1MB.  
- `M` stands for megabytes; use `k` for kilobytes or `G` for gigabytes.  

## Search for files with specific permissions  
Example Usage:  
`find /path/to/search -perm -u+x`  

What it does:  
Finds files with execute permission for the user (owner).  

Command-line Arguments Explained:  
- `-perm -u+x`: Checks if the user has execute permission.  
- The `-` prefix means "all bits in the mode are set"; `+` means "any bit is set."  

## Search for files by owner  
Example Usage:  
`find /path/to/search -user username`  

What it does:  
Identifies files owned by the specified user.  

Command-line Arguments Explained:  
- `-user username`: Matches files owned by the user "username."  

## Search for empty files or directories  
Example Usage:  
`find /path/to/search -empty`  

What it does:  
Locates empty files (zero-size) or empty directories.  

Command-line Arguments Explained:  
- `-empty`: Matches files or directories with no content.  

## Combine multiple search criteria  
Example Usage:  
`find /path/to/search -name "*.txt" -and -size +1k`  

What it does:  
Finds text files larger than 1 kilobyte.  

Command-line Arguments Explained:  
- `-name "*.txt"`: Matches files ending with ".txt".  
- `-and`: Combines conditions; replaces the default logical AND.  

## Execute a command on found files  
Example Usage:  
`find /path/to/search -name "*.log" -exec rm {} \;`  

What it does:  
Removes all `.log` files found in the specified directory.  

Command-line Arguments Explained:  
- `-exec rm {} \;`: Executes the `rm` command on each matched file.  
- `{}` is a placeholder for the file path; `\;` terminates the command.  

## Search for files within a specific depth  
Example Usage:  
`find /path/to/search -maxdepth 2 -name "*.py"`  

What it does:  
Finds Python files up to two levels deep in the directory tree.  

Command-line Arguments Explained:  
- `-maxdepth 2`: Limits the search to two levels of subdirectories.  
- `-name "*.py"`: Matches files ending with ".py".  

## Search for files with a specific inode number  
Example Usage:  
`find /path/to/search -inum 12345`  

What it does:  
Finds files with the inode number 12345.  

Command-line Arguments Explained:  
- `-inum 12345`: Matches files by their inode number.  

## Search for files accessed in the last 7 days  
Example Usage:  
`find /path/to/search -atime -7`  

What it does:  
Identifies files accessed within the last 7 days.  

Command-line Arguments Explained:  
- `-atime -7`: Matches files accessed less than 7 days ago.  
- `-atime +7` would match files accessed more than 7 days ago.  

## Search for files by their file type (e.g., symbolic links)  
Example Usage:  
`find /path/to/search -type l`  

What it does:  
Locates symbolic links (soft links) in the specified directory.  

Command-line Arguments Explained:  
- `-type l`: Matches symbolic links.  
- Use `f` for regular files, `d` for directories, `s` for sockets, etc.  

## Search for files with wildcards in names  
Example Usage:  
`find /path/to/search -name "report*"`  

What it does:  
Finds files starting with "report" (e.g., "report1.txt", "report_final.pdf").  

Command-line Arguments Explained:  
- `-name "report*"`: Matches names beginning with "report" using a wildcard.  

## Search for files modified after a specific date  
Example Usage:  
`find /path/to/search -newermt "2023-10-01"`  

What it does:  
Finds files modified after October 1, 2023.  

Command-line Arguments Explained:  
- `-newermt "2023-10-01"`: Compares modification time to the given date.  
- `mt` specifies the modification time; use `at` for access time.  

## Search for files with case-insensitive names  
Example Usage:  
`find /path/to/search -iname "report.txt"`  

What it does:  
Finds files named "report.txt" regardless of case (e.g., "Report.txt", "REPORT.TXT").  

Command-line Arguments Explained:  
- `-iname "report.txt"`: Case-insensitive name match.  
- `-name` is case-sensitive; `-iname` ignores case.  

## Delete found files instantly  
Example Usage:  
`find /path/to/search -name "*.tmp" -delete`  

What it does:  
Deletes all `.tmp` files found in the specified directory.  

Command-line Arguments Explained:  
- `-delete`: Removes matched files immediately.  
- Must be the last option in the command; cannot be combined with `-exec`.  

## Search for files matching a regular expression  
Example Usage:  
`find /path/to/search -regex ".*\.log$"`  

What it does:  
Finds files matching the regular expression (e.g., files ending with ".log").  

Command-line Arguments Explained:  
- `-regex ".*\.log$"`: Applies a regex pattern to file paths.  
- Use `.*` for any characters and `$` to match the end of a filename.  

## Search for files not matching a condition  
Example Usage:  
`find /path/to/search -not -name "*.txt"`  

What it does:  
Lists all files except those ending with ".txt".  

Command-line Arguments Explained:  
- `-not`: Negates the preceding condition.  
- `-name "*.txt"`: Matches specific files; `-not` excludes them.  

## Search for files by file group  
Example Usage:  
`find /path/to/search -group developers`  

What it does:  
Identifies files belonging to the "developers" group.  

Command-line Arguments Explained:  
- `-group developers`: Matches files with the specified group ownership.
