DEV Community

omkar shelke
omkar shelke

Posted on

Using the find Command to Search for Directories and Files in Linux

Using the find Command to Search for Directories and Files in Linux

The find command in Linux is an essential tool for searching and locating files and directories based on various criteria. Here, we will cover how to use the find command effectively to search for both files and directories, along with some practical examples.

Basic Syntax

find [starting-point] [expression]
Enter fullscreen mode Exit fullscreen mode
  • starting-point: The directory to start the search from (e.g., / for the root directory).
  • expression: Criteria for searching (e.g., name, type, size).

Finding Files

To search for files, you can use the -type f option.

Example: Finding Files Named index.html

find / -type f -name "index.html" 2>/dev/null
Enter fullscreen mode Exit fullscreen mode
  • -type f: Specifies that we are looking for files.
  • -name "index.html": Searches for files with the name index.html.
  • 2>/dev/null: Redirects any error messages (like "Permission denied") to /dev/null, effectively silencing them.

Finding Directories

To search for directories, use the -type d option.

Example: Finding Directories Named index.html

find / -type d -name "index.html" 2>/dev/null
Enter fullscreen mode Exit fullscreen mode
  • -type d: Specifies that we are looking for directories.

Finding Any Type

If you want to search for both files and directories, omit the -type option.

Example: Finding Files or Directories Named index.html

find / -name "index.html" 2>/dev/null
Enter fullscreen mode Exit fullscreen mode

Additional Useful Examples

Finding Files with a Specific Extension

To find all files with the .html extension:

find / -type f -name "*.html" 2>/dev/null
Enter fullscreen mode Exit fullscreen mode

Finding Empty Files

To find all empty files:

find / -type f -empty 2>/dev/null
Enter fullscreen mode Exit fullscreen mode

Finding Files Modified in the Last 7 Days

To find files modified in the last 7 days:

find / -type f -mtime -7 2>/dev/null
Enter fullscreen mode Exit fullscreen mode
  • -mtime -7: Finds files modified in the last 7 days.

Finding Files Larger Than 100MB

To find files larger than 100MB:

find / -type f -size +100M 2>/dev/null
Enter fullscreen mode Exit fullscreen mode
  • -size +100M: Finds files larger than 100 megabytes.

Combining Criteria

You can combine multiple criteria to narrow down your search.

Example: Finding .html Files Larger Than 1MB in /var/www

find /var/www -type f -name "*.html" -size +1M 2>/dev/null
Enter fullscreen mode Exit fullscreen mode

Summary

The find command is a powerful and flexible tool that allows you to search for files and directories based on a wide range of criteria. By mastering this command, you can efficiently locate and manage files and directories in your Linux system. Here’s a quick reference for the commands covered:

  • Find files named index.html:
  find / -type f -name "index.html" 2>/dev/null
Enter fullscreen mode Exit fullscreen mode
  • Find directories named index.html:
  find / -type d -name "index.html" 2>/dev/null
Enter fullscreen mode Exit fullscreen mode
  • Find files or directories named index.html:
  find / -name "index.html" 2>/dev/null
Enter fullscreen mode Exit fullscreen mode
  • Find all .html files:
  find / -type f -name "*.html" 2>/dev/null
Enter fullscreen mode Exit fullscreen mode
  • Find empty files:
  find / -type f -empty 2>/dev/null
Enter fullscreen mode Exit fullscreen mode
  • Find files modified in the last 7 days:
  find / -type f -mtime -7 2>/dev/null
Enter fullscreen mode Exit fullscreen mode
  • Find files larger than 100MB:
  find / -type f -size +100M 2>/dev/null
Enter fullscreen mode Exit fullscreen mode
  • Find .html files larger than 1MB in /var/www:
  find /var/www -type f -name "*.html" -size +1M 2>/dev/null
Enter fullscreen mode Exit fullscreen mode

Top comments (0)