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.

Michael Hampton
  • 244,070
  • 43
  • 506
  • 972

2 Answers2

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).

kubanczyk
  • 13,812
  • 5
  • 41
  • 55
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
Sirch
  • 5,785
  • 4
  • 20
  • 36
  • 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 08 '18 at 17:26