Search code examples
bashaescbc-mac

Encrypt every item inside a folder in Bash


I have a code which is supposed to encrypt every file within a folder but when I put it (the encryption code) within the infinite loop it does not work.

touch Out_file.txt
ls > Out_file.txt
i = 0
while:
i = i + 1
line=$(head -n $i Out_file.txt)
openssl cbc -aes-256-cbc -pass pass:Hello123  -p -in $line -out $line.enc
done

It might be able to encrypt all your files within the folder you are running the program in so please proceed with caution


Solution

  • How about:

    (just simplifying how we generate the list of files and run through them)

    
    touch Out_file.txt
    #write only filename to Out_file.txt
    ls -l | awk 'NF>1{print $NF}' > Out_file.txt
    while read filename; do
      openssl cbc -aes-256-cbc -pass pass:Hello123  -p -in $filename -out $filename.enc
    done <Out_file.txt
    
    

    Good Luck.