The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Renaming multiple files jayell Shell Programming and Scripting 7 01-12-2009 04:27 PM
Renaming multiple files Jazmania Shell Programming and Scripting 6 01-02-2009 02:16 PM
Renaming Multiple files dhiren_shah UNIX for Dummies Questions & Answers 2 09-12-2008 01:23 AM
Renaming multiple files rmayur UNIX for Dummies Questions & Answers 6 02-26-2004 04:40 AM
Renaming multiple files jxh461 Shell Programming and Scripting 4 04-01-2003 06:25 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 01-02-2009
shekhar_v4 shekhar_v4 is offline
Registered User
  
 

Join Date: Jan 2009
Posts: 17
Renaming Files (Multiple files)

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
  #2 (permalink)  
Old 01-02-2009
in2nix4life's Avatar
in2nix4life in2nix4life is offline
Registered User
  
 

Join Date: Oct 2007
Location: East Coast
Posts: 58
If you have Perl installed, this may help you:

for x in *.txt;do mv $x `echo $x | perl -pe 's/^\w+\d+(.*)/$1/'`;done

Hope this helps.
  #3 (permalink)  
Old 01-02-2009
Christoph Spohr Christoph Spohr is offline
Registered User
  
 

Join Date: Sep 2008
Posts: 205
Hi,

shell variables inside '' are not expanded. So here

Code:
mv $i `echo $i | sed 's/$myfile//'`
$myfile will not be substituted by "date '+ABC%Y%m%d'".

Either use "..." or no sed at all. Shell built-ins are enough. Try:
Code:
mv $i ${i/200[7-9][0-1][0-9][0-3][0-9]/}
HTH Chris
  #4 (permalink)  
Old 01-03-2009
reddysiva reddysiva is offline
Registered User
  
 

Join Date: Dec 2008
Posts: 11
Try this
myfile=$(date '+ABC%Y%m%d')
for i in *.txt
do
mv $i `echo $i | sed "s/$myfile//"`
done

i thik u didnt use back tics or $() to set variable myfile.
to expend $myfile use " instead of '

i tried this
a=$(date '+ABC%Y%m%d')

echo ABC20090103AXY.txt | sed 's/$a//g'
ABC20090103AXY.txt

echo ABC20090103AXY.txt | sed "s/$a//g"
AXY.txt

echo '$aABC20090103AXY.txt' | sed "s/$a//g"
$aAXY.txt

echo '$aABC20090103AXY.txt' | sed 's/$a//g'
ABC20090103AXY.txt
  #5 (permalink)  
Old 01-05-2009
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,422
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.
  #6 (permalink)  
Old 01-05-2009
freizl freizl is offline
Registered User
  
 

Join Date: Nov 2008
Posts: 3
Assuming the "ABC20090102" string is fixed length.
And I'm using sh shell.

Code:
ls -1 *.txt | awk '{print "mv", $1, substr($1,12) }' | sh

Last edited by freizl; 01-05-2009 at 04:55 AM..
  #7 (permalink)  
Old 01-05-2009
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,422
Quote:
Originally Posted by freizl View Post
Assuming the "ABC20090102" string is fixed length.
And I'm using sh shell.

Code:
ls -1 *.txt | awk '{print "mv", $1, substr($1,12) }' | sh
it is also not necessary to use ls and sh , by making use of FILENAME variable
Code:
awk '{ system("mv " FILENAME" " substr(FILENAME,1,12) )}' ABC*.txt
not tested.
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 08:02 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0