guest@linuxthings:~/guides$ man search
Search & Location
find
find . -name "myfile" # Find file by name in current dir
find . -iname "*.jpg" # Case-insensitive search
find /var/log -mtime -7 # Modified in the last 7 days
find . -mmin -30 # Modified in the last 30 minutes
find . -newer reference.txt # Modified more recently than a file
find . -type d # Find directories only
find . -type f # Find regular files only
find . -type l # Find symbolic links only
find . -size +10M # Find files larger than 10MB
find . -empty # Empty files and directories
find . -maxdepth 1 -name "*.log" # Don't descend into subdirectories
find /home -user smith # Files owned by smith
find . -perm -o+w # Files that anyone can write to
find . -name "*.tmp" -delete # Find and delete filesQuote patterns such as "*.jpg" so the shell passes them to find instead of expanding them itself.
Combining Tests
find . -name "*.jpg" -o -name "*.png" # OR
find . -type f ! -name "*.txt" # NOT
find . \( -name "*.c" -o -name "*.h" \) -size +100k # GroupingActing on Results
find . -name "*.sh" -exec chmod +x {} \; # Run a command once per file
find . -name "*.log" -exec gzip {} + # Pass many files per command (faster)
find . -name "*.bak" -ok rm {} \; # Ask before each command
find . -type f -print0 | xargs -0 grep -l "TODO" # Safe with spaces in namesxargs turns a list of names into command arguments. Always pair find -print0 with xargs -0 so filenames containing spaces are not split in two. Test with a harmless command such as ls before swapping in rm.
locate
locate searches a prebuilt index instead of the disk, so it is very fast but only as current as the last index update. Many systems ship the newer plocate implementation under the same command name.
locate myfile
locate -i readme # Case-insensitive
locate -r '\.conf$' # Match a regular expression
sudo updatedb # Update the database manuallyExecutable Location
which python # Show full path to executable
whereis python # Show binary, source, and man page locations
type python # Identify if command is alias, builtin, or file
type -a python # Show every match on your PATH, not just the firsttype is a shell builtin, so it also reports aliases and functions that which cannot see.
Searching File Contents
To find text inside files rather than filenames, use grep -r (see Text Manipulation):
grep -rn "listen" /etc/nginx/ # Recursive, with line numbers
grep -rl "password" . # Only list the matching files