DEV Community

Cover image for find command
Chielo Chiamaka
Chielo Chiamaka

Posted on

find command

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

Basic Syntax of find

find [starting_point] [options] [expression]
Enter fullscreen mode Exit fullscreen mode
  • 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.)

1. Find by Exact File Name

find <directory> -name <filename>
Enter fullscreen mode Exit fullscreen mode

Find by Exact File Name

  • <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.

2. Case-Insensitive File Name Search

find <directory> -iname <filename>
Enter fullscreen mode Exit fullscreen mode

Case-Insensitive File Name Search

  • -iname is just like -name but ignores case. So it will match CASEi.txt, Casei.TXT, etc.

3. Find Files by Type

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)

Examples:

To find files only:

find <directory> -type f
Enter fullscreen mode Exit fullscreen mode

To find files only
To find directories only:

find . -type d
Enter fullscreen mode Exit fullscreen mode

This is useful if you're searching inside complex folders.

4. Find Files by Extension

find <directory> -name "*.log"
Enter fullscreen mode Exit fullscreen mode

Find Files by Extension
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
Enter fullscreen mode Exit fullscreen mode

Find Empty Files or Directories<br>

  • Returns empty files or folders
  • Useful for cleanup.

6. Find Files by Size

Find files smaller than 100 MB:

find <directory> -size -500k
Enter fullscreen mode Exit fullscreen mode

Find Files by Size
Find files larger than 10 MB:

find <directory>  -size +10M
Enter fullscreen mode Exit fullscreen mode

To find files between 50MB and 100MB:

find <directory> -size +50M -size -100M
Enter fullscreen mode Exit fullscreen mode

Size units:

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

Examples:

  • -size -500k = smaller than 500 KB
  • -size +1G = larger than 1 GB

7. Find Files by Modification Time

find <directory> -mtime -1
Enter fullscreen mode Exit fullscreen mode

Find Files by Modification Time<br>
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
Enter fullscreen mode Exit fullscreen mode

Find Files Accessed Recently

  • -atime = accessed time.
  • Finds files accessed within the last 2 days.

9. Find Files with Specific Permissions

find <directory> -perm 644
Enter fullscreen mode Exit fullscreen mode

Find Files with Specific Permissions
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

10. Find Files by Owner

find <directory> -user <username>
Enter fullscreen mode Exit fullscreen mode

 Find Files by Owner
Finds files owned by a specific user.

Example:

find /var/log -user john
Enter fullscreen mode Exit fullscreen mode

Useful for checking user activity or auditing.


11. Find and Copy Files Owned by a User

find <directory> -user <username> -exec cp {} <location> \;
Enter fullscreen mode Exit fullscreen mode
  • Finds files owned by a user and copies each one to a backup folder.
  • Replace secteam with your desired backup path.

Image description

12. Find and Delete Files Owned by a User

find <directory> -user <username> -exec rm {} \;
Enter fullscreen mode Exit fullscreen mode
  • Searches for files owned by the user and deletes them. Be cautious, test first using ls instead of rm.

13. Find and Delete Files

find <directory> -name "filename" -exec rm {} \;
Enter fullscreen mode Exit fullscreen mode

Find and Delete Files

  • -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 {} \;
Enter fullscreen mode Exit fullscreen mode

14. Find and List with Full Path

find "$(pwd)" -name "*.sh"
Enter fullscreen mode Exit fullscreen mode
  • $(pwd) gives full path of current directory.
  • Useful to know exact locations of found files.

15. Combine Multiple Conditions

find . -type f -name "*.log" -size +1M
Enter fullscreen mode Exit fullscreen mode
  • Finds all .log files that are regular files and larger than 1 MB.

Conclusion

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

(https://www.linkedin.com/in/chiamaka-chielo?utm_source=share&utm_campaign=share_via&utm_content=profile&utm_medium=android_app)

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

Top comments (0)