find
ファイルとディレクトリ
書き方
find [path] -name '[pattern]' -type fディレクトリを歩いて一つずつ条件に当てます。-name '*.log' のように引用符で囲まないと、シェルが先に展開してしまいます。
よく使うオプション
| オプション | 意味 |
|---|---|
| -name | Match the file name, case sensitive; -iname ignores case |
| -type | Restrict to f (file), d (directory) or l (symlink) |
| -size | Filter by size, as in -size +100M |
| -mtime | Filter by age in days, as in -mtime +7 |
| -maxdepth | Limit how deep to walk; GNU wants it before the tests |
| -delete | Delete every match, no confirmation |
| -exec | Run a command on each hit, ending with \; or + |
| -print0 | Separate results with NUL, to pair with xargs -0 |
例
find . -name '*.log' -mtime +7 -deleteDeletes log files older than a week.
find / -type f -size +1G 2>/dev/nullFinds files over a gigabyte, hiding permission errors.
find . -name '*.jpg' -exec mv {} photos/ +Moves every JPEG it finds.
移したり消したり探したりするものです。聞き返してくれない命令が混ざっているので、-i を付ける癖が手を守ります。
読み方
- 角括弧 [ ] は省いてよい部分です。
- 三点 … は複数並べられるという意味です。
- オプションは大文字小文字を区別します — -r と -R が別物のコマンドもあります。
よくある質問
Q. find は何をしますか。
ディレクトリを歩いて一つずつ条件に当てます。-name '*.log' のように引用符で囲まないと、シェルが先に展開してしまいます。
Q. どう打ちますか。
find [path] -name '[pattern]' -type f — 角括弧は省いてよい部分です。
Q. よく使うオプションはいくつですか。
ここにまとめたのは8個です。全部は man find にあります。このコマンドはファイルとディレクトリの仲間です。
関連するコマンド
man ページ: man find