Say I have a 100GB file that I want to GPG encrypt on a system with 120GB hard drive memory and a small RAM.

How can I decrypt the file without using an external memory, that is gpg encrypt a file and gradually delete the unencrypted version in parallel?


Be careful: what if you delete part of a file, and the process is interrupted in the middle?

Disk space is cheap, so there is hardly ever any point in trying to save disk space in this way.

If you really have to do this, my recommendation is to process the file in chunks. Take a chunk, encrypt it, then delete that part of the original file. Then process the next chunk, and so on. This way, if the process is interrupted, you can remove the unfinished encrypted chunk and resume the process. With this approach, it's easier to process the file from the end to the beginning.

Warning: untested code. Assumes GNU or BusyBox utilities (e.g. Linux) and a 64-bit shell (to handle file sizes above 2GB).

set -e
input_size=$(stat -c %s myfile)
while [ "$input_size" -gt 0 ]; do
  chunk_size=$((input_size % fragment_size)) || chunk_size=$fragment_size
  input_size=$((input_size - fragment_size))
  fragment_name=$(printf fragment-%08d "$((input_size / fragment_size))")
  tail -c "$((input_size+1))" myfile | gpg -e >"$fragment_name.gpg.tmp"
  mv "$fragment_name.gpg.tmp" "$fragment_name.gpg"
  truncate -s "$input_size" myfile

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.