LoginSignup
22
18

More than 5 years have passed since last update.

findコマンドの-nameオプションと-pathオプションの違い

Posted at

はじめに

Linux環境においてfindコマンドはよく使われるコマンドの一つですが、"-name"オプションと"-path"オプションというよく似たオプションがあります。
普段は"-name"オプションばかり使っているので、今回は両者の違いを調べてみました。

-nameオプション

find {検索するディレクトリ} -name {検索するキーワード}

  • "-name"オプションを付けることで、ファイル名、ディレクトリ名を指定して検索できます。
    • 検索するキーワードに"*"を付けることで、ワイルドカードを使った検索が可能です。
  • ただし、"-name"オプションの引数にスラッシュを含む場合は検索できないため、例えば"-name {ディレクトリ名}/{ファイル名}"とパラメーターを指定することができません。
[root@kumotori ~]# find /etc -name "ifcfg*"
/etc/sysconfig/network-scripts/ifcfg-eth0
/etc/sysconfig/network-scripts/ifcfg-lo
[root@kumotori /]# find / -name "/etc/sysconfig/network-scripts/*eth*"
find: warning: Unix filenames usually don't contain slashes (though pathnames do).  That means that '-name `/etc/sysconfig/network-scripts/*eth*'' will probably evaluate to false all the time on this system.  You might find the '-wholename' test more useful, or perhaps '-samefile'.  Alternatively, if you are using GNU grep, you could use 'find ... -print0 | grep -FzZ `/etc/sysconfig/network-scripts/*eth*''.

-pathオプション

find {検索するディレクトリ} -path "{パターン}"

  • "-path"オプションを付けると、パターンにマッチするファイルやディレクトリを検索できます。
    • 検索するパターンに"*"を付けることで、ワイルドカードを使った検索が可能です。
  • "-path"オプションにはスラッシュを含むパターンを指定できるので、ディレクトリもパターンに含めて検索できます。
[root@kumotori /]# find /etc/sysconfig/ -path "*network-scripts/*eth*"
/etc/sysconfig/network-scripts/ifup-eth
/etc/sysconfig/network-scripts/ifcfg-eth0
/etc/sysconfig/network-scripts/ifdown-eth

まとめ

  • どちらのオプションも「ほぼ同等の機能」になるので、通常の操作ではどちらかを覚えておけば十分だと思われます。
  • "-path"オプションの方が検索条件の柔軟性が高いようですが、これまでの癖で"-name"オプションを使い続けそうです。
22
18
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
22
18