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 3 years have passed since last update.

【bash】検索したファイルを別のコマンドの引数にして実行する

Posted at

xargsコマンドを使う。
このコマンドを使えば、あるコマンドの結果を次のコマンドの引数に渡すことができる。

ユースケース

  • ディレクトリ内のファイルを検索し、そのファイルを削除したい

具体的にはこんなディレクトリがあって、sampleという名前を含むファイルだけ削除したいとする。

├── hoge.md
├── sample1.txt
├── sample2.txt
└── sample3.txt

その場合下記のコマンドで実現できる。

## findコマンドを使った場合
find . -name "*sample*" | xargs rm -rf

## lsとgrepコマンドを使った場合
ls | grep sample | xargs rm -rf

結果はこうなる。

└── hoge.md

以上です。

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?