LoginSignup
0
0

More than 5 years have passed since last update.

findの使い方色々

Posted at

はじめに

ファイルシステム検索に使用するfindコマンドのサンプル集。
個人的に現場で割とよく使う書き方を集めてみました。

コマンド

ディレクトリ検索
$find . -type d
 →ディレクトリだけがヒットする。

ファイルのみ検索
$find . -type f

特定のファイル名検索
$find . -name "*log*"
 →"log"を含むファイル・ディレクトリがヒットする。

シンボリックリンク先も検索
$find -L . -type d
 →-Lがないと、シンボリックリンク先は辿ってくれない。

logファイル以外のファイルを検索
$find . -type f ! -name "*.log"

logまたはtxtファイルを検索
$find . -name "*.log" -o -name "*.txt"

1~2階層先だけ表示
$find . -maxdepth 3 ~

find の結果をlsで表示
$find . -name access_log | xargs ls -ld

abc.logより古いファイルを削除
$find . ! -newer abc.log | xargs rm

本日日付から10日より前のファイルを検索する。
$find . -type f -mtime +10
→今日が1/14なら、1/3以前のファイルがひっかかる
 (1/4~1/13の10日分は対象外)。

対象ディレクトリ配下のファイルをフルパス、かつ権限・オーナー込みで表示。
$find / -exec ls -ld {} \;
{}は検索したファイル名を指す。\;はコマンドの終了を指す。
「{}」と「\」の間にスペース要。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0