find

find

The find command in Unix and Linux is a powerful utility used to search for files and directories within a specified directory hierarchy. It allows you to locate files based on various criteria such as name, type, size, permissions, and timestamps.

Basic Usage

The basic syntax for the find command is:

find [path...] [expression]
  • path: Specifies the starting directory or directories to search. If not specified, find starts from the current directory.

  • expression: Defines the search criteria and actions to be performed on matched files and directories.

Examples

Finding Files by Name

To find files with a specific name:

find /path/to/search -name "filename.txt"

This command searches /path/to/search and its subdirectories for files named filename.txt.

Finding Directories

To find directories:

find /path/to/search -type d

This command lists all directories under /path/to/search.

Finding Files by Type

To find all regular files (excluding directories and symbolic links):

This command searches for all regular files under /path/to/search.

Finding Files Based on Size

To find files larger than a specific size (e.g., 1MB):

This command searches for files larger than 1MB under /path/to/search.

Finding Files Modified Recently

To find files modified within the last 7 days:

This command searches for files modified in the last 7 days under /path/to/search.

Options and Expressions

-name Option: Match by Name

To match files by name pattern (supports wildcard characters):

This command finds files ending in .log under /path/to/search.

-type Option: Match by Type

To specify the type of file or directory (f for regular files, d for directories):

This command finds directories under /path/to/search.

-size Option: Match by Size

To specify file size in blocks (c), kilobytes (k), megabytes (M), gigabytes (G), or terabytes (T):

This command finds files larger than 1MB under /path/to/search.

-mtime Option: Match by Modification Time

To specify files modified within a certain number of days (+ for older, - for newer):

This command finds files modified in the last 7 days under /path/to/search.

Practical Use Cases

Deleting Old Log Files

To delete log files older than 30 days:

This command finds log files in /var/log older than 30 days and deletes them.

Finding and Copying Files

To find all .txt files and copy them to a new directory:

This command finds all .txt files under /path/to/search and copies them to /path/to/destination.

Summary

The find command is a versatile tool for searching files and directories in Unix and Linux environments. Its ability to filter based on various criteria makes it invaluable for tasks such as file management, cleanup, and automation. Understanding its options and expressions allows you to perform precise searches and operations on files and directories efficiently.

help

man

Last updated