複製目錄列表中的所有 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

您可以使用 shell 循環:

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不調用 shell 而是調用您直接告訴它的任何實用程序,因此它不會擴展*pdf到文件列表。這意味著您需要使用 shell 明確呼叫它:

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

相關內容