【Linux】findコマンドでファイル検索する方法

analystics

Linuxでファイル検索するfindコマンドについて説明します。

ファイル名を指定して検索再帰的に検索全ディレクトリを対象に検索するといった方法も実例を用いて説明します。

筆者はWindowsサーバーやLinuxサーバーの運用構築を11年以上おこなってきました。その経験を元に知識を公開していきます!

この記事を読めばLinuxのややこしいfindコマンドで、検索する事が簡単にできるようになります。

目次

Linuxサーバーのfindコマンドでファイル検索する方法

analystics

Linuxサーバーでファイルを検索するfindコマンドの基礎は以下です。

find パス オプション

※まず検索するパスを指定してから、オプションを指定します。

Linuxサーバーでfindコマンドのオプションについて

検索オプション

Linuxサーバーでfindコマンドのオプションについてまとめます。

オプション効果
-name一致するファイル名を指定
-maxdepth検索する階層を指定
-ls検索した結果の詳細を表示する
-typeファイルタイプを指定
-sizeファイルやディレクトリのサイズ指定
-empty空のファイルやディレクトリを検索
-perm特定の権限を持ったファイルの検索
-atimeアクセスがあった日数を指定
-mtime更新があった日数を指定
-pathファイルのパスが一致しているファイルを検索
-print検索結果を標準出力に出力する
-exec command \;検索語コマンドを実行する
Linuxサーバーでfindコマンドのオプション一覧

需要の高いものを実例を用いて解説します。

findコマンドで一致するファイル名を指定

ファイル検索

findコマンドファイル名を指定します。

find パス -name ファイル名

lsコマンドでカレントディレクトリのファイル名を縦に表示してみます。

Linuxのファイルやディレクトリを表示させる、lsコマンドについては以下の記事を参照下さい。

[root@localhost ~]# ls -1
anaconda-ks.cfg
backup
bash-completion
blog_test
enforce~
enforcx~
enforcy~
enforcz~
file1.txt
link.ln
memory_usage_check.log
sh_test
startup.log
symbolic-link
symbolic-link-dir
symbolic-link-file1
symbolic-link-file1.ln
symbolic-link-file2
thinclient_drives
vmstat.log
workspace

findコマンドでsymbolic-linkのファイルを検索します。

[root@localhost ~]# find . -name symbolic-link
./symbolic-link

カレントディレクトリは「./」でも良いです。

[root@localhost ~]# find ./ -name symbolic-link
./symbolic-link

カレントディレクトリは省略も可能です。

[root@localhost ~]# find -name symbolic-link
./symbolic-link

検索されたファイルが表示されました。

findコマンドで検索する階層を指定

findコマンドでは指定したパス配下の全てのファイルやディレクトリが対象となります。

しかし階層の範囲を指定したい場合があります。そんな時は「maxdepth」オプションを使用します。

find パス -maxdepth 検索する階層 -name 名前

他のオプションと併用して使用します。

指定したパスのみ再帰的に検索しない場合は以下の様に指定します。

find パス -maxdepth 1 -name 名前

これで指定したパスのみが対象範囲となります。

findコマンドで検索した結果の詳細を表示する

findコマンドで検索した結果の詳細を表示する事が可能です。

find パス -name 名前 -ls

※検索オプションの最後つけます。

他のオプションと併用して使用します。

検索したファイルなどの詳細を表示します。

[root@localhost ~]# find . -name startup.log -ls
10766006    0 -rw-r--r--   1 root     root            0  5月  4 17:27 ./startup.log

検索したファイルの詳細が表示されています。

findコマンドでファイルタイプを指定

findコマンドでファイルタイプを指定します。

find パス -type タイプ

findコマンドのファイルタイプは3種類あります。

findコマンドのファイルタイプ一覧
  • d・・・ディレクトリ
  • f・・・ファイル
  • l・・・シンボリックリンク

lsコマンドでカレントディレクトリのファイル名を、詳細表示してみます。

[root@localhost ~]# ls -l
合計 80
-rw-------.  1 root root  1257  1月  7  2023 anaconda-ks.cfg
drwxr-xr-x.  2 root root     6  4月 25  2023 backup
drwxr-xr-x.  3 root root    48  4月 13  2023 bash-completion
drwxr-xr-x.  2 root root    75 12月  4 19:32 blog_test
-rw-r--r--.  1 root root     1 11月 18 23:24 enforce~
-rw-r--r--.  1 root root     1 11月 19 00:43 enforcx~
-rw-r--r--.  1 root root     1 11月 19 00:43 enforcy~
-rw-r--r--.  1 root root     1 11月 19 00:42 enforcz~
-rw-r--r--   1 root root    42  4月 25 23:01 file1.txt
-rwxrwxrwx   1 root root     0  5月  6 23:22 link.ln
-rw-r--r--   1 root root   911  4月 29 18:29 memory_usage_check.log
drwxr-xr-x. 12 root root  4096  4月 27  2023 sh_test
-rw-r--r--   1 root root     0  5月  4 17:27 startup.log
drwxr-xr-x   2 root root    19  5月  8 18:42 symbolic-link
drwxr-xr-x   2 root root     6  5月  8 19:48 symbolic-link-dir
-rw-r--r--   1 root root    29  5月  8 18:24 symbolic-link-file1
lrwxrwxrwx   1 root root    25  5月  8 21:08 symbolic-link-file1.ln -> /root/symbolic-link-file1
-rw-r--r--   1 root root    29  5月  8 18:33 symbolic-link-file2
drwxr-xr-t   2 root root     6 12月 21 22:58 thinclient_drives
-rw-r--r--   1 root root 28327  4月 29 19:21 vmstat.log
drwxr-xr-x.  2 root root  4096  4月 24  2023 workspace

次にfindコマンドでディレクトリのみ表示してみます。

カレントディレクトリの配下のみ表示したいので先程の「-maxdepth 1」オプションと併用します。

[root@localhost ~]# find . -maxdepth 1 -type d
.
./sh_test
./.ssh
./workspace
./blog_test
./bash-completion
./backup
./.pki
./.vnc
./.local
./thinclient_drives
./.dbus
./symbolic-link
./symbolic-link-dir

カレントディレクトリのディレクトリのみ表示されました。

シンボリックリンクのみ表示してみます。

[root@localhost ~]# find . -maxdepth 1 -type l
./symbolic-link-file1.ln

カレントディレクトリのシンボリックリンクのみ表示されました。

シンボリックリンクの作成方法は以下の記事を参照下さい。

findコマンドでファイルやディレクトリのサイズ指定

ファイル検索

findコマンドでファイルやディレクトリのサイズ指定して検索します。

find パス -size サイズ(小数点は使用不可)
findコマンドのサイズ指定方法
  • +100k・・・100kを超えるファイルを検索
  • -100m・・・100m未満のファイルを検索

小数点は対応してないので、整数を指定します。

lsコマンドでカレントディレクトリの全ファイルを、サイズ順で単位をいい感じに表示してみます。

[root@localhost ~]# ls -aSlh
合計 160K
-rw-r--r--   1 root root  28K  4月 29 19:21 vmstat.log
-rw-------.  1 root root  19K  5月  9 23:38 .bash_history
-rw-r--r--   1 root root  12K  4月 25 23:36 .file1.txt.swo
-rw-r--r--   1 root root  12K  4月 25 00:26 .file1.txt.swp
dr-xr-x---. 15 root root 4.0K  5月  8 21:09 .
drwxr-xr-x. 12 root root 4.0K  4月 27  2023 sh_test
drwxr-xr-x.  2 root root 4.0K  4月 24  2023 workspace
-rw-------   1 root root 4.0K 12月 22 01:10 .mysql_history
-rw-------.  1 root root 1.3K  1月  7  2023 anaconda-ks.cfg
-rw-------   1 root root 1.0K 12月 21 22:31 .rnd
-rw-r--r--   1 root root  911  4月 29 18:29 memory_usage_check.log
dr-xr-xr-x. 17 root root  244 11月 19 12:58 ..
-rw-r--r--.  1 root root  176 12月 29  2013 .bash_profile
-rw-r--r--.  1 root root  176 12月 29  2013 .bashrc
-rw-r--r--.  1 root root  129 12月 29  2013 .tcshrc
-rw-r--r--.  1 root root  100 12月 29  2013 .cshrc
drwxr-xr-x.  2 root root   75 12月  4 19:32 blog_test
-rw-------   1 root root   67 12月 22 22:16 .Xauthority
drwx------   2 root root   57 12月 21 22:58 .vnc
drwxr-xr-x.  3 root root   48  4月 13  2023 bash-completion
-rw-r--r--   1 root root   42  4月 25 23:01 file1.txt
-rw-r--r--   1 root root   29  5月  8 18:24 symbolic-link-file1
-rw-r--r--   1 root root   29  5月  8 18:33 symbolic-link-file2
drwx------   3 root root   25 12月 22 22:16 .dbus
drwx------.  2 root root   25  1月 14  2023 .ssh
lrwxrwxrwx   1 root root   25  5月  8 21:08 symbolic-link-file1.ln -> /root/symbolic-link-file1
drwx------   3 root root   19 12月 21 22:58 .local
drwxr-----   3 root root   19 12月 21 22:28 .pki
drwxr-xr-x   2 root root   19  5月  8 18:42 symbolic-link
-rw-r--r--.  1 root root   18 12月 29  2013 .bash_logout
drwxr-xr-x.  2 root root    6  4月 25  2023 backup
drwxr-xr-x   2 root root    6  5月  8 19:48 symbolic-link-dir
drwxr-xr-t   2 root root    6 12月 21 22:58 thinclient_drives
-rw-r--r--.  1 root root    1 11月 18 23:24 enforce~
-rw-r--r--.  1 root root    1 11月 19 00:43 enforcx~
-rw-r--r--.  1 root root    1 11月 19 00:43 enforcy~
-rw-r--r--.  1 root root    1 11月 19 00:42 enforcz~
-rw-------   1 root root    0 12月 22 22:16 .xsession-errors
-rwxrwxrwx   1 root root    0  5月  6 23:22 link.ln
-rw-r--r--   1 root root    0  5月  4 17:27 startup.log

次にfindコマンドで4kを超えるファイルのみ表示してみます。

カレントディレクトリの配下のみ表示したいので先程の「-maxdepth 1」オプションと併用します。

[root@localhost ~]# find . -maxdepth 1 -size +4k
./.bash_history
./.file1.txt.swp
./.file1.txt.swo
./vmstat.log

サイズが4kを超えるカレントディレクトリの全ファイルのみ表示されました。

超えるなので4kジャストのファイルは表示されていない事に注意

findコマンドで空のファイルやディレクトリを検索

findコマンドで空のファイルやディレクトリを検索します。

find パス -empty

空のファイルとディレクトリのみ表示してみます。

カレントディレクトリの配下のみ表示したいので先程の「-maxdepth 1」オプションと併用します。

[root@localhost ~]# find . -maxdepth 1 -empty
./backup
./.xsession-errors
./thinclient_drives
./startup.log
./link.ln
./symbolic-link-dir

空のファイルとディレクトリが表示されています。

空のディレクトリとはサイズが0という訳ではありません。空のディレクトリは容量が0ではありません。

findコマンドで特定の権限を持ったファイルの検索

findコマンドで特定の権限を持ったファイルの検索をします。

find パス -perm パーミッション

パーミッションが755のファイルやディレクトリを表示します。

カレントディレクトリの配下のみ表示したいので、先程の「-maxdepth 1」オプションと併用します。

[root@localhost ~]# find . -maxdepth 1 -perm 755
./sh_test
./workspace
./blog_test
./bash-completion
./backup
./symbolic-link
./symbolic-link-dir

パーミッションが755(rwxr-xr-x)のファイルとディレクトリが表示されました。

実行ユーザーが実行権限をもったファイルやディレクトリを表示します。

[root@localhost ~]# find . -maxdepth 1 -perm -u+x
.
./sh_test
./.ssh
./workspace
./blog_test
./bash-completion
./backup
./.pki
./.vnc
./.local
./thinclient_drives
./.dbus
./link.ln
./symbolic-link
./symbolic-link-dir
./symbolic-link-file1.ln

実行ユーザーが実行権限をもったファイルやディレクトリが表示されました。

findコマンドでアクセスがあった日数を指定

findコマンドでアクセスがあった日数を指定します。

find パス -atime 日数
findコマンドでの日数の指定方法
  • +3・・・3日以前にアクセスがあったファイル(3日前より過去)
  • 3・・・3日前にアクセスがあったファイル(丁度3日前)
  • -3・・・3日未満にアクセスがあったファイル(3日より未来から現在まで)

3日以前にアクセスがあったファイルやディレクトリを表示します。

カレントディレクトリの配下のみ表示したいので、先程の「-maxdepth 1」オプションと併用します。

[root@localhost ~]# find . -maxdepth 1 -atime +3
./.bash_logout
./.cshrc
./.tcshrc
./anaconda-ks.cfg
./.mysql_history
./enforce~
./enforcz~
./enforcy~
./enforcx~
./.rnd
./.xsession-errors
./.Xauthority
./.file1.txt.swp
./file1.txt
./.file1.txt.swo
./vmstat.log

3日以前にアクセスがあったファイルやディレクトリが表示されました。

3日前にアクセスがあったファイルやディレクトリを表示します。

[root@localhost ~]# find . -maxdepth 1 -atime 3
./link.ln

3日前にアクセスがあったファイルやディレクトリが表示されました。

3日前から現在まで(直近3日)にアクセスがあったファイルやディレクトリを表示します。

[root@localhost ~]# find . -maxdepth 1 -atime -3
.
./.bash_profile
./.bashrc
./.bash_history
./sh_test
./.ssh
./workspace
./blog_test
./bash-completion
./backup
./.pki
./.vnc
./.local
./thinclient_drives
./.dbus
./memory_usage_check.log
./startup.log
./symbolic-link
./symbolic-link-file1
./symbolic-link-file2
./symbolic-link-dir
./symbolic-link-file1.ln

直近3日でアクセスがあったファイルやディレクトリが表示されました。

findコマンドで更新があった日数を指定

更新日時で検索

findコマンドで更新があった日数を指定します。

find パス -mtime 日数
findコマンドでの日数の指定方法
  • +3・・・3日以前に更新があったファイル(3日前より過去)
  • 3・・・3日前に更新があったファイル(丁度3日前)
  • -3・・・3日未満に更新があったファイル(3日より未来から現在まで)

lsコマンドで更新日時を詳細表示して順に並べます。

[root@localhost ~]# date
2024年  5月 10日 金曜日 19:54:19 JST
[root@localhost ~]#
[root@localhost ~]# ls -tl --full-time
合計 80
lrwxrwxrwx   1 root root    25 2024-05-08 21:08:30.984000000 +0900 symbolic-link-file1.ln -> /root/symbolic-link-file1
drwxr-xr-x   2 root root     6 2024-05-08 19:48:39.173000000 +0900 symbolic-link-dir
drwxr-xr-x   2 root root    19 2024-05-08 18:42:02.484000000 +0900 symbolic-link
-rw-r--r--   1 root root    29 2024-05-08 18:33:17.071000000 +0900 symbolic-link-file2
-rw-r--r--   1 root root    29 2024-05-08 18:24:55.993000000 +0900 symbolic-link-file1
-rwxrwxrwx   1 root root     0 2024-05-06 23:22:53.597000000 +0900 link.ln
-rw-r--r--   1 root root     0 2024-05-04 17:27:30.070000000 +0900 startup.log
-rw-r--r--   1 root root 28327 2024-04-29 19:21:45.766000000 +0900 vmstat.log
-rw-r--r--   1 root root   911 2024-04-29 18:29:59.609000000 +0900 memory_usage_check.log
-rw-r--r--   1 root root    42 2024-04-25 23:01:15.694000000 +0900 file1.txt
drwxr-xr-t   2 root root     6 2023-12-21 22:58:46.774000000 +0900 thinclient_drives
drwxr-xr-x.  2 root root    75 2023-12-04 19:32:23.632000000 +0900 blog_test
-rw-r--r--.  1 root root     1 2023-11-19 00:43:44.000000000 +0900 enforcx~
-rw-r--r--.  1 root root     1 2023-11-19 00:43:17.000000000 +0900 enforcy~
-rw-r--r--.  1 root root     1 2023-11-19 00:42:58.000000000 +0900 enforcz~
-rw-r--r--.  1 root root     1 2023-11-18 23:24:08.000000000 +0900 enforce~
drwxr-xr-x. 12 root root  4096 2023-04-27 10:24:18.580000000 +0900 sh_test
drwxr-xr-x.  2 root root     6 2023-04-25 11:21:39.897000000 +0900 backup
drwxr-xr-x.  2 root root  4096 2023-04-24 17:36:35.843000000 +0900 workspace
drwxr-xr-x.  3 root root    48 2023-04-13 14:30:01.922000000 +0900 bash-completion
-rw-------.  1 root root  1257 2023-01-07 20:59:41.775000000 +0900 anaconda-ks.cfg

findコマンドで直近4日で更新があったファイルなどを表示します。

[root@localhost ~]# find . -maxdepth 1 -mtime -4
.
./.bash_history
./link.ln
./symbolic-link
./symbolic-link-file1
./symbolic-link-file2
./symbolic-link-dir
./symbolic-link-file1.ln

findコマンドで2日前に更新があったファイルなどを表示します。

[root@localhost ~]# find . -maxdepth 1 -mtime 2
./symbolic-link
./symbolic-link-file1
./symbolic-link-file2
./symbolic-link-dir

全て指定通りに表示されています。

まとめ:Linuxサーバーでfindコマンドでファイル検索する方法

analystics

Linuxサーバーでfindコマンドの基礎について

Linuxサーバーでファイルを検索するfindコマンドの基礎は以下です。

find パス オプション

※まず検索するパスを指定してから、オプションを指定します。

Linuxサーバーでfindコマンドのオプションについて

Linuxサーバーでfindコマンドのオプションについてまとめます。

オプション効果
-name一致するファイル名を指定
-maxdepth検索する階層を指定
-typeファイルタイプを指定
-sizeファイルやディレクトリのサイズ指定
-empty空のファイルやディレクトリを検索
-perm特定の権限を持ったファイルの検索
-atimeアクセスがあった日数を指定
-mtime更新があった日数を指定
-pathファイルのパスが一致しているファイルを検索
-print検索結果を標準出力に出力する
-exec command \;検索語コマンドを実行する
Linuxサーバーでfindコマンドのオプション一覧

これでLinuxのfindコマンドで検索する時に、効率よく検索できるようになります。

analystics

この記事が気に入ったら
フォローしてね!

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!
目次