Close Menu
    Facebook X (Twitter) Instagram
    Command Linux
    • About
    • How to
      • Q&A
    • OS
      • Windows
      • Arch Linux
    • AI
    • Gaming
      • Easter Eggs
    • Statistics
    • Blog
      • Featured
    • MORE
      • IP Address
      • Man Pages
    • Write For Us
    • Contact
    Command Linux
    Home - Q&A - How to Find File in Linux

    How to Find File in Linux

    WillieBy WillieDecember 24, 2025Updated:December 24, 2025No Comments5 Mins Read

    Locate Files by Name or Extension

    The find command searches for objects matching specific criteria. Search for all .log files in /var/log and subdirectories:

    $ find /var/log -name "*.log"

    Command Syntax and Structure

    The find command follows this pattern:

    find [options] [path] [expression]

    The path defines where the search starts. Options control behavior and optimization. Expression specifies matching criteria.

    Example with optimization and symbolic link following:

    $ find -O3 -L /home -name "*.conf"

    This enables maximum optimization (-O3) and follows symbolic links (-L). The search covers /home for all configuration files.

    Basic Search Examples

    Search current directory for a specific file:

    $ find . -name "config.yml"

    Locate all image files in home directory:

    $ find /home -name "*.png"

    Find empty files in current location:

    $ find . -type f -empty

    Search for database files modified within the past week, owned by a specific user:

    $ find /var/data -user postgres -mtime -7 -name "*.db"

    Optimization and Search Options

    The find command ignores symbolic links by default. Add -L to follow links during searches.

    Three optimization levels improve search performance. -O1 filters by filename first. -O2 adds file type filtering before resource-intensive tests. -O3 reorders all tests by efficiency and success probability.

    Common options include:

    • -O1 – Default optimization filtering by filename
    • -O2 – Filter by name and type before complex tests
    • -O3 – Automatic reordering based on resource efficiency
    • -maxdepth X – Limit search depth to X directory levels
    • -iname – Case-insensitive name matching
    • -not – Return results that do not match criteria
    • -type f – Match files only
    • -type d – Match directories only

    Search by Modification Time

    Filter results based on when files were last modified:

    $ find / -name "*.txt" -mtime -3

    This returns all text files modified within three days.

    Search a user directory for configuration files changed in the past two days:

    $ find /home/admin -name "*.conf" -mtime -2
    Note: The -mtime value represents days. Use -mmin for minute-based searches. Negative values indicate “less than,” while positive values mean “more than.”

    Search File Content Using grep

    The find command locates files by name and metadata. Combine it with grep to search file contents:

    $ find . -type f -exec grep "database_password" '{}' \; -print

    This searches every file in the current tree for the specified string and prints matching results.

    The curly braces {} represent each matched file. Single quotes prevent shell interpretation. The semicolon \; terminates the -exec command.

    Alternative syntax using xargs:

    $ find . -type f | xargs grep "configuration"
    Tip: Use grep -l to display only filenames without content snippets. This produces cleaner output when you need file locations rather than matching text.

    Execute Commands on Results

    The -exec option runs commands against matched files. Modify permissions for all configuration files:

    $ find . -name "*.conf" -exec chmod 640 '{}' \;

    Commands execute from the find process root directory. Use -execdir to run commands from each matched file’s location for improved security and performance.

    Replace -exec with -ok or -execdir with -okdir to prompt before executing each command.

    Warning: The -exec option runs automatically without confirmation. Test commands thoroughly before applying them to production systems.

    Advanced Search Techniques

    Search by File Size

    Locate files exceeding specific sizes:

    $ find / -type f -size +500M

    This returns files larger than 500 megabytes.

    Size units include:

    • c – bytes
    • k – kilobytes
    • M – megabytes
    • G – gigabytes

    Search by Permissions

    Find files with specific permission settings:

    $ find /var/www -type f -perm 644

    Case-Insensitive Searches

    Standard searches respect case. Use -iname for case-insensitive matching:

    $ find . -iname "readme.txt"

    This matches README.txt, readme.TXT, and any case variation.

    Alternative: The locate Command

    The locate command queries a pre-built database instead of scanning directories in real-time. This provides faster results but requires database updates to reflect recent changes.

    $ locate example.conf

    Update the database manually:

    $ sudo updatedb
    Feature find locate
    Search Speed Slower Faster
    Real-time Results Yes No
    Search Flexibility Extensive Limited
    Database Required No Yes
    Note: Choose find for recent files or complex queries. Use locate for quick searches of established content.

    Summary

    The find command delivers powerful file location capabilities through its extensive filtering options. Master basic name searches, then progress to advanced techniques including modification time filtering, content searches with grep, and automated command execution.

    The locate command provides speed advantages for established files. Both tools serve distinct purposes within effective Linux file management strategies.

    FAQs

    The find command scans directories in real-time, reflecting current filesystem state. The locate command queries a database, offering speed but requiring updates via updatedb for recent changes.

    Use the -name option with wildcards: find . -name "*.pdf" locates all PDF files. For case-insensitive searches, use -iname instead.

    No, find searches names and metadata only. Combine it with grep for content searches: find . -type f -exec grep "text" '{}' \;

    Use -mtime for day-based searches or -mmin for minutes. Example: find . -mtime -7 returns files modified within seven days.

    Always test your search without -delete first. Verify results match expectations, then add the deletion flag. Use -ok instead of -exec for confirmation prompts.

    Willie
    • Website

    Willie has over 15 years of experience in Linux system administration and DevOps. After managing infrastructure for startups and enterprises alike, he founded Command Linux to share the practical knowledge he wished he had when starting out. He oversees content strategy and contributes guides on server management, automation, and security.

    Related Posts

    Copy File Using CP Command In Linux

    April 16, 2026

    How To Find Text in Files In Linux

    April 14, 2026

    Ubuntu Install AppImage On Linux

    April 14, 2026

    How to Check Linux Version

    April 9, 2026
    Top Posts

    ISPELL

    March 27, 2026

    Try Hard Wordle 2026

    April 10, 2026

    XHOST

    February 24, 2026

    M4

    January 24, 2026
    • Home
    • Contact Us
    • Privacy Policy
    • Terms of Use

    Type above and press Enter to search. Press Esc to cancel.