17 Easy find Command Examples for Linux Beginners

Linux Find Commands

The find command is a powerful tool in Linux used to search for files and directories based on various criteria such as name, type, permissions, and modification time. It allows users to locate files quickly and execute actions on them.

Syntax

find [path] [expression]
  • [path] specifies the directory to search in.
  • [expression] defines search criteria like name, type, permissions, etc.

Common Use Cases and Examples

  1. Find a file by name
  2. Find a file ignoring case
  3. Find and delete files
  4. Find files modified in the last 7 days
  5. Find empty directories
  6. Find executable files
  7. Find files with specific permissions
  8. Find writable, readable, and empty files
  9. Find files with specific user or group permissions
  10. Find files containing specific text
  11. Execute commands on found files
  12. Find files with multiple patterns
  13. Find files with max depth
  14. Find empty directories and files
  15. Ignore permission errors
  16. Find files owned by a user
  17. Find files and directories by name

1. Find a file by name

find /home/user -name "filename.txt"

This searches for filename.txt in /home/user.

2. Find a file ignoring case

find /home/user -iname "filename.txt"

The -iname option makes the search case-insensitive.

3. Find and delete files

find /home/user -type f -name "*.log" -delete

This finds and deletes all .log files in /home/user.

4. Find files modified in the last 7 days

find /home/user -mtime -7

This finds files modified in the last 7 days.

5. Find empty directories

find /home/user -type d -empty

This lists all empty directories in /home/user.

6. Find executable files

find / -executable

Finds all executable files.

7. Find files with specific permissions

find /etc/ -perm 777

This finds files in /etc/ with 777 permissions.

8. Find writable, readable, and empty files

find / -writable # Writable files
find / -readable # Readable files
find / -empty # Empty files

9. Find files with specific user or group permissions

find / -perm -u=w # User write permissions
find / -perm -g=w # Group write permissions
find / -perm -o=w # Others write permissions

10. Find files containing specific text

find /var/log -type f -name "*.log" -exec grep "root" {} \;

Searches .log files in /var/log containing the word “root”.

11. Execute commands on found files

find . -exec date {} \;
find . -exec id {} \;
find . -exec uname {} \;

Executes commands on each found file.

12. Find files with multiple patterns

find / -type f \( -name "*.log" -o -name "*ple" -o -name "*pass" \)

Searches for files matching multiple patterns.

13. Find files with max depth

find / -maxdepth 2 -name "pass*"

Limits search depth to 2 levels.

14. Find empty directories and files

find / -empty -type d # Empty directories
find / -empty -type f # Empty files

15. Ignore permission errors

find / -name "passwd" 2> /dev/null

Suppresses permission errors while searching.

16. Find files owned by a user

find / -user username

Finds all files owned by a specific user.

17. Find files and directories by name

find .
find . -name "user"
find . -name "user*"
find / -name "data" -type f # Files named "data"
find / -name "data" -type d # Directories named "data"

Conclusion

The find command is an essential tool for locating files and directories efficiently. Mastering its options enables users to perform advanced searches, automate tasks, and manage system files effectively.

Related Posts

Leave a Reply