Searching and Finding Files
1. Finding Files by Name
| Command | Description | Examples |
|---|---|---|
| find | Searches for files and directories recursively | find /home -name "filename.txt" |
| locate | Searches a database of file names (faster than find) | locate filename.txt |
| which | Shows the full path of a command | which ls |
| whereis | Searches for executable, source, and manual files | whereis python |
Note: The
locate command requires the database to be updated periodically using updatedb.
2. Finding by Type
| Command | Description | Examples |
|---|---|---|
| find | Searches for files and directories by type, name, size, or other criteria | find /home -type f (all files)find /etc -type d (all directories) |
| -type f | Finds regular files only | find /var/log -type f -name "*.log" |
| -type d | Finds directories only | find /tmp -type d -mtime -7 |
| -type l | Finds symbolic links only | find /usr/bin -type l |
| -type b | Finds block devices (e.g., hard drives) | find /dev -type b |
| -type c | Finds character devices (e.g., serial ports) | find /dev -type c |
| -type p | Finds named pipes (FIFOs) | find /tmp -type p |
| -type s | Finds sockets | find /var/run -type s |
Note: The
-type option is case-sensitive and must be lowercase.
3. Finding by Size
| Command | Description | Examples |
|---|---|---|
| find | Searches for files based on size criteria | find /home -size +100M |
| ls -lh | Lists files with human-readable sizes | ls -lh /var/log |
Note: Size units include
k (kilobytes), M (megabytes), G (gigabytes). Use +N for larger than N, -N for smaller than N, and N for exactly N.4. Finding by Modification Time
| Option | Values | Description |
|---|---|---|
| -mtime | n |
Find files modified exactly n days ago |
| -mtime | +n |
Find files modified more than n days ago |
| -mtime | -n |
Find files modified less than n days ago |
| -mmin | n |
Find files modified exactly n minutes ago |
| -mmin | +n |
Find files modified more than n minutes ago |
| -mmin | -n |
Find files modified less than n minutes ago |
Note: The
-mtime and -mmin options consider the file's modification time, not creation or access time.
5. Finding by Permissions
| Permission | Symbolic Value | Octal Value | Description |
|---|---|---|---|
| read | r |
4 |
Allows reading file contents or listing directory contents |
| write | w |
2 |
Allows modifying file contents or creating/deleting directory entries |
| execute | x |
1 |
Allows executing files as programs or entering directories |
Note: The
-perm option in find can use either symbolic or octal notation. For symbolic, use -perm u=rw,g=rx,o= format. For octal, use -perm 644 where each digit represents owner/group/other permissions.
6. Executing Commands on Results
| Option | Description | Notes |
|---|---|---|
| -exec | Execute a command on each file found | Use \; to terminate the command |
| -execdir | Execute command in the directory of each file found | Useful for avoiding path issues |
| -ok | Execute command with confirmation for each file | Safer than -exec for destructive operations |
Note: The
-exec option can be used with any shell command. It's particularly useful for batch operations like renaming, moving, or deleting files.Warning: Be cautious when using
-exec with destructive commands like rm. Always test the find command first to ensure it matches the intended files.7. Using locate Command
| Option | Values | Description |
|---|---|---|
| locate | locate filename |
Search for files by name in the database |
| locate -i | locate -i filename |
Case-insensitive search |
| locate -r | locate -r pattern |
Search using regex pattern |
| locate -f | locate -f /path/to/file |
Search in a specific directory |
Note: The locate command uses a pre-built database of files. Run
sudo updatedb to update the database if needed.
8. Finding Executables with which
| Option | Values | Description |
|---|---|---|
| which | which [command] |
Shows the full path of (shell) commands |
Note:
which only shows the first match in PATH and does not check for aliases or shell built-ins.
9. Finding Files with whereis
| Option | Values | Description |
|---|---|---|
| whereis | whereis [options] filename |
Finds binary, source, and manual files for a given command or filename |
Note: The
whereis command searches a predefined set of directories and may not find files in non-standard locations.
Summary of whereis Options
-b: Find only the binary files-m: Find only the manual pages-s: Find only the source files-u: Show only files that are not in the standard locations-B: Specify directories to search for binaries
10. Searching File Contents
| Command | Description | Usage Example |
|---|---|---|
| grep | Searches for patterns in files or input streams | grep "pattern" filename.txt |
| egrep | Extended grep with regex support | egrep "pattern|regex" file.txt |
| fgrep | Fixed string grep (no regex) | fgrep "literal" file.txt |
| rg | Fast, recursive grep alternative (ripgrep) | rg "pattern" --type=txt |
Note: The
grep command supports various flags like -i for case-insensitive search, -r for recursive search, and -n to show line numbers.
Warning: Be cautious when using
grep on binary files as it may produce unreadable output.
11. Combining Find Conditions
| Option | Description | Example |
|---|---|---|
| -and | Logical AND operator; combines two conditions | find /path -name "*.txt" -and -size +100k |
| -or | Logical OR operator; matches either condition | find /path -name "file1" -or -name "file2" |
| -not | Logical NOT operator; negates a condition | find /path -not -name "*.tmp" |
| () | Parentheses to group conditions; controls operator precedence | find /path \( -name "*.txt" -or -name "*.log" \) -and -size +10k |