文字列のfind

[find] 実行して理解 findコマンドで出力したパスを他コマンドに使用

created:2025/11/01 updated:2025/11/05

前書き

このリポジトリは、findコマンドの-execオプションを使用してgrepコマンドにファイルパスを渡して実行します

インストールする必要のあるコマンド

  1. git

クイックスタート

上記のコマンドをインストール済みの方は、以下のコマンドを実行してリポジトリからダウンロード後、ディレクトリを移動し、findコマンドの-execオプションを使用してgrepコマンドを実行してみてください

ubuntu

git clone https://github.com/trygfmi/find_-exec_getMatchedFilepath
cd find_-exec_getMatchedFilepath
find . -type f -exec grep -l "hello" {} +
find . -type f -exec grep -l "world" {} +
出力結果
./README.md
./folder1/example3.txt
./folder1/folder3/example5.txt

./README.md
./folder1/example2.txt
./folder1/folder2/example4.txt

macos

MacPorts

source ~/bashrc_folder/macports_alias
git clone https://github.com/trygfmi/find_-exec_getMatchedFilepath
cd find_-exec_getMatchedFilepath
find . -type f -exec grep -l "hello" {} +
find . -type f -exec grep -l "world" {} +
出力結果
./README.md
./folder1/example3.txt
./folder1/folder3/example5.txt

./README.md
./folder1/example2.txt
./folder1/folder2/example4.txt

windows

WSL2

git clone https://github.com/trygfmi/find_-exec_getMatchedFilepath
cd find_-exec_getMatchedFilepath
find . -type f -exec grep -l "hello" {} +
find . -type f -exec grep -l "world" {} +
出力結果
./README.md
./folder1/example3.txt
./folder1/folder3/example5.txt

./README.md
./folder1/example2.txt
./folder1/folder2/example4.txt

MSYS2 MINGW64

git clone https://github.com/trygfmi/find_-exec_getMatchedFilepath
cd find_-exec_getMatchedFilepath
find . -type f -exec grep -l "hello" {} +
find . -type f -exec grep -l "world" {} +
出力結果
./folder1/example3.txt
./folder1/folder3/example5.txt
./README.md

./folder1/example2.txt
./folder1/folder2/example4.txt
./README.md

実行手順

ubuntu

クリックして詳細を開く

事前確認

以下のコマンドを端末に打ち込んでcommand not foundが出なければokです

git --version

preinstall

端末にcommand not foundが出たコマンドを以下のコマンドでインストールしてください

sudo apt install git

コマンド

以下のコマンドを端末で実行することで詳細のような文字列が出力されるはずです

git clone https://github.com/trygfmi/find_-exec_getMatchedFilepath
cd find_-exec_getMatchedFilepath
find . -type f -exec grep -l "hello" {} +
find . -type f -exec grep -l "world" {} +
詳細
./README.md
./folder1/example3.txt
./folder1/folder3/example5.txt

./README.md
./folder1/example2.txt
./folder1/folder2/example4.txt

macos

クリックして詳細を開く

事前確認

以下のコマンドをターミナルに打ち込んでcommand not foundが出なければokです

source ~/bashrc_folder/macports_alias
git --version

※macosはMacPortsパッケージマネージャを使用してコマンドを管理します。もしインストールしていない方は以下のリンクからMacPortsのインストール手順をご覧ください
またコマンドに別名を設定して既存の環境と競合しないでコマンドを呼び出せるようにします。
初めてこのブログを利用する方は、以下の2つの記事を参考に環境構築してください

preinstall

ターミナルでcommand not foundが出たコマンドを以下のコマンドでインストールしてエイリアスを設定してください

sudo port install git
echo 'alias git="/opt/local/bin/git"' >> ~/bashrc_folder/macports_alias

コマンド

以下のコマンドをターミナルで実行することで詳細のような文字列が出力されるはずです

source ~/bashrc_folder/macports_alias
git clone https://github.com/trygfmi/find_-exec_getMatchedFilepath
cd find_-exec_getMatchedFilepath
find . -type f -exec grep -l "hello" {} +
find . -type f -exec grep -l "world" {} +
詳細
./README.md
./folder1/example3.txt
./folder1/folder3/example5.txt

./README.md
./folder1/example2.txt
./folder1/folder2/example4.txt

windows

クリックして詳細を開く

事前確認

以下のコマンドをプロンプトに打ち込んでcommand not foundが出なければokです

WSL2
git --version
MSYS2 MINGW64
git --version

※windowsはWSL2とMSYS2 MINGW64で確認しています。可能な限りWSL2をインストールしていただいて、もし設定できなかった場合はMSYS2をインストールすることで実行できますが、所々WSL2でしか実行できないコマンドが出てくるかもしれません。WSL2とMSYS2のインストール方法は下記の記事を参考にしてください

preinstall

プロンプトでcommand not foundが出たコマンドを以下のコマンドでインストールしてください

WSL2
sudo apt install git
MSYS2 MINGW64
pacman --sync git

コマンド

以下のコマンドをプロンプトで実行することで詳細のような文字列が出力されるはずです

WSL2
git clone https://github.com/trygfmi/find_-exec_getMatchedFilepath
cd find_-exec_getMatchedFilepath
find . -type f -exec grep -l "hello" {} +
find . -type f -exec grep -l "world" {} +
詳細
./README.md
./folder1/example3.txt
./folder1/folder3/example5.txt

./README.md
./folder1/example2.txt
./folder1/folder2/example4.txt
MSYS2 MINGW64
git clone https://github.com/trygfmi/find_-exec_getMatchedFilepath
cd find_-exec_getMatchedFilepath
find . -type f -exec grep -l "hello" {} +
find . -type f -exec grep -l "world" {} +
詳細
./folder1/example3.txt
./folder1/folder3/example5.txt
./README.md

./folder1/example2.txt
./folder1/folder2/example4.txt
./README.md

後書き

find . -type f -exec grep -l "hello" {} +

上記のシェルは、カレントディレクトリ以下のファイルで、helloが含まれるファイルを{}のファイルパスからできる限り1回のプロセスで実行するという意味です

.は現在のディレクトリ、-type fはファイルを対象、-execオプションの後にコマンドを指定、-lオプションでファイルパスだけを出力、検索文字列として”hello”を指定、findコマンドで出力された文字列が格納された記号{}、できるだけまとめて1回のプロセスで実行させる文字+、です

find . -type f -exec grep -l "hello" {} \;

でも実行できますが、1つのファイルパスに対して1つのプロセスで実行するため数が多いと処理が遅くなるようです