linux-icon

Linux

findコマンド完全ガイド:初心者のための詳細オプション解説

Linuxにおいて効率的にファイルを検索するためには、findコマンドの習得が不可欠となる。本記事では、初心者の方にもわかりやすくfindコマンドの使い方と、様々なオプションについて詳しく解説していく。

「サボチン」です。AWSパートナー企業でエンジニアとして働いています。
お問い合わせ自己紹介

findコマンドの基本

findコマンドは、ファイルシステム内でファイルやディレクトリを検索するための強力なツール。基本的な構文は以下の通り

find [検索開始ディレクトリ] [オプション] [検索条件] [アクション]

基本的な使い方

構文

findコマンドの基本的な構文は以下の通り

find [検索開始ディレクトリ] [オプション] [検索条件] [アクション]

例1: 現在のディレクトリ以下のすべてのファイルを表示

find .

この例では、カレントディレクトリ(.)以下のすべてのファイルとディレクトリを表示する。

実際に個人的に利用しているサーバのec2-userのホームディレクトリ(/home/ec2-user)で実行すると以下の表示となる。

**$ pwd
/home/ec2-user

$ find .
.
./.bash_logout
./.bash_profile
./.bashrc
./.ssh
./.ssh/authorized_keys
./.bash_history
./.ansible
./.ansible/tmp**

findコマンドの主要オプション

1. 名前による検索 (-name, -iname)

  • name: ファイル名で検索(大文字小文字を区別)
  • iname: ファイル名で検索(大文字小文字を区別しない)
find /home/user -name "*.txt"
find /home/user -iname "README*"

下は、ec2-userのホームディレクトリ(/home/ec2-user)で実行した際の例。

$ pwd
/home/ec2-user
$ ls -a
.  ..  .ansible  .bash_history  .bash_logout  .bash_profile  .bashrc  .ssh  readme.md  test.txt

# 大文字小文字を区別する場合
$ find . -name "*.txt"
./test.txt

# 大文字小文字を区別しない場合
$ find /home/ec2-user/ -iname "README*"
/home/ec2-user/readme.md

2. タイプによる検索 (-type)

  • type f: 通常のファイル
  • type d: ディレクトリ
  • type l: シンボリックリンク
find /home/user -type d

下は、ec2-userのホームディレクトリ(/home/ec2-user)で実行した際の例。

$ pwd
/home/ec2-user
$ ls -a
.  ..  .ansible  .bash_history  .bash_logout  .bash_profile  .bashrc  .ssh  readme.md  test.txt

# 通常のファイルを検索する場合
$ find . -type f
./.bash_logout
./.bash_profile
./.bashrc
./.ssh/authorized_keys
./.bash_history
./test.txt
./readme.md

# ディレクトリを検索する場合
$ find . -type d
.
./.ssh
./.ansible
./.ansible/tmp

3. サイズによる検索 (-size)

  • +n: nより大きい
  • n: nより小さい
  • n: ちょうどnのサイズ

単位:c(バイト)、k(キロバイト)、M(メガバイト)、G(ギガバイト)

find /home/user -size +10M
find /home/user -size -1G

下は、ec2-userのホームディレクトリ(/home/ec2-user)で実行した際の例。


$ ls -lh
total 5.1G
-rw-r--r--. 1 ec2-user ec2-user 1.0M Aug 25 14:10 file_1mb
-rw-r--r--. 1 ec2-user ec2-user 5.0G Aug 25 14:05 file_5gb
-rw-r--r--. 1 ec2-user ec2-user    0 Aug 25 13:51 readme.md
-rw-r--r--. 1 ec2-user ec2-user    0 Aug 25 13:45 test.txt

# 10MBより大きいファイルを検索する場合
$ find . -size +10M
./file_5gb

# 2MBより小さいファイルを検索する場合
$ find . -size -2M
.
./.bash_logout
./.bash_profile
./.bashrc
./.ssh
./.ssh/authorized_keys
./.bash_history
./.ansible
./.ansible/tmp
./test.txt
./readme.md
./file_1mb # 含まれていることが確認できる

4. 時間による検索 (-atime, -mtime, -ctime)

  • atime: アクセス時間
  • mtime: 修正時間
  • ctime: 変更時間

数字の前に「+」をつけると「より古い」、「-」をつけると「より新しい」を意味する。

find /home/user -mtime -7  # 7日以内に修正されたファイル
find /home/user -atime +30 # 30日以上前にアクセスされたファイル

下は、ec2-userのホームディレクトリ(/home/ec2-user)で実行した際の例。

# 7日以内に修正されたファイルを検索する場合
$ find . -mtime -7
.
./.bash_history
./test.txt
./readme.md
./file_5gb
./file_1mb

# 30日以上前にアクセスされたファイルを検索する場合
$ find . -atime +30
.
./.bash_logout
./.bash_profile
./.bashrc
./.ssh
./.ssh/authorized_keys
./.bash_history

5. パーミッションによる検索 (-perm)

find /home/user -perm 644  # パーミッションが644のファイル
find /home/user -perm -u+w # ユーザーに書き込み権限があるファイル

下は、ec2-userのホームディレクトリ(/home/ec2-user)で実行した際の例。

$ find . -perm 644
./.bash_logout
./.bash_profile
./.bashrc
./test.txt
./readme.md
./file_5gb
./file_1mb

6. 所有者・グループによる検索 (-user, -group)

find /home/user -user john
find /home/user -group developers

下は、ec2-userのホームディレクトリ(/home/ec2-user)で実行した際の例。

$ ls -la
total 5243920
drwx------. 4 ec2-user ec2-user        176 Aug 25 14:12 .
drwxr-xr-x. 3 root     root             22 Jul 21 06:37 ..
drwx------. 3 ec2-user ec2-user         17 Jul 28 07:02 .ansible
-rw-------. 1 ec2-user ec2-user       2744 Aug 25 14:16 .bash_history
-rw-r--r--. 1 ec2-user ec2-user         18 Jan 28  2023 .bash_logout
-rw-r--r--. 1 ec2-user ec2-user        141 Jan 28  2023 .bash_profile
-rw-r--r--. 1 ec2-user ec2-user        492 Jan 28  2023 .bashrc
drwx------. 2 ec2-user ec2-user         29 Jul 21 06:37 .ssh
-rw-r--r--. 1 ec2-user ec2-user    1048576 Aug 25 14:12 file_1mb
-rw-r--r--. 1 ec2-user ec2-user 5368709120 Aug 25 14:05 file_5gb
-rw-r--r--. 1 ec2-user ec2-user          0 Aug 25 13:51 readme.md
-rw-r--r--. 1 ec2-user ec2-user          0 Aug 25 13:45 test.txt

$ find . -user ec2-user
.
./.bash_logout
./.bash_profile
./.bashrc
./.ssh
./.ssh/authorized_keys
./.bash_history
./.ansible
./.ansible/tmp
./test.txt
./readme.md
./file_5gb
./file_1mb

7. 深さの制限 (-maxdepth, -mindepth)

find /home/user -maxdepth 2 -name "*.log"

下は、ec2-userのホームディレクトリ(/home/ec2-user)で実行した際の例。

$ find . -maxdepth 1 -user ec2-user 
.
./.bash_logout
./.bash_profile
./.bashrc
./.ssh
./.bash_history
./.ansible
./test.txt
./readme.md
./file_5gb
./file_1mb

高度な使用方法

1. 論理演算子の使用

  • and (デフォルト): 両方の条件を満たす
  • or: いずれかの条件を満たす
  • not: 条件を満たさない
find /home/user -name "*.txt" -and -size +1M
find /home/user \\\\( -name "*.jpg" -or -name "*.png" \\\\) -and -size +500k

2. アクションの実行 (-exec)

find /home/user -name "*.tmp" -exec rm {} \\\\;
find /home/user -type f -exec chmod 644 {} +

3. 結果の出力形式の変更 (-printf)

find /home/user -type f -printf "%p - %s bytes\\\\n"

よくあるエラーと解決方法

  1. Permission denied エラー:
    • sudoを使用するか、適切な権限を持つユーザーで実行する
  2. ファイルが見つからない:
    • パスが正しいか確認する
    • ファイル名やパターンが正しいか確認する

まとめ

findコマンドはとても便利なコマンドだと思う。この記事で紹介したオプションを組み合わせることで、様々なファイル検索に対応できる気と思う。実際に試してみてほしい。

-Linux
-