The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com




View Single Post in the UNIX and Linux Forums - Click on the Thread or Permalink to View Entire Thread -->
  #5 (permalink)  
Old 01-05-2009
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,557
Quote:
Originally Posted by shekhar_v4 View Post
Hi,

I have a directory with files names like ABC20090101AXY.txt, ABC20090102BZ.txt,ABC20090101COF.txt etc. The digits in the filenames represent the date. I want to rename the files to AXY.txt, BZ.txt and COF.txt

I tried with this code.

myfile= date '+ABC%Y%m%d'
for i in *.txt
do
mv $i `echo $i | sed 's/$myfile//'`
done

But its giving message

mv: ABC20090101AXY.txt and ABC20090101AXY.txt are identical

Can u help me finding the problem.

Regards,
Shekhar
if you have Python and can use it, here's a ready script you can use. Usage examples

Code:
# ls -1
ABC20090101AXY.txt
ABC20090101COF.txt
ABC20090102BZ.txt

# filerenamer.py -p "ABC\d+" -e "" -l "ABC*.txt"
==>>>>  [ /home/ABC20090101COF.txt ]==>[ /home/COF.txt ]
==>>>>  [ /home/ABC20090101AXY.txt ]==>[ /home/AXY.txt ]
==>>>>  [ /home/ABC20090102BZ.txt ]==>[ /home/BZ.txt ]

remove "-l" to do actual rename.