Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a lot of files encrypted with gpg. All files have the same password. Is it possible to use xargs to decrypt files?

ls | xargs -n 1 gpg asks for the password for every file.

share|improve this question
You could save your password to a variable which gpg can be called with. It's also a good idea to avoid depending on the output of ls for operations such as this. – ephsmith Jul 6 '12 at 14:12
up vote 5 down vote accepted

Run gpg-agent or a similar program. Set up gpg to look for a running agent, as explained in the documentation. Enter the passphrase in the agent once and for all (for this session).

Note that ls | xargs -n 1 gpg only works if your file names do not contain any special characters. Generally speaking, don't parse the output of ls, and xargs is pointless when you want to run the program once per file. Do this instead:

for x in *.gpg; do gpg "$x"; done
share|improve this answer

You can do this :

for i in *; do 
  /usr/bin/gpg --batch --passphrase-fd 3  --decrypt $i 3<pass > $i.decoded ; 

When "pass" is a complete path to a file that contains your passphrase.

Files will be decoded to [same name].decoded.

share|improve this answer
Don't use * here. Use ./* so that you can be sure that the expansion of $i does not begin with - (making it look like another option). – James Youngman Jul 6 '12 at 14:49
Can also use --passphrase or --passphrase-file instead. – rsaw Jul 6 '12 at 14:54
@JamesYoungman nice tip, +1 – Tim Jul 6 '12 at 15:16

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.