Need to change of file

I need to change the filed named from

3479238.conf.bad
3274293.conf.bad
2389749.conf.bad
3242397.conf.bad

Need to move as

3479238.conf
3274293.conf
2389749.conf
3242397.conf

Via shell script or any find command.

use man cut

Assuming the only files present with the name *.conf.bad are the ones to be renamed then:

for SFILE in *.conf.bad; do
  TFILE=`echo ${SFILE} | sed 's/.bad//'`
  mv ${SFILE} ${TFILE}
done

Would do the job.

Thank you, it worked out...

That's useless use of echo & cut

for FILE in *.conf.bad
do
   mv ${FILE} ${FILE%.*}
done

Not "useless", just not as efficient (and it was sed not cut :wink: ).

"TonyFullerMalv" solution is cool because it follows logical lines. "danmero" solution is naive.

Slight improvement if we quote every filename and every ".", and allow for no files:

ls -1 *\.conf\.bad 2>/dev/null | while read SFILE
do
  TFILE=`echo "${SFILE}" | sed 's/.bad//'`
  mv "${SFILE}" "${TFILE}"
done

Can you explaing why? Let's see the difference on 10000 test files.

time for SFILE in *.conf.bad; do   TFILE=`echo ${SFILE} | sed 's/.bad//'`;   mv ${SFILE} ${TFILE}; done

real    1m54.197s
user    0m28.088s
sys     1m20.431s

And now using shell built-in parameter expansion.

# time for FILE in *.conf.bad; do mv ${FILE} ${FILE%.*}; done

real    0m32.753s
user    0m7.041s
sys     0m24.094s

Look like the shell only is almost 4 time faster that your solution. You should think about that :rolleyes: