Skip to main content

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

Example: Find files with a specific extension

find /home -name "*.log"

Example: Find files with a specific name pattern

locate "config*.yaml"
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

Example: Find all .txt files in the home directory

find ~ -type f -name "*.txt"

Example: Find all directories named "backup"

find /home -type d -name "backup"
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

Example: Find files larger than 50MB

find /home -size +50M

Example: Find files smaller than 1KB

find /tmp -size -1k

Example: Find files exactly 100MB

find /data -size 100M
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

Example: Find files modified in the last 24 hours

find /path/to/search -type f -mtime -1

Example: Find files modified more than 7 days ago

find /home/user -type f -mtime +7

Example: Find files modified exactly 5 minutes ago

find /tmp -type f -mmin 5
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

Example: Find files with read permission for owner

find /home -type f -perm 600

Example: Find files with execute permission for group

find /var -type f -perm 010

Example: Find directories with no write permission for others

find /etc -type d -perm 755
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

Example: Execute command on all .txt files

find /home/user -name "*.txt" -exec rm {} \;
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.

Example: Rename all .log files to .old

find /var/log -name "*.log" -exec mv {} {}.old \;

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

Example: Find all files named 'config.json'

locate config.json

Example: Case-insensitive search for 'readme'

locate -i readme
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

Example: Finding executable location

which python3
/usr/bin/python3
Note: which only shows the first match in PATH and does not check for aliases or shell built-ins.

Example: Using which with multiple commands

which ls grep
/bin/ls
/bin/grep

9. Finding Files with whereis

Option Values Description
whereis whereis [options] filename Finds binary, source, and manual files for a given command or filename
whereis ls
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

Example: Using grep to find lines matching a pattern

grep "error" /var/log/syslog

Example: Using ripgrep to search recursively in a directory

rg "function" src/ --type=js
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

Example: Complex Find with Multiple Conditions

find /home/user -type f \( -name "*.log" -or -name "*.tmp" \) -and -size +5M -not -path "/home/user/backup/*"

Was this article helpful?