Finding Files
find, locate, which, whereis — searching the filesystem by name and content
Skills taught: linux.find, linux.which, linux.locate, linux.find-exec
find — search by name, type, size, time
# Find by name
find . -name "*.json" # all .json files from current dir
find /var/log -name "*.log" # all .log files in /var/log
find . -name "index.html" -type f # only files, not dirs named index.html
find . -type d -name "node_modules" # find node_modules directories
# Case-insensitive
find . -iname "readme*" # matches README.md, readme.txt, Readme
# By size
find . -size +100M # files larger than 100MB
find . -size -1k # files smaller than 1KB
# By modification time
find . -mtime -7 # modified in last 7 days
find . -mtime +30 # not modified in 30 days
# Execute a command on each found file
find . -name "*.log" -exec rm {} \; # delete all .log files
find . -name "*.sh" -exec chmod +x {} \; # make all .sh executable
find . -name "*.png" -exec ls -lh {} \; # show sizes of all PNGs
# Exclude directories
find . -name "*.js" -not -path "*/node_modules/*"
which, whereis — locating commands
# Find where a command is installed
which python3 # /usr/bin/python3
which node # /usr/local/bin/node
which git # /usr/bin/git
# whereis: find binary, source, and man pages
whereis python3 # python3: /usr/bin/python3 /usr/lib/python3 ...
# type: shows what a command is (builtin, function, alias, or path)
type cd # cd is a shell builtin
type ls # ls is aliased to 'ls --color=auto'
type python3 # python3 is /usr/bin/python3
Tip: find is powerful but slow for large filesystems. locate uses a pre-built database and is much faster for searching by filename: locate '*.conf'. Update the database with: sudo updatedb. Use find for fresh results and complex criteria.
Frequently Asked Questions
How do I search for text inside files?
Use grep, not find. find locates files by metadata (name, size, date). grep searches file contents: grep -r 'TODO' ./src/ searches all files in src/ for the text 'TODO'. Combine them: find . -name '*.js' | xargs grep 'console.log' to grep only JS files.