I have two directories: dir1 and dir2 In dir1 I have files like:

  • blacklist1.dat.bz2
  • blacklist2.dat.bz2
  • blacklist3.dat.bz2

and so on.

In dir2 I have encrypted files like:

  • blacklist1.dat.bz2.enc

I would like to encrypt all the files that (comparing dir1 with dir2) doesn't exist on dir2. For encrypt a file I use Openssl: - openssl enc -aes-256-cbc -in dir1/file -out dir2/file.enc -k Password

I did like this but I'm missing the condition of the if

for i in /dir1/*; do
   name=$(basename "$i")
   if [ **<$test doesn't exist on dir2>** ]; then
      openssl enc -aes-256-cbc -in dir1/name -out dir2/name.enc -k Password
  • So, your question is "how to test for file existence" ? Have you tried google ? – don_crissti Dec 20 '16 at 16:14
  • Sorry, I realized my question was a bit stupid. – LucaP Dec 20 '16 at 16:32

I was going to recommend trying to use something like rsync to determine a list of files missing/different and then use that listing. However, that wouldn't easily work with your needs.

You may also want to consider using find to help with possible recursion. Even diff'ing files from the 2 directories with some sed and grep commands could give you the file listing you want.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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