Skip to content

Latest commit

 

History

History
173 lines (117 loc) · 3.59 KB

find.md

File metadata and controls

173 lines (117 loc) · 3.59 KB

find

The find command is used to search for files and directories within a directory hierarchy in Linux.

Basic Usage

  • Syntax:

    find [path] [options] [expression]
  • Examples:

    find /home/user -name "file.txt"
    • Searches for a file named file.txt in the /home/user directory and its subdirectories.
    find /var/log -type f -name "*.log"
    • Finds all files with a .log extension in the /var/log directory.
    find / -type d -name "backup"
    • Searches for directories named backup starting from the root directory.
    find . -mtime -7
    • Finds files modified in the last 7 days in the current directory.
    find /home/user -size +100M
    • Finds files larger than 100 MB in /home/user.

Common Options

  • -name [pattern]:

    • Searches for files or directories that match the specified name or pattern.
    find . -name "*.txt"
    • Finds all .txt files in the current directory.
  • -type [d/f]:

    • Specifies the type of item to search for:
      • d for directories
      • f for files
    find / -type d -name "config"
    • Finds directories named config.
  • -size [N]:

    • Finds files of a specific size.
      • +N for files larger than N units.
      • -N for files smaller than N units.
      • N for files exactly N units.
    find . -size +10M
    • Finds files larger than 10 MB.
  • -mtime [N]:

    • Finds files modified N days ago.
      • +N for files modified more than N days ago.
      • -N for files modified less than N days ago.
      • N for files modified exactly N days ago.
    find . -mtime -1
    • Finds files modified in the last 24 hours.
  • -exec [command] {} \;:

    • Executes a command on each file found.
    find . -name "*.log" -exec rm {} \;
    • Deletes all .log files in the current directory and its subdirectories.
  • -perm [mode]:

    • Finds files with specific permissions.
    find . -perm 644
    • Finds files with 644 permissions.
  • -user [username]:

    • Finds files owned by a specific user.
    find /home -user alice
    • Finds all files owned by alice in /home.
  • -group [groupname]:

    • Finds files belonging to a specific group.
    find /data -group admins
    • Finds all files owned by the admins group in /data.
  • -inum [inode number]:

    • Finds files with a specific inode number.
    find / -inum 123456
    • Finds the file with inode number 123456.
  • --help:

    • Displays help information about the find command.
    find --help
    • Shows usage information and options.

Quick Tips

  • Search from Current Directory:

    • Running find with . as the path starts the search from the current directory.
    find . -name "*.conf"
  • Combining Multiple Conditions:

    • You can combine multiple conditions using -and (default) or -or.
    find / -name "*.conf" -or -name "*.log"
  • Exclude Certain Paths:

    • Use -prune to exclude directories from the search.
    find / -path "/proc" -prune -or -name "*.log"
  • Speed Up Searches:

    • To speed up searches, specify a narrower path or use more specific options.

Summary

The find command is a versatile and powerful tool for searching files and directories in Linux. It supports various options to filter results by name, type, size, modification time, permissions, and more. Use it to efficiently locate files, perform bulk operations, or analyze the filesystem.