bash GNU Parallel - 将输出重定向到具有特定名称的文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30760449/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
GNU Parallel - redirect output to a file with a specific name
提问by Crista23
In bash I am running GnuPG to decrypt some files and I would like the output to be redirected to a file having the same name, but a different extension. Basically, if my file is named
在 bash 中,我正在运行 GnuPG 来解密一些文件,并且我希望将输出重定向到具有相同名称但具有不同扩展名的文件。基本上,如果我的文件被命名
file1.sc.xz.gpg
the file which comes out after running the GnuPG tool I would like to be stored inside another file called
运行 GnuPG 工具后出现的文件我想存储在另一个名为
file1.sc.xz
I am currently trying
我目前正在尝试
find . -type f | parallel "gpg {} > {}.sc.xz"
but this results in a file called file1.sc.xz.gpg.sc.xz. How can I do this?
但这会生成一个名为 file1.sc.xz.gpg.sc.xz 的文件。我怎样才能做到这一点?
Later edit: I would like to do this inside one single bash command, without knowing the filename in advance.
稍后编辑:我想在一个单独的 bash 命令中执行此操作,而无需事先知道文件名。
回答by Maxim Egorushkin
You can use bash variable expansion to chop off the extension:
您可以使用 bash 变量扩展来切断扩展:
$ f=file1.sc.xz.gpg
$ echo ${f%.*}
file1.sc.xz
E.g.:
例如:
find . -type f | parallel bash -c 'f="{}"; g="${f%.*}"; gpg "$f" > "$g"'
Alternatively, use expansion of parallel
:
或者,使用扩展parallel
:
find . -type f | parallel 'gpg "{}" > "{.}"'
回答by Ole Tange
If file names are guaranteed not to contain \n:
如果保证文件名不包含\n:
find . -type f | parallel gpg {} '>' {.}
parallel gpg {} '>' {.} ::: *
If file names may contain \n:
如果文件名可能包含\n:
find . -type f -print0 | parallel -0 gpg {} '>' {.}
parallel -0 gpg {} '>' {.} ::: *
Note that opposite shell variables GNU Parallel's substitution strings should not be quoted. This will notcreate the file 12", but instead 12\" (which is wrong):
请注意,不应引用相反的 shell 变量 GNU Parallel 的替换字符串。这不会创建文件 12",而是 12\"(这是错误的):
parallel "touch '{}'" ::: '12"'
These will all do the right thing:
这些都将做正确的事情:
parallel touch '{}' ::: '12"'
parallel "touch {}" ::: '12"'
parallel touch {} ::: '12"'
回答by Charles Duffy
find . -type f -print0 | \
xargs -P 0 -n 1 -0 \
bash -s -c 'for f; do g=${f%.*}; gpg "$f" >"$g"; done' _
Right now this processes only one file per shell, but you could trivially modify that by changing the -n
value.
现在每个 shell 只处理一个文件,但是您可以通过更改-n
值来简单地修改它。