Skip to main content

Linux find Command Explained

Introduction

find is a very powerful command-line tool in Linux used to search for files and directories in a directory tree. It can search based on various criteria (such as name, size, time, etc.) and supports performing complex operations.

Basic Usage

The basic syntax of the find command is:

find [search path] [matching criteria] [action]
  • Search path: Specifies the directory path to search. If not specified, the current directory is used by default.
  • Matching criteria: Specifies the conditions for searching files or directories.
  • Action: Specifies the operation to perform on matched files or directories.

For example:

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

This command will search for a file named test.txt in the /home/user directory and its subdirectories.

Common Options

Find Files by Name

  • -name: Find files by name, case-sensitive.
  • -iname: Find files by name, case-insensitive.
find /path/to/search -name "filename"
find /path/to/search -iname "filename"

Find Files by Type

  • -type: Find files by type.

Common types:

  • f: Regular file
  • d: Directory
  • l: Symbolic link
find /path/to/search -type f
find /path/to/search -type d

Find Files by Size

  • -size: Find files by size.

Size units:

  • c: Bytes
  • k: Kilobytes
  • M: Megabytes
  • G: Gigabytes
find /path/to/search -size +10M  # Find files larger than 10MB
find /path/to/search -size -1G # Find files smaller than 1GB

Find Files by Time

  • -atime: Find by access time.
  • -mtime: Find by modification time.
  • -ctime: Find by status change time.

Time units:

  • n: n days ago
  • +n: More than n days ago
  • -n: Within the last n days
find /path/to/search -atime -7  # Find files accessed within the last 7 days
find /path/to/search -mtime +30 # Find files modified more than 30 days ago

Find Files by Permissions

  • -perm: Find by permissions.
find /path/to/search -perm 644  # Find files with permissions 644
find /path/to/search -perm /u+x # Find files where the user has execute permission

Advanced Usage

Combining with Other Commands

The find command can be combined with other commands using the -exec option.

find /path/to/search -name "*.log" -exec rm {} \;  # Find and delete all .log files

Excluding Specific Directories

Use the -prune option to exclude specific directories.

find /path/to/search -path "/path/to/search/exclude_dir" -prune -o -name "*.txt" -print

Finding and Performing Actions

Use -exec or -ok to perform actions on found files. -exec executes the command directly, while -ok prompts the user for confirmation before execution.

find /path/to/search -type f -name "*.sh" -exec chmod +x {} \;  # Find and grant execute permission
find /path/to/search -type f -name "*.sh" -ok chmod +x {} \; # Find and confirm before execution

Summary

The find command is a very powerful tool in Linux systems, capable of searching for files and directories in a directory tree based on multiple criteria, and can be combined with other commands to perform complex operations. Mastering the find command can greatly improve your work efficiency.