0

I need to compress a considerable number of pdf and txt files but I want to leave the file structure untouched. The directories have many, many subdirectories.

When I want to compress files of a specific type in a single directory I run:

for i in `find | grep -E "\.pdf$|\.txt$"`; do gzip "$i" ; done

but this would take me years to do by hand.

  • 1
    It looks like you already have a command to do that. What exactly do you want to know? – Michael Hampton May 8 '18 at 16:15
3

Try this in top-level directory.

find . -type f -mmin +1 '(' -iname '*.txt' -o -iname '*.pdf' ')' -exec gzip {} ';'

I took the liberty to only gzip files older than 2 minutes, so that it doesn't touch files that are likely to be presently being written to (mmin +1 does that).

1

Im not certain why your existing 'command' isnt working. This script should though. Run in a parent directory.

#!/bin/bash

find . -type f| while read FILE
do
  if grep -E "\.pdf$|\.txt$"; then
    gzip ${FILE}
  fi
done
  • You are right, my original command did work, I just assumed (yes, I know) that it wouldn't as I only tested it in a single directory with no sub directories. Thank you for your reply :) – Patrick Morton May 8 '18 at 17:26

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.