LoginSignup
7
6

More than 1 year has passed since last update.

find コマンドの出力を while read ループ処理(シェルスクリプト)

Last updated at Posted at 2018-05-23

こんにちは。
find コマンドで検索した複数のファイルを処理するシェルスクリプトを作りました。

while read を使ってファイルを一つずつループ処理し、下記例は ls コマンドのような働きをします(極めて多数のファイルにも対応可能のはず)。

$ ./ls.sh "*.jpg"
./1.jpg
./2.jpg
./3.jpg
ls.sh
#!/bin/sh
filePattern="$1"
find . -maxdepth 1 -name "$filePattern" | while read -r fname
do
  echo "$fname" | grep -q "^/" && fname="."$fname
  echo "$fname"
done
exit $?

上記の echo $fname|grep "^/" の箇所は、ファイル名先頭の . の欠落の発生に気が付いたので善後策です(まだ原因は調べていません)。

7
6
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
7
6