3

So I am using amazon to serve a few files.

I want to gzip these files before I upload them

first I copy templates into a new folder

cp -r templates/ templatesGZIP

then I GZIP that folder

gzip -r templatesGZIP

the problem is that this adds .gz to all the file names. so for example homeTemplate.html changes to homeTemplate.html.gz

is there any way when running gzip -r templatesGZIP that I state I want to keep the extensions the same

Thanks

Dan

Dan
  • 1,295
  • 2
  • 22
  • 46

5 Answers5

3

Bash script: Gzip an entire folder and keep files extensions same

This would surely help first compress them with gzip and then rename them.

Thanks & Regards,
Alok

Community
  • 1
  • 1
linux_fanatic
  • 4,767
  • 3
  • 19
  • 20
2

gzip just does one thing, turns a single file into a gz archive. What you need is a tar.gz file. Your friend is tar, which can use gzip as well

cp -r templates templatesGZIP
tar czf templatesGZIP.tar.gz templatesGZIP

Backround: tar does another one thing well: it turns a directory structure into a single file. The tar commands above, explained:

  • c = create
  • z = zipped, default gzip
  • f FILE = name of the archive file
SzG
  • 12,333
  • 4
  • 28
  • 41
1

after copying the directory

find templatesGZIP -type f ! -wholename *images* -exec gzip {} \; -exec mv {}.gz {} \;
Dan
  • 1,295
  • 2
  • 22
  • 46
0

You can use the stdout as a temporary buffer and write the output in a file of your choise:

gzip -cr templatesGZIP > outputfile.extension
Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
0

There is no option in gzip to do this. Using the two-directory method, the following should do the trick

for in_file in $(find templates -type f)
do
    out_file="templatesGZIP/${in_file#templates/}"
    mkdir -p "$(dirname "$out_file")"
    gzip -c <"$in_file" >"$out_file"
done

With this method, there's no need for the cp command.

This has been tested.

One comment on this - it's uncommon to see gzip'ed files without an extension.

ash
  • 4,867
  • 1
  • 23
  • 33