Searching Files and Directories¶
Linux provides powerful tools to search for files, directories, commands, and executables. Whether you're locating configuration files, troubleshooting applications, or managing production servers, mastering file search commands is an essential Linux skill.
Learning Path¶
Course Progress
What You'll Learn¶
After completing this lesson, you'll be able to:
- Search files and directories
- Search by name
- Search by type
- Search by size
- Search by owner
- Search by permissions
- Search by modification time
- Locate executables
- Search efficiently in production systems
Prerequisites¶
Before starting this lesson, complete:
- Module 1 – Linux Fundamentals
- Module 2 Lessons 1–5
Why Learn File Searching?¶
Imagine you're working on a production server.
You need to find:
- NGINX configuration
- Docker Compose file
- Kubernetes manifests
- SSH configuration
- Log files
- Recently modified files
Instead of manually browsing thousands of directories, Linux provides commands that can find them in seconds.
Linux Search Commands¶
Linux offers several search utilities.
| Command | Purpose |
|---|---|
| find | Search files and directories |
| locate | Fast filename search |
| which | Find executable in PATH |
| whereis | Locate binary, source and man pages |
| type | Identify command type |
Each command solves a different problem.
The find Command¶
find is the most powerful file search command.
Syntax
Search from current directory:
Search entire filesystem:
Search by File Name¶
Find a file named:
Example output:
Case-Insensitive Search¶
Matches:
Search Directories¶
Output:
Search Regular Files¶
Search Hidden Files¶
Search Using Wildcards¶
Find all text files.
Find all YAML files.
Find shell scripts.
Search by Size¶
Files larger than 100 MB
Files smaller than 10 KB
Exactly 50 MB
Search by Owner¶
Search by Group¶
Search by Permissions¶
Example:
Find executable files.
Search by Modification Time¶
Modified today
Modified within last 7 days
Older than 30 days
Search Empty Files¶
Execute Commands on Search Results¶
Delete log files.
Display file details.
Warning
Always verify search results before using -delete.
The locate Command¶
locate searches a pre-built database.
Example:
Much faster than find.
Update locate Database¶
Ubuntu
Now:
which¶
Find executable location.
Example
Useful for verifying installed commands.
whereis¶
Displays:
- Binary
- Source
- Manual page
Example:
Output
type¶
Identify command type.
Output
Example
Output
Command Comparison¶
| Command | Best For |
|---|---|
| find | Powerful searches |
| locate | Fast filename search |
| which | Executables |
| whereis | Binary + Man Page |
| type | Built-in vs External |
Real Production Examples¶
Find NGINX configuration.
Find Docker Compose files.
Find Kubernetes YAML.
Find large log files.
Find recently modified files.
Find executable shell scripts.
Production Perspective¶
System administrators frequently search for:
- Configuration files
- Log files
- Core dumps
- Backup archives
- SSL certificates
- Shell scripts
- Kubernetes manifests
- Docker files
Knowing how to search efficiently can significantly reduce troubleshooting time.
Hands-on Lab¶
Task 1¶
Create a lab.
Task 2¶
Create files.
Task 3¶
Search YAML.
Task 4¶
Search shell scripts.
Task 5¶
Search files only.
Task 6¶
Search directories.
Task 7¶
Locate Python.
Task 8¶
Find SSH.
Task 9¶
Identify command type.
Command Deep Dive¶
| Command | Purpose | Common Options | Production Example |
|---|---|---|---|
| find | Search filesystem | -name, -type, -size, -mtime | Find configs and logs |
| locate | Fast filename search | -i | Locate SSH configs |
| which | Find executable | Default | Verify installed tools |
| whereis | Binary + docs | Default | Find binaries |
| type | Command type | -a | Check shell built-ins |
Production Troubleshooting Scenario¶
Scenario
A web application has stopped working after a configuration change.
Your tasks:
- Find the NGINX configuration file.
- Locate all backup configuration files.
- Identify recently modified configuration files.
- Find log files larger than 100 MB.
- Verify where the
nginxexecutable is installed.
Example commands:
find /etc -name "nginx.conf"
find /etc -name "*.bak"
find /etc -mtime -1
find /var/log -size +100M
which nginx
Mini Challenge¶
Create the following structure.
project/
├── app.py
├── config/
│ ├── app.yaml
│ ├── db.yaml
│ └── nginx.conf
├── logs/
│ ├── access.log
│ └── error.log
└── scripts/
├── deploy.sh
└── backup.sh
Complete these tasks:
- Find all
.yamlfiles. - Find all shell scripts.
- Find all log files.
- Display only directories.
- Find all files modified today.
- Locate the
bashexecutable.
Best Practices¶
- Use
findfor comprehensive searches. - Use
locatewhen speed is important. - Limit searches to specific directories instead of searching the entire filesystem.
- Verify results before deleting files.
- Combine search criteria for more precise results.
Common Mistakes¶
❌ Searching the entire filesystem unnecessarily.
✅ Use:
This can be slow.
Instead:
❌ Using rm immediately after find.
✅ Always inspect results first.
❌ Forgetting quotes around wildcards.
✅ Correct:
Interview Questions¶
Beginner¶
- What is the difference between
findandlocate? - Which command finds executables?
- How do you search for directories only?
- Which command identifies built-in commands?
Intermediate¶
- Explain
find -exec. - How do you search files modified within the last seven days?
- How do you search by permissions?
- Why is
locatefaster thanfind?
Architect Level¶
- How would you locate configuration files during a production outage?
- How would you identify large log files consuming disk space?
- How can efficient file searching reduce Mean Time to Resolution (MTTR)?
Summary¶
In this lesson, you learned:
- Searching files and directories
- Searching by name, type, size, owner, permissions, and modification time
- Using
find,locate,which,whereis, andtype - Real-world search techniques used in production environments
Mastering these commands will make troubleshooting, automation, and system administration significantly faster and more efficient.
Key Takeaways¶
findis the most powerful Linux search command.locateprovides fast filename searches using an indexed database.whichfinds executables in yourPATH.whereislocates binaries, source files, and manual pages.typeidentifies whether a command is built-in or external.- Always verify search results before performing destructive actions.
What's Next?¶
In the next lesson, you'll learn:
- What wildcards are
- Globbing patterns
*,?, and[]- Character ranges
- Brace expansion
- Real-world file selection techniques
- Using wildcards safely in production