GNU Parallel - redirecting 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 with the same name but with a different extension. Basically, if my file is called

file1.sc.xz.gpg

      

the file that appears after running the GnuPG tool, I would like to save it in another file named

file1.sc.xz 

      

I am currently trying

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

      

but this results in file1.sc.xz.gpg.sc.xz file. How can i do this?

Further editing: I would like to do this inside a single bash command without knowing the filename in advance.

+3


source to share


3 answers


You can use bash variable expansion to strip off the expansion:

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

      

eg:.

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

      




Alternatively use the extension parallel

:

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

      

+3


source


If filenames are guaranteed not to contain \ n:

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

      

If filenames may contain \ n:

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

      



Note that opposite GNU shell variables. Parallel replacement lines should not be specified. This will not create file 12 ", but instead 12 \" (which is not correct):

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

      

They will all do the right thing:

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

      

+1


source


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 only processes one file per shell, but you can trivially change this by changing the value -n

.

0


source







All Articles