1
______________ myFolder

What can I replace with _____________ to recursively gzip every file starting at myFolder and have the gzip be overwrite the file (rename the gzip file to the original filename)?

3 Answers 3

4

You could also try

 find myFolder -type f -exec gzip {} \; -exec mv {}.gz {} \;
1
  • Aha, forgot about multiple -exec calls in find, me likey
    – Wesley Mason
    Jul 31, 2009 at 11:09
2

try:

find myFolder -type f -exec gzip {} +
1
  • Be aware that this will rename your files from myFile to myFile.gz
    – Kristof Provost
    Jul 30, 2009 at 10:42
1

A simple, not very elegant bash script is to simply cd in, gzip them all in a loop, and mv them back (gzip by default removes the non-compressed file):

#!/bin/bash
cd myFolder
for f in `find ./ -type f`
do
    gzip $f
    mv $f.gz $f
done

Put that in a file called "gzip_and_rename.sh" for example, chmod -775 and run it like ./gzip_and_rename.sh (if running from within myFolder itself, remove the "cd myFolder" line from the script).

3
  • And yes I do know backticks are evil, but as I said this is just a quick hack script.
    – Wesley Mason
    Jul 30, 2009 at 10:50
  • why are backticks evil, what do you replace them with ?
    – Anonymous
    Jul 30, 2009 at 13:36
  • See commandlinefu.com/commands/view/1387/backticks-are-evil
    – Wesley Mason
    Jul 31, 2009 at 10:13

Your Answer

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