簡體   English   中英

GNU Parallel - 將輸出重定向到具有特定名稱的文件

[英]GNU Parallel - redirect output to a file with a specific name

在 bash 中,我正在運行 GnuPG 來解密一些文件,我希望將輸出重定向到具有相同名稱但具有不同擴展名的文件。 基本上,如果我的文件被命名

file1.sc.xz.gpg

運行 GnuPG 工具后出現的文件我想存儲在另一個名為

file1.sc.xz 

我目前正在嘗試

find . -type f | parallel "gpg {} > {}.sc.xz"

但這會生成一個名為 file1.sc.xz.gpg.sc.xz 的文件。 我怎樣才能做到這一點?

稍后編輯:我想在一個單獨的 bash 命令中執行此操作,而無需事先知道文件名。

您可以使用 bash 變量擴展來切斷擴展:

$ f=file1.sc.xz.gpg
$ echo ${f%.*}
file1.sc.xz

例如:

find . -type f | parallel bash -c 'f="{}"; g="${f%.*}"; gpg "$f" > "$g"'

或者,使用parallel擴展:

find . -type f | parallel 'gpg "{}" > "{.}"'

如果保證文件名不包含\\n:

find . -type f | parallel gpg {} '>' {.}
parallel gpg {} '>' {.} ::: *

如果文件名可能包含\\n:

find . -type f -print0 | parallel -0 gpg {} '>' {.}
parallel -0 gpg {} '>' {.} ::: *

請注意,不應引用相反的 shell 變量 GNU Parallel 的替換字符串。 不會創建文件 12",而是 12\\"(這是錯誤的):

parallel "touch '{}'" ::: '12"'

這些都將做正確的事情:

parallel touch '{}' ::: '12"'
parallel "touch {}" ::: '12"'
parallel touch {} ::: '12"'
find . -type f -print0 | \
  xargs -P 0 -n 1 -0 \
    bash -s -c 'for f; do g=${f%.*}; gpg "$f" >"$g"; done' _

現在每個 shell 只處理一個文件,但您可以通過更改-n值輕松修改它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM