Ben Chuanlong Du's Blog

And let it direct your passion with reason.

Tips on the find command in Linux

Better Alternatives to find

There are some better alternatives to find. The Python module pathlib is the most suitable one for relatively complex jobs. fd, ripgrep, fselect and osquery are other alternatives.

find fd fselect osquery ripgrep (rg)
Primary Use Case File metadata search & execution (scripting) Interactive file name search (developer productivity) SQL-based file attribute search (data analysis) System instrumentation & security (fleet management) File content search (code searching)
Usability/Syntax Low. Powerful but arcane, non-intuitive syntax. Very High. Simple, ergonomic, sensible defaults. High. SQL-like syntax is very readable for complex queries. Low. Requires knowledge of SQL and specific OS schemas. Very High. Simple, grep-like, sensible defaults.
Performance Fair. Single-threaded by default. Slow in codebases. Excellent. Parallel, ignores gitignored/hidden files by default. Very Good. Optimized for its complex query execution. Good. Optimized for low-overhead daemon, not raw traversal speed. Exceptional. The gold standard for file content search speed.
Implementation C. Universally available, part of findutils. Rust. Single static binary, needs installation. Rust. Single static binary, needs installation. C++. Cross-platform framework, needs installation. Rust. Single static binary, needs installation.
Key Strength Ubiquity & -exec. The POSIX standard, powerful actions. Speed & Ergonomics. The perfect interactive find replacement. Expressive Query Language. Unmatched for complex attribute filters. Holistic System View. Queries files alongside processes, users, etc. Raw Speed for Content Search. The fastest tool for finding text in files.
Key Weakness Clunky syntax & poor defaults. Not user-friendly. Less powerful expressions than find for edge cases. Niche use case. Overkill for simple searches. Massive overkill for just finding files. Steep learning curve. Not a file finder. Only lists files as a secondary function (-l).
Best For... Shell scripts, system administration, guaranteed portability. Developers, daily interactive use, searching in git repos. Data analysts, sysadmins running complex filesystem audits. Security engineers, SREs, IT compliance teams. Developers, searching for code, log analysis.

Example Usages

Search Files By Name

  1. Find all files with the extension ".out" in the current directory and its subdirectory, and then make them executable.

    find . -type f -iname *.out -exec chmod +x '{}' \;
    # or you can use 
    find . -type f -iname *.out -print0 | xargs -0 chmod +x
    
  2. Find files whose names contain "conflicted" and remove them.

    find . -iname '*conflicted*' -print0 | xargs -0 rm
    
  3. Find Python scripts in the current directory recursively but ignore those under directories with the name .ipynb_checkpoints.

    find . -type f -iname '*.py' -not -path '*/.ipynb_checkpoints/*'
    

Search Files by Size

  1. Find files with 0 size and delete them.

    find /path/to/files -size 0 -ok -exec rm {} \;
    # or you can use
    find /path/to/files -size 0 -ok | xargs rm
    
  2. Find empty directories.

    find / -type d -empty
    
  3. Find files greater than 1G.

    find . -xdev -type f -size +1G
    
  4. First find files and then pass them to other commands is a very useful trick. For example, you can use the following command to find all R scripts containing the word paste.

    find . -type f -iname '*.r' | grep --color=auto paste
    

Search Files by Time

  1. Find files created with in 60 minutes.

    find . -cmin 60
    
  2. Find files more than 30 days ago

    find . -ctime +30
    
  3. Find file less than 30 days ago.

    find . -ctime -30
    
  4. Find files that are exactly 30 days ago.

    find . -ctime 30
    
  5. Find all files modified on the June 7, 2007 in the current directory.

    find . -type f -newermt 2007-06-07 ! -newermt 2007-06-08
    
  6. Find all files accessed on the Sep 29, 2008 in the current directory.

    find . -type f -newerat 2008-09-29 ! -newerat 2008-09-30
    
  7. Find files which had their permission changed on the same day.

    find . -type f -newerct 2008-09-29 ! -newerct 2008-09-30
    

Search Files by Type

  1. Find broken symbolic links.

    find . -xtype l
    # or
    find -L . -type l
    
  2. Find executable files in current directory

    find .  -maxdepth 1 -type f -executable
    
  3. Check file type of all files under the current directory.

    find . -type f | xargs file
    

Search Files by User Permission

  1. Find files that belong to a user but writable by its group or other people.

    find /path/to/file -user user1 -perm /022
    
    • -perm mode: File's permission bits are exactly mode (octal or symbolic).
    • -perm -mode: All of the permission bits mode are set for the file.
    • -perm /mode: Any of the permission bits mode are set for the file.
  2. The following command finds all files that readable or writable by the group or (readable or writable) by others.

    find /path/to/file -user user1 -perm /066
    
  3. The following command find all files that readable and writable by the group and (readable and writable) by others.

    find /path/to/file -user user1 -perm -066
    
  4. The following command find all files that readable or writable by the group and (readable or writable) by others.

    find /path/to/file -user user1 -perm /060 -perm /006
    

References

Comments