DEV Community

kaede
kaede

Posted on • Edited on

Bash 基礎 Part 02 -- 絞り込み。ファイルの中身、ファイル名、ディレクトリ名で検索する

AG

AG とは?

https://github.com/ggreer/the_silver_searcher

銀の検索者?

ack より速い検索ツールらしい。
ディレクトリでの検索でよく使われる。

sudo apt-get install silversearcher-ag 
Enter fullscreen mode Exit fullscreen mode

silversearcher-ag として apt-get でインストールできる。


AG で再帰的にディレクトリに対してファイルの中身を検索する

試しに source / shell の内部に

  • hoge の文字列の入ったディレクトリ
  • hoge の文字列がファイル名にのみあるファイル
  • hoge の文字列がファイルの中身にのみあるファイル
  • hoge の文字列がファイル名とファイルの中身両方あるファイル
  • hoge の文字列ではないディレクトリに入った hoge の文字列がファイル名とファイルの中身両方あるファイル

これらを作成して ag で探してみる

~/source/shell

 ag hoge
ho-ge-inside-only.txt
1:hoge

hoge-fileName-and-inside.txt
1:hoge

NO-ho-ge-dirName/hoge.txt
1:hoge

Enter fullscreen mode Exit fullscreen mode

単に ag hoge と打つと
現在いるディレクトリと、その配下のディレクトリに入っている
ファイルの中身の文字列が検索された。

ファイル名は検索されなかった。


階層の深い対象を無視する

https://www.mankier.com/1/ag

  • shell
    • NO-ho-ge-dirName
    • Depth2
      • hoge.txt

このようにディレクトリの更に下にディレクトリを作った

~/source/shell
 ag hoge                  
NO-ho-ge-dirName/hoge.txt
1:hoge

hoge-fileName-and-inside.txt
1:hoge

NO-ho-ge-dirName/Depth2/hoge.txt
1:hoge
Enter fullscreen mode Exit fullscreen mode

通常だと depth が 25 のために 25 段掘られるが

~/source/shell
 ag hoge --depth 1
hoge-fileName-and-inside.txt
1:hoge

NO-ho-ge-dirName/hoge.txt
1:hoge
Enter fullscreen mode Exit fullscreen mode

depth を 1 にすると、2 層目は掘られない。

~/source/shell
 ag hoge --depth 0
hoge-fileName-and-inside.txt
1:hoge
Enter fullscreen mode Exit fullscreen mode

0 にすれば、現在のディレクトリに直接入っているファイルだけを検索できる。


find

find でファイル名 or ディレクトリ名で検索する

https://www.howtogeek.com/112674/how-to-find-files-and-folders-in-linux-using-the-command-line/

~/source/shell
 find . -name \*hoge\*
./NO-ho-ge-dirName/hoge.txt
./NO-ho-ge-dirName/Depth2/hoge.txt
./hoge-fileName-and-inside.txt
./hoge-fileName-only.txt
./hoge-dirName
./hoge-dirName/hoge-fileName-only.txt
Enter fullscreen mode Exit fullscreen mode

find を使えばファイル名 or ディレクトリ名で検索できる。
これも深い階層まで見てくれる。

ここでは正規表現の * はバックスラッシュの \ とセットで使い

*hoge で hoge で終わるファイル名
hoge*\ で hoge から始まるファイル名
\hoge\ で hoge が含まれるファイル名

として検索できる。


find でファイル名で検索する

 find  -name \*hoge\* -type f
./NO-ho-ge-dirName/hoge.txt
./NO-ho-ge-dirName/Depth2/hoge.txt
./hoge-fileName-and-inside.txt
./hoge-fileName-only.txt
./hoge-dirName/hoge-fileName-only.txt
Enter fullscreen mode Exit fullscreen mode

-type f をつけるとファイルのみがヒットして


find でディレクトリ名で検索する

 find  -name \*hoge\* -type d
./hoge-dirName
Enter fullscreen mode Exit fullscreen mode

-type d をつけるとディレクトリのみがヒットする。


まとめ

ag hoge ディレクトリ名(optional)

でディレクトリの中で再帰的に hoge をファイル内に含むファイルを検索できる。

find ディレクトリ名(optional) -name *hoge* -type f

でディレクトリの中で再帰的に hoge をファイル名に含むファイルを検索できる

find ディレクトリ名(optional) -name *hoge* -type d

でディレクトリの中で再帰的に hoge をファイル名に含むディレクトリを検索できる

👋 Kindness is contagious

Please leave your appreciation by commenting on this post!

It takes one minute and is worth it for your career.

Get started

Thank you!

Top comments (0)

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay