in

40 Best Examples of Find Command in Linux

default image

The Linux find command is an incredibly useful tool for locating files and directories on your system. With find, you can search for files by name, size, date modified, permissions, and much more.

In this comprehensive guide, we will provide 40 examples of the find command in Linux. Whether you are a beginner or a seasoned Linux user, this guide will teach you how to master the find command through practical examples.

Overview of Find Command

The basic syntax of find is:

find [path] [options] [expression]

Where:

  • path is the directory to search in. If no path is specified, find will search from the current working directory.

  • options modify the behavior of find, like searching by file size, permissions, etc.

  • expression allows searching by file name, modification time, and other criteria.

Here are some common options for find:

  • -name – Search by file or directory name, supports wildcards.

  • -size – Search by file size.

  • -type – Search by file type – f for file, d for directory.

  • -mtime – Search by modification time.

  • -exec – Execute a command on each matched file.

Let‘s look at some examples of using these options.

1. Find Files by Name

To find files by name, use the -name option:

find . -name "*.txt"

This will find all files with a .txt extension in the current directory and subdirectories.

You can also search for partial names with wildcards:

find /home -name "*doc*"

This will find files like mydoc.txt or document.pdf in /home.

To make the search case-insensitive, use -iname instead of -name.

2. Find Files by Size

To find files above or below a certain size, use the -size option.

For example, to find all files over 1 megabyte:

find . -size +1M

Common size units are:

  • c – bytes
  • k – kilobytes
  • M – megabytes
  • G – gigabytes

Some other size examples:

# Find files over 10MB
find / -size +10M

# Find files smaller than 1KB
find . -size -1k 

# Find files between 100KB and 200KB
find . -size +100k -size -200k

3. Find Files by Type

The -type option allows searching specifically for files or directories.

For example, to find only directories:

find /home -type d

To find only files:

find /var/log -type f -name "*.log"

This will find log files in /var/log and ignore directories.

4. Find Files by Modification Time

To find files based on modification time, use the -mtime option.

For example, to find files modified over 7 days ago:

find . -mtime +7

Or to find files modified within the last day:

find /home -mtime -1

-mtime takes a positive or negative integer value in days.

Some other time examples:

# Files modified exactly 5 days ago
find . -mtime 5 

# Files modified between 10 and 20 days ago
find . -mtime +10 -mtime -20

5. Find Empty Files and Directories

To find empty files and folders in the current directory:

# Empty files
find . -type f -empty

# Empty directories  
find . -type d -empty

This can help clean up unused files or folders taking up disk space.

6. Find Files with Permissions

To find files with specific permissions, use the -perm option.

For example, to find files with 755 permissions:

find . -perm 755

Or to find files with 777 permissions:

find /home -perm 777

You can also find files with at least a specific permission, like executable:

find /usr/bin -perm /a+x

This will find executables in /usr/bin, even if they have additional permissions.

7. Find Files Owned by User/Group

To find files owned by a specific user or group, use -user and -group:

# Find files owned by user ‘john‘
find . -user john

# Find files owned by ‘finance‘ group  
find /accounting -group finance

This can help audit permissions or find user files for backup.

8. Find Files based on Content

To search file content rather than metadata, you can use grep in combination with find:

find . -type f -exec grep -l "exception" {} +

This will find all files in the current directory containing the text "exception".

9. Find Files then Delete Them

To find files then immediately delete them, use the -delete action:

find . -type f -name "*.tmp" -delete

This will find and delete all files with a .tmp extension.

Be very careful with -delete, as you can accidentally remove important files!

10. Find Files and Copy Them

To find files then copy them to a target directory, use -exec cp:

find . -type f -name "*.pdf" -exec cp {} /backups \;

This copies all PDFs in the current directory to /backups.

11. Find Files and Move Them

Similarly, you can find and move files with -exec mv:

find . -type f -name "*.doc" -exec mv {} /docs \;

This moves all Word documents to the /docs directory.

12. Run Commands on Found Files

-exec allows you to run any command on the files find locates, by appending {} as the filename.

For example, to change permissions on found PDFs:

find . -type f -name "*.pdf" -exec chmod 644 {} \;

Or to create a ZIP archive of JPEGs:

find . -type f -name "*.jpg" -exec zip images.zip {} +

The syntax {} + executes the command on multiple files at once for better performance.

13. Find Files Ignoring Case

By default find is case sensitive. Use -iname to make searches case insensitive:

find . -iname "*.JPG"

This will find .jpg and .JPG files.

14. Find Files with Special Characters

If searching for files with spaces or quotes, wrap the name in escaped quotes:

find . -name "*.txt" -or -name \"* file?.txt\"

This will find .txt files along with ones like my file1.txt.

15. Find Files containing a String

To find files containing a specific text string, pipe grep into find:

find . -type f | xargs grep "exception"

This will return all files containing "exception".

16. Find Recently Changed Files

To find files changed within the last hour:

find . -mmin -60 

-mmin searches by last modification time in minutes, allowing you to find recently changed files.

17. Find Files based on Depth

By default find will recurse infinitely deep into subdirectories.

To limit the depth, use -maxdepth. For example:

find . -maxdepth 3 -type f

This will find only files 3 levels deep from the current directory.

18. Find Files in Multiple Paths

To search multiple directories, simply provide multiple start paths:

find /home /usr/share -name "*.conf"

This will find config files ending in .conf in both /home and /usr/share.

19. Find Files excluding a Path

To exclude directories from a find search, use -prune:

find /home -name cache -prune -o -type f -print

This will search /home but skip the cache subdirectory.

20. Find Files older than X Days

A common task is finding files older than a certain number of days.

To find files in /var/log older than 7 days:

find /var/log -type f -mtime +7

Adding -delete will remove the old log files:

find /var/log -type f -mtime +7 -delete

21. Find Files between Date Range

To find files modified between two dates, use the -newer option.

For example, to find files changed between Jan 1 and Jan 15:

find . -type f -newermt "Jan 1" ! -newermt "Jan 15"

-newermt takes a date in Month Day or Month Day Year format.

22. Find Files with Extensions

To find all files with a .doc extension:

find . -type f -name "*.doc"

Or find files with several extensions:

find . -type f \( -name "*.xml" -o -name "*.pdf" \)  

Searching by extension is useful for batch file operations.

23. Find Zero Byte Files

To find empty zero byte files, combine -size and -type:

find . -size 0 -type f

Another method is to use -empty instead of -size 0.

24. Find Files with Specific Permissions

To find files with 755 permissions:

find . -type f -perm 755

Or with at least 755 permissions:

find . -type f -perm -755

Searching permissions is handy for auditing and restricting access.

25. Find Files Changed in Last Hour

To find files modified in the last hour:

find . -mmin -60

-mmin lets you specify the time modified in minutes. Useful for change monitoring.

26. Count Files Found

To get a count of files found instead of listing them:

find . -type f | wc -l

This will print the number of files found. Great for totals and overviews.

27. Find Directories Only

To find only directories matching a condition:

find /home -type d -name "Pictures"

This will match /home/user/Pictures but not files.

To find broken symlinks that point nowhere:

find . -type l -print -delete

The -delete removes any broken symlinks found.

29. Redirect Find Errors to Null

By default find sends errors to standard error. To ignore errors:

find . -name missing 2> /dev/null

This redirects the error output to /dev/null

30. Find Files with Specific SUID/GUID

To find files with the SUID or SGID permission bit set:

# SUID 
find / -perm -4000

# SGID
find / -perm -2000

Ensuring proper SUID/SGID files can improve security.

31. Find New Files

To find files created after a certain date:

find . -type f -newermt "Jan 1"

-newermt lets you specify a date to compare the file modify time against.

32. Find Files Not Matching Pattern

To exclude matching patterns, insert ! before the test:

find /home -type f ! -name "*.txt"

This will find all files in /home except for .txt files.

33. Find Unreadable Files

To find files that you don‘t have read permission for:

find . ! -readable -print

Unreadable files might indicate permission problems or filesystem corruption.

34. Find Files Owned by Root

To find files owned by root:

find / -user root 

Finding root-owned files outside of system directories could indicate a breach.

35. Find World-Writable Files

To find files writable by anyone:

find / -perm -2 -type f

World-writable files can pose a security risk.

36. Find Files Modified in Date Range

To find files modified between two dates:

find . -type f -newermt "Jan 1" ! -newermt "Feb 1" 

This finds files changed between Jan 1 and Feb 1.

37. Find Unreadable Files for User

To find files that you can‘t read due to permissions:

find . ! -readable -print

Useful for troubleshooting permission issues restricting access.

To find symlinks:

find . -type l

The -L option will follow symlinks and match the targets.

39. Find Executables

To find executable binaries:

find / -perm /a+x

Adding -user or -group filters for owner can help audit executables.

40. Find Top Biggest Files

To find files over 50MB and sort by size:

find . -size +50M -print0 | xargs -0 du -h | sort -rh

This prints the top disk usage results over 50MB.


These are just 40 examples of the many uses for the powerful find command in Linux. From locating files by metadata, attributes, and content to taking bulk actions like moving, deleting or modifying, find is one of the most versatile utilities for Linux system administration.

Master these practical examples of the find command, and it will become an indispensable tool in your Linux toolbox. Let us know in the comments your favorite use cases and find recipes!

Written by