0
0

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 1 year has passed since last update.

【Bash】指定した条件を満たす名前のファイルだけを抽出し、指定先にコピーする

Posted at

要件

test[任意の文字列].txtを満たすファイルを抽出し、別ディレクトリにコピーしたい

期待する動き(コピーの様子)

inputディレクトリに存在するtest[任意の文字列].txtを、outputディレクトリにコピーする。

Before

.
├── input
│   ├── bar.txt
│   ├── hoge
│   │   ├── bar2.txt
│   │   ├── tes1.txt
│   │   ├── test3.txt
│   │   └── test4.txt
│   ├── test1.txt
│   └── test2.txt
└── output

After

.
├── input
│   ├── bar.txt
│   ├── hoge
│   │   ├── bar2.txt
│   │   ├── tes1.txt
│   │   ├── test3.txt
│   │   └── test4.txt
│   ├── test1.txt
│   └── test2.txt
└── output
    ├── hoge
    │   ├── test3.txt
    │   └── test4.txt
    ├── test1.txt
    └── test2.txt

試したこと

コマンド

inputディレクトリで次のコマンドを実行する。
(カレントディレクトリ次第では想定外の動きをするため、絶対パスでディレクトリを指定するなど工夫したほうがいいかもしれない)

$ find ./ -type d | xargs -I{} mkdir -p ../output/{} && find -name "test*.txt" | xargs -t -I{} cp {} ../output/{}

コマンドの実行内容

  1. input以下のディレクトリ構造をoutputディレクトリにコピーする
    (cpコマンドでは、存在しないディレクトリを作成して処理を続ける・・ということはできないため)
  2. 条件を満たすファイルを指定ディレクトリにコピーする

参考

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?