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
# 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
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
# cd util
# pwd
# ls
GridService.class.gz               MDPDFXMLParser.class.gz            PDFColumn.class.gz                 PDFXMLParser.class.gz                 
MDExcelXmlParser.class.gz          MasterDetailsPrintWriter.class.gz  PDFRow.class.gz                    RGBColor.class.gz            
MDExcleWriter.class.gz             PDFCell.class.gz                   PDFWriter.class.gz                 xml2excel              

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)
    mv $i ${i%.gz} 2> /dev/null

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.