This question already has an answer here:

I want to rename my file with its substring.Because unfortunately renamed all the files in my server.

Now I want to remove suffix( .gz) of all the files including files in subdirectories also.

Below is avaliable files with extra .gz.

# pwd
/usr/apache-tomcat-6.0.36/webapps/rdnqa/WEB-INF/classes
# ls
META-INF                           jbpm.hibernate.cfg.xml.gz          jbpm.jpdl.cfg.xml.gz               jpdl-4.0.xsd.gz
config                             jbpm.hibernate.cfg.xml.mirror.gz   jbpm.mail.templates.xml.gz         jpdl-4.2.xsd.gz
ecnet                              jbpm.hibernate.cfg.xml.staging.gz  jbpm.repository.hbm.xml.gz         jpdl-4.3.xsd.gz
hibernate.queries.hbm.xml.gz       jbpm.history.hbm.xml.gz            jbpm.task.hbm.xml.gz               jpdl-4.4.xsd.gz
jbpm.businesscalendar.cfg.xml.gz   jbpm.identity.cfg.xml.gz           jbpm.task.lifecycle.xml.gz         labels
jbpm.cfg.xml.gz                    jbpm.identity.hbm.xml.gz           jbpm.tx.hibernate.cfg.xml.gz       log4j.properties.gz
jbpm.console.cfg.xml.gz            jbpm.jboss.idm.cfg.xml.gz          jbpm.tx.jta.cfg.xml.gz             nohup.out.gz
jbpm.default.cfg.xml.gz            jbpm.jbossremote.cfg.xml.gz        jbpm.tx.spring.cfg.xml.gz
jbpm.default.scriptmanager.xml.gz  jbpm.jobexecutor.cfg.xml.gz        jbpm.variable.types.xml.gz
jbpm.execution.hbm.xml.gz          jbpm.jpdl.bindings.xml.gz          jbpm.wire.bindings.xml.gz
# cd ecnet
# ls
core  jms   rd    util
# cd core
# ls
util
# cd util
# pwd
/usr/apache-tomcat-6.0.36/webapps/rdnqa/WEB-INF/classes/ecnet/core/util
# ls
GridService.class.gz               MDPDFXMLParser.class.gz            PDFColumn.class.gz                 PDFXMLParser.class.gz
GridService.java.gz                MDPDFXMLParser.java.gz             PDFColumn.java.gz                  PDFXMLParser.java.gz
MDExcelXmlParser.class.gz          MasterDetailsPrintWriter.class.gz  PDFRow.class.gz                    RGBColor.class.gz
MDExcelXmlParser.java.gz           MasterDetailsPrintWriter.java.gz   PDFRow.java.gz                     RGBColor.java.gz
MDExcleWriter.class.gz             PDFCell.class.gz                   PDFWriter.class.gz                 xml2excel
MDExcleWriter.java.gz              PDFCell.java.gz                    PDFWriter.java.gz
#

marked as duplicate by cuonglm, slm Jul 22 '14 at 11:47

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

up vote 2 down vote accepted

You can use rename (it's designed for that). Just execute this command in the folder where the *.gz file are:

rename -n 's/\.gz$//' *.gz

This removed the .gz extension from all files that have a .gz extension. Output should look like this:

hibernate.queries.hbm.xml.gz renamed as hibernate.queries.hbm.xml
jbpm.businesscalendar.cfg.xml.gz renamed as jbpm.businesscalendar.cfg.xml
jbpm.cfg.xml.gz renamed as jbpm.cfg.xml
...

Note: If the output is as desired, execute the command without the -n switch. That switch causes rename not to act, just show what files would have been renamed.

  • 2
    Note that there are two tools called rename. The Perl version which you have supplied the syntax for and there's also a util-linux version which supports a simpler syntax. – Matt Jul 22 '14 at 9:40

Following script might help:

for i in $(ls)
do
    mv $i ${i%.gz} 2> /dev/null
done

Basically it loops the files in directory and renames the files by removing 'gz' from the end.

The easiest and safest method would be to use find and tailor the command. E.g:

find . -type f -name '*.gz' -exec bash -c 'n=$1; mv -- "$n" "${n%.*}"' _ {} \;

Not the answer you're looking for? Browse other questions tagged or ask your own question.