xargs内にネストされたコマンド

xargs内にネストされたコマンド

ファイルフォルダフォルダがあります。すべてのファイルを親ファイルにコピーしようとしています。次のコマンドはdirname "{}"findコマンドの前に実行されるため、機能しません。何ができますか?

find . -name "*" -type f | xargs -I "{}" cp "{}" `dirname "{}"`

ベストアンサー1

現在の作業ディレクトリからディレクトリツリーの下のすべてのファイルを各ファイルの親ディレクトリに移動するには、breakまたはを実行xargsする必要さえありません。dirnamefind

$ tree
.
+--- dir1
|   +--- somefile
+--- dir2
|   +--- someotherfile
$ find . -type f -execdir mv "{}" ../ \;
$ tree
.
+--- dir1
+--- dir2
+--- somefile
+--- someotherfile

execdirforオプションは、find一致する各ファイルを含むディレクトリで指定されたコマンドを実行します。

おすすめ記事