Question

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.

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top