- Регистрация
- 1 Мар 2015
- Сообщения
- 1,481
- Баллы
- 155
Introduction to the find Command in Linux
The find command helps you search for files and directories across your filesystem based on name, type, size, modification time, permissions, and more.
Unlike grep, which searches inside files, find searches for files or directories themselves.
Here’s how to get comfortable with it.
Table of Contents
find [starting_point] [options] [expression]
find <directory> -name <filename>
find <directory> -iname <filename>
In the find command, -type is used to specify the type of file you're looking for.
Common values for -type:
Examples:
To find files only:
find <directory> -type f
To find directories only:
find . -type d
This is useful if you're searching inside complex folders.
4. Find Files by Extension
find <directory> -name "*.log"
This finds all files ending in .log.
You can use -iname "*.log" to do it case-insensitively.
5. Find Empty Files or Directories
find <directory> -empty
Find files smaller than 100 MB:
find <directory> -size -500k
Find files larger than 10 MB:
find <directory> -size +10M
To find files between 50MB and 100MB:
find <directory> -size +50M -size -100M
Size units:
Examples:
find <directory> -mtime -1
Finds files modified in the last 1 day.
-1 means less than 1 day ago.
Use +7 for files older than 7 days.
8. Find Files Accessed Recently
find <directory> -atime -2
find <directory> -perm 644
Finds files with exact permission 755 (owner can read/write & execute, group and others can read & write).
Other modes:
find <directory> -user <username>
Finds files owned by a specific user.
Example:
find /var/log -user john
Useful for checking user activity or auditing.
11. Find and Copy Files Owned by a User
find <directory> -user <username> -exec cp {} <location> \;
12. Find and Delete Files Owned by a User
find <directory> -user <username> -exec rm {} \;
find <directory> -name "filename" -exec rm {} \;
Test safely:
find <directory> -name "filename" -exec ls {} \;
14. Find and List with Full Path
find "$(pwd)" -name "*.sh"
find . -type f -name "*.log" -size +1M
From everyday maintenance to system-wide audits, the find command proves its worth in countless scenarios.
Knowing how to use it efficiently can save you time and prevent costly mistakes.
Start with simple queries, build your confidence, and you'll soon be a find command power user.
Let’s connect on LinkedIn
()
As I automate my journey into RHCE and Ansible, I’d love to connect with fellow learners and professionals. Feel free to reach out and join me as I share tips, resources, and insights throughout this 30-day challenge.
cloudwhistler #30daysLinuxchallenge
The find command helps you search for files and directories across your filesystem based on name, type, size, modification time, permissions, and more.
Unlike grep, which searches inside files, find searches for files or directories themselves.
Here’s how to get comfortable with it.
Table of Contents
- Basic Syntax of find
- 1. Find by Exact File Name
- 2. Case-Insensitive File Name Search
- 3. Find Files by Type
- 4. Find Files by Extension
- 5. Find Empty Files or Directories
- 6. Find Files by Size
- 7. Find Files by Modification Time
- 8. Find Files Accessed Recently
- 9. Find Files with Specific Permissions
- 10. Find Files by Owner
- 11. Find and Copy Files Owned by a User
- 12. Find and Delete Files Owned by a User
- 13. Find and Delete Files
- 14. Find and List with Full Path
- 15. Combine Multiple Conditions
- Conclusion
- Let's Connect on LinkedIn
find [starting_point] [options] [expression]
- starting_point: where the search begins (e.g., . for current directory or /home/user)
- options / expression: what to look for (e.g., -name, -type, etc.)
find <directory> -name <filename>
- <directory> means search from the current directory.
- -name tells find to look for a file with the exact name "notes.txt".
- If it finds the file, it will show you the full path.
find <directory> -iname <filename>
- -iname is just like -name but ignores case. So it will match CASEi.txt, Casei.TXT, etc.
In the find command, -type is used to specify the type of file you're looking for.
Common values for -type:
| Letter | Meaning |
|---|---|
| f | Regular file (e.g., .txt, .log, .sh) |
| d | Directory |
| l | Symbolic link |
| c | Character device file (like /dev/tty) |
| b | Block device file (like /dev/sda) |
To find files only:
find <directory> -type f
To find directories only:
find . -type d
This is useful if you're searching inside complex folders.
4. Find Files by Extension
find <directory> -name "*.log"
This finds all files ending in .log.
You can use -iname "*.log" to do it case-insensitively.
5. Find Empty Files or Directories
find <directory> -empty
- Returns empty files or folders
- Useful for cleanup.
Find files smaller than 100 MB:
find <directory> -size -500k
Find files larger than 10 MB:
find <directory> -size +10M
To find files between 50MB and 100MB:
find <directory> -size +50M -size -100M
Size units:
- c = bytes
- k = kilobytes
- M = megabytes
- G = gigabytes
Examples:
- -size -500k = smaller than 500 KB
- -size +1G = larger than 1 GB
find <directory> -mtime -1
Finds files modified in the last 1 day.
-1 means less than 1 day ago.
Use +7 for files older than 7 days.
8. Find Files Accessed Recently
find <directory> -atime -2
- -atime = accessed time.
- Finds files accessed within the last 2 days.
find <directory> -perm 644
Finds files with exact permission 755 (owner can read/write & execute, group and others can read & write).
Other modes:
- -perm -mode = must have all permission bits
- -perm /mode = match any of the bits
find <directory> -user <username>
Finds files owned by a specific user.
Example:
find /var/log -user john
Useful for checking user activity or auditing.
11. Find and Copy Files Owned by a User
find <directory> -user <username> -exec cp {} <location> \;
- Finds files owned by a user and copies each one to a backup folder.
- Replace secteam with your desired backup path.
12. Find and Delete Files Owned by a User
find <directory> -user <username> -exec rm {} \;
- Searches for files owned by the user and deletes them. Be cautious, test first using ls instead of rm.
find <directory> -name "filename" -exec rm {} \;
- -exec lets you run a command on each found file.
- {} is replaced with the filename.
- \; ends the -exec command.
Test safely:
find <directory> -name "filename" -exec ls {} \;
14. Find and List with Full Path
find "$(pwd)" -name "*.sh"
- $(pwd) gives full path of current directory.
- Useful to know exact locations of found files.
find . -type f -name "*.log" -size +1M
- Finds all .log files that are regular files and larger than 1 MB.
From everyday maintenance to system-wide audits, the find command proves its worth in countless scenarios.
Knowing how to use it efficiently can save you time and prevent costly mistakes.
Start with simple queries, build your confidence, and you'll soon be a find command power user.
Let’s connect on LinkedIn
()
As I automate my journey into RHCE and Ansible, I’d love to connect with fellow learners and professionals. Feel free to reach out and join me as I share tips, resources, and insights throughout this 30-day challenge.
cloudwhistler #30daysLinuxchallenge