Cheat Sheet速查表
跳转到主要内容

Find 速查表

本文根据 Devhints / Find 翻译。代码示例及版本范围沿用原文。原文标注更新日期:2019-11-17。

用法

find <path> <conditions> <actions>

条件

-name "*.c"
-user jonathan
-nouser
-type f            # File
-type d            # Directory
-type l            # Symlink
-depth 2           # At least 3 levels deep
-regex PATTERN
-size 8            # Exactly 8 512-bit blocks
-size -128c        # Smaller than 128 bytes
-size 1440k        # Exactly 1440KiB
-size +10M         # Larger than 10MiB
-size +2G          # Larger than 2GiB
-newer   file.txt
-newerm  file.txt        # modified newer than file.txt
-newerX  file.txt        # [c]hange, [m]odified, [B]create
-newerXt "1 hour ago"    # [t]imestamp

访问时间条件

-atime 0           # Last accessed between now and 24 hours ago
-atime +0          # Accessed more than 24 hours ago
-atime 1           # Accessed between 24 and 48 hours ago
-atime +1          # Accessed more than 48 hours ago
-atime -1          # Accessed less than 24 hours ago (same a 0)
-ctime -6h30m      # File status changed within the last 6 hours and 30 minutes
-mtime +1w         # Last modified more than 1 week ago

这些条件仅适用于 macOS 和类 BSD 系统,不支持 GNU/Linux。

条件流程

\! -name "*.c" # NOT named "*.c"
\( x -or y \)

操作

-exec rm {} \;
-print
-delete

示例

find . -name '*.jpg'
find . -name '*.jpg' -exec rm {} \;
find . -newerBt "24 hours ago"
find . -type f -mtime +29 # find files modified more than 30 days ago
find . -type f -newermt 2016-08-07 \! -newermt 2016-08-08 # find in date range