Searching for files and directories in Linux is an essential skill, whether you’re an administrator, developer, or casual user. Linux provides powerful tools like find, locate, and grep to help you quickly and efficiently search for files and their contents. This tutorial covers these commands in detail.
1. find – Searching for Files & Directories
The find command searches for files and directories in a directory hierarchy. It is highly flexible and allows searching by name, type, size, modification time, and more.
Basic Syntax
find [path] [expression]Common find Usage Examples
1.1 Find a file by name
find /home -name "file.txt"- Searches for
file.txtin the/homedirectory and its subdirectories. -namemakes the search case-sensitive.
1.2 Case-insensitive search
find /home -iname "file.txt"-inamemakes the search case-insensitive.
1.3 Find directories
find / -type d -name "Documents"- Searches for directories (
-type d) named “Documents”.
1.4 Find files by extension
find /var/log -type f -name "*.log"- Searches for all log files (
*.log) in/var/log.
1.5 Find files by size
find / -size +100M- Finds files larger than 100MB.
- Use
-size -100Mfor files smaller than 100MB.
1.6 Find files modified in the last 7 days
find /home -mtime -7-mtime -7finds files modified in the last 7 days.-mtime +7finds files older than 7 days.
1.7 Find files accessed in the last 24 hours
find /var/log -atime -1-atime -1finds files accessed in the last day.
1.8 Find and delete files
find /tmp -type f -name "*.tmp" -delete- Deletes all
.tmpfiles in/tmp.
1.9 Find files with specific permissions
find / -type f -perm 644- Searches for files with
644permissions.
2. locate – Fast File Searching
The locate command is much faster than find because it searches a pre-built database instead of scanning the file system.
Installing locate
sudo apt install mlocate # Debian/Ubuntu
sudo yum install mlocate # CentOS/RHELAfter installing, update the database:
sudo updatedbBasic Usage
2.1 Find a file by name
locate file.txt- Searches for
file.txtacross the system.
2.2 Case-insensitive search
locate -i file.txt2.3 Find files with a specific extension
locate "*.log"2.4 Limit search results
locate -n 10 "*.log"- Shows only the first 10 results.
2.5 Find recently modified files
sudo updatedb && locate file.txt- Updates the database before searching (useful if files were recently created or moved).
3. grep – Searching Inside Files
The grep command is used to search for text inside files.
Basic Syntax
grep [options] "pattern" fileCommon grep Usage Examples
3.1 Search for a word in a file
grep "error" /var/log/syslog- Searches for “error” in
/var/log/syslog.
3.2 Case-insensitive search
grep -i "error" /var/log/syslog3.3 Search in multiple files
grep "error" /var/log/*.log- Searches for “error” in all
.logfiles in/var/log.
3.4 Show line numbers
grep -n "error" /var/log/syslog- Displays the line numbers where “error” is found.
3.5 Recursive search (search in directories)
grep -r "error" /var/log- Searches for “error” in all files within
/var/log.
3.6 Invert match (show lines without the pattern)
grep -v "error" /var/log/syslog- Shows lines that do NOT contain “error”.
3.7 Use regex in search
grep -E "error|fail|warn" /var/log/syslog- Searches for “error”, “fail”, or “warn”.
3.8 Count the number of matches
grep -c "error" /var/log/syslog- Displays the number of times “error” appears.
Conclusion
- Use
findfor precise, real-time searches based on file attributes. - Use
locatefor lightning-fast searches using a pre-built database. - Use
grepfor searching inside files for text patterns.
Each tool has its strengths, so use the one that fits your needs best! 🚀