-5

It seems that if I run gzip somefile.js, I get somefile.js.gz as the output.

However, I need to preserve the original file extension, i.e for somefile.js, I need it to remain somefile.js after gzipping.

How can I do that?

Edit: To be clear, I need to do this for dozens of files, so I can't just mv each one. I simply want to gzip all the static css / js files and then upload them to my CDN, so I can serve them as regular js / css files.

Ali
  • 261,656
  • 265
  • 575
  • 769
  • 1
    `mv somefile.js.gz somefile.js`?? – ElGavilan May 04 '14 at 15:04
  • Need to know. *Why* do you want this? The `gz` extension indicates the file has been gzipped, and needs to be `gunzip`ed before usage. – Jongware May 04 '14 at 15:05
  • Yep. Renaming it from .js.gz to .js doesn't magically make it a javascript file... – ElGavilan May 04 '14 at 15:06
  • @ElGavilan I'm trying to gzip over a hundred files, so its not practical to mv each one. – Ali May 04 '14 at 15:14
  • @Jongware So that I can just upload the gzipped files to my CDN and serve them as normal js/css files. – Ali May 04 '14 at 15:15
  • There seems to be no 'new file name' flag, and `gzip test -c > test` does not work -- probably because it overwrites the original file too soon. This suggests creating an alias or bash script is the better alternative. – Jongware May 04 '14 at 15:25
  • @Jongware Is there any script that will allow recursively going through every sub-directory and removing `.gz` from the file name? – Ali May 04 '14 at 15:31
  • Recursively!? I was just able to come up with this: `for i in *.gz; do mv $i ${i%.gz}; done` -- I'll have to leave the *recursive* part for someone else to find out. – Jongware May 04 '14 at 15:32
  • @Jongware That works for now, I'll just have to run this for each directory. – Ali May 04 '14 at 15:33

1 Answers1

4

If you really want to do so, you could simply use a for construct which exists in almost every shell (even on cmd.exe !). In Bourne or Posix sh flavour, it gives

for file in *.js *.css ; do gzip "$file" ; mv "$file.gz" "$file"; done

In Windows cmd.exe it should write (provided you've got a gzip command in your path):

for %file in (*.js *.css) do gzip %file && move %file.gz %file

But BEWARE : as others warned you, you will have binary gzipped files named foo.js or fee.css. If you serve them to standard browsers it definitely will not work !

Be sure to make a backup copy before trying that - it can easily be reversed, but you could at least lose time ...

EDIT : added quotes to shell command as suggested by gniourf_gniourf

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
  • Just for the fun of it: can this be made to work recursively over multiple folders? – Jongware May 04 '14 at 22:25
  • 1
    @Jongware : If you are using a shell, find is your friend for recursivity. You should look at [that question](http://stackoverflow.com/questions/307015/how-do-i-include-a-pipe-in-my-linux-find-exec-command) for references (and man find ...). Here it would give (beware : not really tested) : `find . \( -name *.js -o -name *.css \) -type f -printf "gzip \"%p\" ; mv \"%p.gz\" \"%p\" \n" | sh` – Serge Ballesta May 05 '14 at 06:10