Skip to content

Instantly share code, notes, and snippets.

@Hayao0819
Created December 24, 2019 03:08
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Hayao0819/c95c4f5c187c0544f4cbfb565f3ba3f0 to your computer and use it in GitHub Desktop.
Save Hayao0819/c95c4f5c187c0544f4cbfb565f3ba3f0 to your computer and use it in GitHub Desktop.
指定したディレクトリ内のテキストファイルをcatで開き、検索語句があればそのファイルのパスを表示するスクリプト
#!/usr/bin/env bash
if [[ ! -d $1 || -z $1 ]]; then
echo "ディレクトリを指定してください。"
exit 1
fi
if [[ -z $2 ]]; then
echo "検索する語句がありません。"
exit 1
fi
dir=$1
word=$2
dir_list=( $(find ${dir} -type f 2> /dev/null | perl -nle 'print if -f && -T') )
for file in ${dir_list[@]}; do
if [[ -n $(cat $file 2> /dev/null | grep $word) ]]; then
echo $file
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment