ディレクトリのリスト内のすべてのPDFをコピーする

ディレクトリのリスト内のすべてのPDFをコピーする

ディレクトリのリスト内のすべての PDF を一時ディレクトリにコピーしようとしています。該当するディレクトリのリストを取得するには、次のコマンドを使用します。

dirname $(grep -r <my search criteria> /c01/app/*sup) | sort -u

これは、.sup検索条件に一致するすべてのファイルを検索します。これらのディレクトリには、コピーしたい PDF があります。

編集: 更新されたコマンドを実行する場合:

dirname $(grep -r <my search criteria> /c01/app/09/*/*/*/*/*/system/output/*sup)) | sort -u | xargs -I {} cp -- {}/*pdf .

次のエラーが発生します: cp: cannot stat ‘/c01/app/09/ba/016/500/000/082/system/output/*pdf’: No such file or directory

さらに出力:

[user@server payment]$ dirname $(grep -r <my search> /c01/app/09/*/*/*/*/*/system/output/*sup) | sort -u | xargs -I {} cp -- {}/*pdf .

cp: cannot stat ‘./*pdf’: No such file or directory
cp: cannot stat ‘/c01/app/09/ad/016/500/000/026/system/output/*pdf’: No such file or directory
cp: cannot stat ‘/c01/app/09/ad/016/500/000/030/system/output/*pdf’: No such file or directory
cp: cannot stat ‘/c01/app/09/ad/016/500/000/033/system/output/*pdf’: No such file or directory
cp: cannot stat ‘/c01/app/09/ad/016/500/000/086/system/output/*pdf’: No such file or directory
cp: cannot stat ‘/c01/app/09/ad/016/500/000/0a7/system/output/*pdf’: No such file or directory
cp: cannot stat ‘/c01/app/09/ad/016/500/000/112/system/output/*pdf’: No such file or directory
cp: cannot stat ‘/c01/app/09/ad/016/500/000/191/system/output/*pdf’: No such file or directory
cp: cannot stat ‘/c01/app/09/ae/016/500/000/030/system/output/*pdf’: No such file or directory
cp: cannot stat ‘/c01/app/09/ae/016/500/000/033/system/output/*pdf’: No such file or directory
cp: cannot stat ‘/c01/app/09/ae/016/500/000/036/system/output/*pdf’: No such file or directory
cp: cannot stat ‘/c01/app/09/ae/016/500/000/03b/system/output/*pdf’: No such file or directory
cp: cannot stat ‘/c01/app/09/ae/016/500/000/043/system/output/*pdf’: No such file or directory
cp: cannot stat ‘/c01/app/09/ae/016/500/000/04b/system/output/*pdf’: No such file or directory
cp: cannot stat ‘/c01/app/09/ae/016/500/000/04e/system/output/*pdf’: No such file or directory
cp: cannot stat ‘/c01/app/09/ae/016/500/000/087/system/output/*pdf’: No such file or directory
cp: cannot stat ‘/c01/app/09/ae/016/500/000/08d/system/output/*pdf’: No such file or directory
cp: cannot stat ‘/c01/app/09/ae/016/500/000/096/system/output/*pdf’: No such file or directory
cp: cannot stat ‘/c01/app/09/ae/016/500/000/09a/system/output/*pdf’: No such file or directory
cp: cannot stat ‘/c01/app/09/ae/016/500/000/0a0/system/output/*pdf’: No such file or directory
cp: cannot stat ‘/c01/app/09/ae/016/500/000/0a3/system/output/*pdf’: No such file or directory
cp: cannot stat ‘/c01/app/09/ae/016/500/000/185/system/output/*pdf’: No such file or directory
cp: cannot stat ‘/c01/app/09/ae/016/500/000/1c1/system/output/*pdf’: No such file or directory
cp: cannot stat ‘/c01/app/09/ae/016/500/000/1c4/system/output/*pdf’: No such file or directory
cp: cannot stat ‘/c01/app/09/af/016/500/000/007/system/output/*pdf’: No such file or directory
cp: cannot stat ‘/c01/app/09/af/016/500/000/00a/system/output/*pdf’: No such file or directory
cp: cannot stat ‘/c01/app/09/af/016/500/000/00d/system/output/*pdf’: No such file or directory
cp: cannot stat ‘/c01/app/09/af/016/500/000/011/system/output/*pdf’: No such file or directory
cp: cannot stat ‘/c01/app/09/af/016/500/000/01c/system/output/*pdf’: No such file or directory
cp: cannot stat ‘/c01/app/09/af/016/500/000/01f/system/output/*pdf’: No such file or directory
cp: cannot stat ‘/c01/app/09/af/016/500/000/02a/system/output/*pdf’: No such file or directory
cp: cannot stat ‘/c01/app/09/af/016/500/000/042/system/output/*pdf’: No such file or directory
cp: cannot stat ‘/c01/app/09/af/016/500/000/08b/system/output/*pdf’: No such file or directory
cp: cannot stat ‘/c01/app/09/b2/016/500/000/006/system/output/*pdf’: No such file or directory
cp: cannot stat ‘/c01/app/09/b2/016/500/000/00b/system/output/*pdf’: No such file or directory
cp: cannot stat ‘/c01/app/09/b2/016/500/000/018/system/output/*pdf’: No such file or directory
cp: cannot stat ‘/c01/app/09/b2/016/500/000/024/system/output/*pdf’: No such file or directory
cp: cannot stat ‘/c01/app/09/b2/016/500/000/026/system/output/*pdf’: No such file or directory
cp: cannot stat ‘/c01/app/09/b2/016/500/000/02e/system/output/*pdf’: No such file or directory
cp: cannot stat ‘/c01/app/09/b2/016/500/000/037/system/output/*pdf’: No such file or directory
cp: cannot stat ‘/c01/app/09/b3/016/500/000/007/system/output/*pdf’: No such file or directory
cp: cannot stat ‘/c01/app/09/b3/016/500/000/00a/system/output/*pdf’: No such file or directory
cp: cannot stat ‘/c01/app/09/b3/016/500/000/00e/system/output/*pdf’: No such file or directory
cp: cannot stat ‘/c01/app/09/b3/016/500/000/012/system/output/*pdf’: No such file or directory
cp: cannot stat ‘/c01/app/09/b3/016/500/000/017/system/output/*pdf’: No such file or directory
cp: cannot stat ‘/c01/app/09/b3/016/500/000/021/system/output/*pdf’: No such file or directory
cp: cannot stat ‘/c01/app/09/b3/016/500/000/08d/system/output/*pdf’: No such file or directory
cp: cannot stat ‘/c01/app/09/b3/016/500/000/15e/system/output/*pdf’: No such file or directory
cp: cannot stat ‘/c01/app/09/b4/016/500/000/016/system/output/*pdf’: No such file or directory
cp: cannot stat ‘/c01/app/09/b4/016/500/000/018/system/output/*pdf’: No such file or directory
cp: cannot stat ‘/c01/app/09/b4/016/500/000/061/system/output/*pdf’: No such file or directory
cp: cannot stat ‘/c01/app/09/b4/016/500/000/076/system/output/*pdf’: No such file or directory
cp: cannot stat ‘/c01/app/09/b4/016/500/000/086/system/output/*pdf’: No such file or directory
cp: cannot stat ‘/c01/app/09/b4/016/500/000/088/system/output/*pdf’: No such file or directory
cp: cannot stat ‘/c01/app/09/b4/016/500/000/0df/system/output/*pdf’: No such file or directory
cp: cannot stat ‘/c01/app/09/b5/016/500/000/00a/system/output/*pdf’: No such file or directory
cp: cannot stat ‘/c01/app/09/b5/016/500/000/032/system/output/*pdf’: No such file or directory
cp: cannot stat ‘/c01/app/09/b5/016/500/000/05b/system/output/*pdf’: No such file or directory
cp: cannot stat ‘/c01/app/09/b5/016/500/000/066/system/output/*pdf’: No such file or directory
cp: cannot stat ‘/c01/app/09/b9/016/500/000/004/system/output/*pdf’: No such file or directory
cp: cannot stat ‘/c01/app/09/b9/016/500/000/014/system/output/*pdf’: No such file or directory
cp: cannot stat ‘/c01/app/09/b9/016/500/000/01a/system/output/*pdf’: No such file or directory
cp: cannot stat ‘/c01/app/09/b9/016/500/000/02e/system/output/*pdf’: No such file or directory
cp: cannot stat ‘/c01/app/09/b9/016/500/000/03b/system/output/*pdf’: No such file or directory
cp: cannot stat ‘/c01/app/09/b9/016/500/000/081/system/output/*pdf’: No such file or directory
cp: cannot stat ‘/c01/app/09/b9/016/500/000/08b/system/output/*pdf’: No such file or directory
cp: cannot stat ‘/c01/app/09/b9/016/500/000/08e/system/output/*pdf’: No such file or directory
cp: cannot stat ‘/c01/app/09/b9/016/500/000/091/system/output/*pdf’: No such file or directory
cp: cannot stat ‘/c01/app/09/b9/016/500/000/098/system/output/*pdf’: No such file or directory

提案されたコマンドからの出力:

[user@server payment]$ dirname $(grep <my search> /c01/app/09/*/*/*/*/*/system/output/*sup) | sort -u | head
.
/c01/app/09/ad/016/500/000/026/system/output
/c01/app/09/ad/016/500/000/086/system/output
/c01/app/09/ad/016/500/000/0a7/system/output
/c01/app/09/ad/016/500/000/112/system/output
/c01/app/09/ad/016/500/000/191/system/output
/c01/app/09/ae/016/500/000/030/system/output
/c01/app/09/ae/016/500/000/033/system/output
/c01/app/09/ae/016/500/000/036/system/output
/c01/app/09/ae/016/500/000/03b/system/output

答え1

シェルループを使用できます:

dirname $(grep -r <my search criteria> /c01/app/*sup) | 
    sort -u |
        while IFS= read -r dir; do
            cp -- "$dir"/*pdf /path/to/target/dir
        done

を使っても同じことができますxargsが、 はxargsシェルを呼び出さず、代わりに指示されたユーティリティを直接呼び出すため、*pdfファイルのリストに展開されません。つまり、 を明示的にシェルで呼び出す必要があります。

    dirname $(grep -r <my search criteria> /c01/app/*sup) | 
        sort -u |
            xargs -I {} sh -c "cp -- {}/*pdf /path/to/target/dir"

関連情報