LoginSignup
175
146

More than 3 years have passed since last update.

shell でディレクトリ内のファイルに対してループ処理

Last updated at Posted at 2015-08-11

あるディレクトリ内でファイル一覧に対して同じ処理をしたい時のループの書き方

for file in `\find . -maxdepth 1 -type f`; do
    # TODO
done

\find . -maxdepth 1 -type f
カレントディレクトリの’ファイル’のみを取得している

\ls -F | grep -v /
でもファイルを絞り込めるが実行可能ファイルに「*」がついたりする

中身を結合してファイルに出力

for file in `\find . -maxdepth 1 -type f`; do
    cat $file >> out
done

再帰的にtxt のみファイルに出力して削除

for file in `\find . -name '*.txt'`; do
    cat $file >> out
    rm $file
done

png のみ バックアップ

for file in `\find . -maxdepth 1 -name '*.png'`; do
    cp $file $file.tmp
done
175
146
2

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
175
146