Renaming files | Unix Linux Forums | Shell Programming and Scripting

  Unix/Linux Go Back    


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

Renaming files

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 03-25-2013
Aswex's Unix or Linux Image
Aswex Aswex is offline
Registered User
 
Join Date: Apr 2009
Last Activity: 13 November 2013, 4:20 AM EST
Location: France - EU
Posts: 173
Thanks: 73
Thanked 0 Times in 0 Posts
Renaming files

Hello,

I am looking for a command line that will rename name files :


Code:
f700_abc_o_t_MASTERID_AS_AE_20130323.csv

like this


Code:
f700_abc_o_t_MASTERID_AS_AE_20130324.csv

The great idea could be to get the date stamp
Code:
20130323

and change any part of it, instead of just change the last number, if possible.

Thanks
Sponsored Links
    #2  
Old Unix and Linux 03-25-2013
PikK45 PikK45 is offline
Registered User
 
Join Date: Jul 2012
Last Activity: 26 November 2014, 5:10 AM EST
Location: Chennai
Posts: 567
Thanks: 43
Thanked 74 Times in 72 Posts
basename "f700_abc_o_t_MASTERID_AS_AE_20130324.csv" .csv | awk -F"_" '{print $NF}'

Hope this will help Unix or Linux Image
Sponsored Links
    #3  
Old Unix and Linux 03-25-2013
Aswex's Unix or Linux Image
Aswex Aswex is offline
Registered User
 
Join Date: Apr 2009
Last Activity: 13 November 2013, 4:20 AM EST
Location: France - EU
Posts: 173
Thanks: 73
Thanked 0 Times in 0 Posts
Thanks PiKK45, but this wont work as all files dont have the same name.

The only part that doesn't change is the date stamp in the file name.

And this is the part a need to change in the file name.

Any idea ?

it's about 100 files to change ... Unix or Linux Image
    #4  
Old Unix and Linux 03-25-2013
PikK45 PikK45 is offline
Registered User
 
Join Date: Jul 2012
Last Activity: 26 November 2014, 5:10 AM EST
Location: Chennai
Posts: 567
Thanks: 43
Thanked 74 Times in 72 Posts
Considering all the filenames will have the same YYYYDDMM in their names, you can loop 'em all Unix or Linux Image


Code:
ls | while read file
do
newfile=$(echo $file | sed 's/20130323/20130324/g' )
mv $file $newfile
done

The Following User Says Thank You to PikK45 For This Useful Post:
Aswex (03-26-2013)
Sponsored Links
    #5  
Old Unix and Linux 03-25-2013
anbu23's Unix or Linux Image
anbu23 anbu23 is offline Forum Advisor  
Advisor
 
Join Date: Mar 2006
Last Activity: 16 March 2015, 4:56 AM EDT
Location: Bangalore,India
Posts: 2,136
Thanks: 14
Thanked 150 Times in 143 Posts

Code:
for file_nm in *
do
 ext=${file_nm##*.}
 mv $file_nm ${file_nm%_*}"_"$(date +%Y%m%d)"."${ext}
done

The Following User Says Thank You to anbu23 For This Useful Post:
Aswex (03-26-2013)
Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Unix or Linux Image More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Renaming Files crl21 UNIX for Dummies Questions & Answers 2 04-20-2011 08:12 PM
renaming files or adding a name in the beginning of all files in a folder Lucky Ali Shell Programming and Scripting 4 02-22-2011 05:03 AM
renaming files, please help mqasim Shell Programming and Scripting 2 11-14-2008 08:44 AM
Need help renaming files bbbngowc UNIX for Dummies Questions & Answers 5 04-23-2008 02:08 PM
Renaming files cedrichiu Shell Programming and Scripting 3 04-06-2007 04:35 AM



All times are GMT -4. The time now is 12:15 PM.