LoginSignup
56
42

More than 5 years have passed since last update.

find で検索したファイルを一括で移動(mv)させる

Posted at

色々なところにファイルが散らばっているときに一括で移動させたい場合は
以下のようにコマンドを叩けば移動できます。

find `path` -name `ファイル名のパターン` | xargs -I% mv % `path`

やってみる

例えば以下の様にファイルが置かれていて

% tree
.
├── fuga
│   ├── image10.png
│   ├── image8.png
│   └── image9.png
└── hoge
    ├── fugafuga
    │   ├── image6.png
    │   ├── image7.png
    │   └── text3.txt
    ├── hogehoge
    │   ├── image4.png
    │   └── image5.png
    ├── image1.png
    ├── image2.png
    ├── image3.png
    ├── text1.txt
    └── text2.txt

一括で現在いるディレクトリに画像ファイル(png ファイル)を移動させたい場合は

find ./ -name '*.png' | xargs -I% mv % ./

という感じ。

実際に叩くと

% ls
fuga/        image1.png   image2.png   image4.png   image6.png   image8.png
hoge/        image10.png  image3.png   image5.png   image7.png   image9.png

% tree
.
├── fuga
├── hoge
│   ├── fugafuga
│   │   └── text3.txt
│   ├── hogehoge
│   ├── text1.txt
│   └── text2.txt
├── image1.png
├── image10.png
├── image2.png
├── image3.png
├── image4.png
├── image5.png
├── image6.png
├── image7.png
├── image8.png
└── image9.png

できた!

56
42
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
56
42