find

file managementlinux
The find command is one of the most frequently used commands in Linux/Unix-like operating systems. find The find command is used to search for files and directories in a directory hierarchy based on various criteria. It's one of the most powerful and flexible commands for locating files in a Linux/Unix system.

Quick Reference

Command Name:

find

Category:

file management

Platform:

linux

Basic Usage:

find . -name "*.txt"

Common Use Cases

    Syntax

    find [starting-point...] [expression]

    Options

    Option Description
    -name pattern Match filenames against the shell pattern (case-sensitive)
    -iname pattern Like -name, but case-insensitive
    -type c File is of type c: f (regular file), d (directory), l (symbolic link), etc.
    -size n[cwbkMG] File uses n units of space (c: bytes, w: 2-byte words, b: 512-byte blocks, k: kilobytes, M: megabytes, G: gigabytes)
    -mtime n File's data was last modified n*24 hours ago. Use -n for less than n days, +n for more than n days
    -mmin n File's data was last modified n minutes ago. Use -n for less than n minutes, +n for more than n minutes
    -atime n File was last accessed n*24 hours ago
    -ctime n File's status was last changed n*24 hours ago
    -user name File is owned by user 'name'
    -group name File belongs to group 'name'
    -perm mode File's permission exactly matches the octal number mode
    -perm -mode All permission bits in mode are set for the file
    -perm /mode Any of the permission bits in mode are set for the file
    -exec command {} \; Execute command on each found file/directory. {} is replaced by the current file name
    -exec command {} \+ Like -exec, but pass multiple files to command at once, making it more efficient
    -delete Delete files (use with caution!)
    -path pattern File path matches pattern
    -not expr Negate the expression
    expr1 -a expr2 Both expr1 and expr2 are true (AND)
    expr1 -o expr2 Either expr1 or expr2 is true (OR)
    -maxdepth n Descend at most n directory levels
    -mindepth n Only process files and directories at least n levels deep
    -newer file File was modified more recently than reference file
    -empty File or directory is empty

    Examples

    How to Use These Examples

    The examples below show common ways to use the find command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

    #

    Basic Examples:

    find /home -name "*.txt"

    Searches for all .txt files in the /home directory and its subdirectories. The -name option specifies the filename pattern to search for using wildcards.

    find . -type f -mtime -7

    Finds all files (not directories) modified in the last 7 days in the current directory and its subdirectories. The -type f option restricts the search to regular files, and -mtime -7 specifies files modified less than 7 days ago.

    find /var/log -type f -size +10M

    Locates all files larger than 10 megabytes in the /var/log directory. The -size +10M option specifies files whose size is greater than 10 megabytes.

    find . -type d -name "src"

    Finds all directories named "src" in the current directory and its subdirectories. The -type d option restricts the search to directories only.

    Advanced Examples:

    find . -type f -name "*.log" -exec grep "error" {} \;

    Searches for all .log files in the current directory and subdirectories, then executes the grep command on each file to find lines containing "error". The -exec option allows you to run commands on each file found, with {} representing the current file.

    find . -type f -name "*.jpg" -size +1M -exec cp {} /backup/ \;

    Finds all JPEG files larger than 1 megabyte and copies them to the /backup/ directory. This combines multiple criteria (-name, -size) with an action (-exec).

    find /tmp -type f -user username -mtime +30 -delete

    Deletes all files in /tmp owned by "username" that haven't been modified in more than 30 days. The -user option specifies the file owner, and -delete removes the files instead of just listing them.

    find . -type f -name "*.txt" -o -name "*.log"

    Searches for files with either .txt or .log extensions. The -o (or) operator combines multiple search conditions.

    find /var -type d -perm -755 -not -path "*/proc/*"

    Finds directories with permission 755 (or more permissive) in /var, excluding paths containing "proc". The -perm option checks file permissions, and -not -path excludes certain paths from the search.

    find . -type f -newer reference.txt

    Finds files that were modified more recently than reference.txt. The -newer option compares modification times with a reference file.

    Try It Yourself

    Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

    Understanding Syntax

    Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

    Notes

    How 'find' Works:

    • The find command searches through a directory hierarchy starting from the specified path(s)
    • For every file or directory it encounters, it applies tests specified by the options
    • Files matching all criteria are then displayed or operated on using -exec
    • Without any options, find lists all files in the current directory and its subdirectories

    Common Use Cases:

    • Locating files by name, size, or type
    • Finding and cleaning up old, large, or temporary files
    • Searching for files modified during specific time periods
    • Finding files with specific permissions or owners
    • Executing operations on groups of files matching certain criteria
    • Creating complex file search patterns with AND, OR, and NOT logic

    Performance Tips:

    • Use -type early in your command to narrow down the search
    • Limit search depth with -maxdepth when you don't need to search deeply
    • Avoid searching network-mounted filesystems unless necessary
    • Use -exec cmd {} \+ instead of -exec cmd {} \; when possible for better performance
    • Combine find with xargs for more efficient processing of large result sets
    • Consider using locate for simple name-based searches that don't need real-time results

    Safety Considerations:

    • Always test find commands without destructive actions (-delete, -exec rm) first
    • Be extremely careful with -delete option, especially with root privileges
    • Use absolute paths when specifying starting points to avoid ambiguity
    • Quote patterns containing wildcards to prevent shell expansion
    • When using -exec with rm, consider adding the -i option for confirmation prompts

    Related Commands:

    • locate - Find files by name quickly using a pre-built database
    • grep - Search for patterns in file contents
    • xargs - Build and execute commands from standard input
    • ls - List directory contents
    • rm - Remove files or directories

    Related Commands

    locate

    locate

    View command

    grep

    grep

    View command

    xargs

    xargs

    View command

    ls

    ls

    View command

    rm

    rm

    View command

    Learn By Doing

    The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the find command works in different scenarios.

    $ find
    View All Commands