14
14

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

別々のディレクトリにあるファイルを条件指定してcpする

Last updated at Posted at 2017-05-18

目的

  • カレントディレクトリより下にある全ディレクトリ・全ファイルを再帰的に検索して,特定の条件を満たすファイルだけをカレントディレクトリにコピーしたい.

方法

  • findコマンドに-execオプションをつける.

使用例

カレントディレクトリ直下にある全てのpngファイルを~/destというディレクトリにコピーする

find . -name "*.png" -exec cp {} ~/dest/ \;
  • 解説
    • cp直後の{}に検索結果のファイル名のリストが格納される.
    • 最後の\;-execの終わりを表す記号.末尾に必ずつける.
  • 応用例
    • 他の動作:
      • cpmvに変更すれば移動・リネーム
      • cprmに変更すれば削除
    • ワイルドカードの使用:
      • "*.png""hoge*/*.png"に変更すれば,ファイル名がhogeで始まるすべてのpngファイルが操作対象になる.
      • find .の部分をfind fooに変更すれば,カレントディレクトリ直下ではなくfooというディレクトリ直下のファイルが操作対象になる.
    • 確認しながら操作
      • -exec-okに変更すれば,操作を行う前に逐一確認できる
        • find foo -name "hoge*" -ok mv {} ~/dest \;と書けばfooディレクトリ直下にある,ファイルがhogeから始まる全てのファイルに対して1個ずつ「移動しますか?」と確認されyesと入力すれば移動される

参考文献

14
14
1

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?