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.txt
in the/home
directory and its subdirectories. -name
makes the search case-sensitive.
1.2 Case-insensitive search
find /home -iname "file.txt"
-iname
makes 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 -100M
for files smaller than 100MB.
1.6 Find files modified in the last 7 days
find /home -mtime -7
-mtime -7
finds files modified in the last 7 days.-mtime +7
finds files older than 7 days.
1.7 Find files accessed in the last 24 hours
find /var/log -atime -1
-atime -1
finds files accessed in the last day.
1.8 Find and delete files
find /tmp -type f -name "*.tmp" -delete
- Deletes all
.tmp
files in/tmp
.
1.9 Find files with specific permissions
find / -type f -perm 644
- Searches for files with
644
permissions.
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/RHEL
After installing, update the database:
sudo updatedb
Basic Usage
2.1 Find a file by name
locate file.txt
- Searches for
file.txt
across the system.
2.2 Case-insensitive search
locate -i file.txt
2.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" file
Common 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/syslog
3.3 Search in multiple files
grep "error" /var/log/*.log
- Searches for “error” in all
.log
files 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
find
for precise, real-time searches based on file attributes. - Use
locate
for lightning-fast searches using a pre-built database. - Use
grep
for searching inside files for text patterns.
Each tool has its strengths, so use the one that fits your needs best! 🚀