Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GNU Parallel - redirect output to a file with a specific name

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.

like image 249
Crista23 Avatar asked Jun 10 '15 15:06

Crista23


People also ask

How do I redirect standard output to a file?

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.

What is redirect output file?

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.


2 Answers

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 "{}" > "{.}"'
like image 182
Maxim Egorushkin Avatar answered Oct 18 '22 15:10

Maxim Egorushkin


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"'
like image 3
Ole Tange Avatar answered Oct 18 '22 16:10

Ole Tange