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
file1.sc.xz.gpg
the file which comes out after running the GnuPG tool I would like to be stored inside another file called
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?
Later edit: I would like to do this inside one single bash command, without knowing the filename in advance.
Redirecting stdout and stderr to a file: The I/O streams can be redirected by putting the n> operator in use, where n is the file descriptor number. For redirecting stdout, we use “1>” and for stderr, “2>” is added as an operator.
If you redirect the output, you can save the output from a command in a file instead. The output is sent to the file rather than to the screen. At the end of any command, enter: >filename For example: cat file1 file2 file3 >outfile. writes the contents of the three files into another file called outfile.
You can use bash variable expansion to chop off the extension:
$ 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
:
find . -type f | parallel 'gpg "{}" > "{.}"'
If file names are guaranteed not to contain \n:
find . -type f | parallel gpg {} '>' {.}
parallel gpg {} '>' {.} ::: *
If file names may contain \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 not create the file 12", but instead 12\" (which is wrong):
parallel "touch '{}'" ::: '12"'
These will all do the right thing:
parallel touch '{}' ::: '12"'
parallel "touch {}" ::: '12"'
parallel touch {} ::: '12"'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With