find

ファイルとディレクトリ

書き方

find [path] -name '[pattern]' -type f

ディレクトリを歩いて一つずつ条件に当てます。-name '*.log' のように引用符で囲まないと、シェルが先に展開してしまいます。

よく使うオプション

オプション意味
-nameMatch the file name, case sensitive; -iname ignores case
-typeRestrict to f (file), d (directory) or l (symlink)
-sizeFilter by size, as in -size +100M
-mtimeFilter by age in days, as in -mtime +7
-maxdepthLimit how deep to walk; GNU wants it before the tests
-deleteDelete every match, no confirmation
-execRun a command on each hit, ending with \; or +
-print0Separate results with NUL, to pair with xargs -0

find . -name '*.log' -mtime +7 -delete

Deletes log files older than a week.

find / -type f -size +1G 2>/dev/null

Finds 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