Computing.Net > Forums > Unix > Renaming the file

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Renaming the file

Reply to Message Icon

Name: gobi (by gp12)
Date: May 10, 2007 at 23:18:37 Pacific
OS: UNIX
CPU/Ram: -/512
Comment:

hi,
i am having set of files with the naming convention grap_Life_$cc$yy$mm$dd.txt.
The above file was created by a script on this month..
i would like to get the above files to get renamed to today's date.
i am encountering problem when there is two or more files with same format..
your help will be greatly appreciated.
thanks for everyone

gobi



Sponsored Link
Ads by Google

Response Number 1
Name: ghostdog
Date: May 13, 2007 at 05:24:17 Pacific
Reply:

what is $cc? and what do you mean by 2 or more files with same format? an example?


0

Response Number 2
Name: wollie
Date: May 14, 2007 at 22:56:32 Pacific
Reply:

Hi ghostdog,

I tend to think that $cc represents the "century" - and the format: well, I guess that gobi refers to files with identical filenames when talking about "format".
Well, several files with identical filename in the same directory ? no chance .

cheers


0

Response Number 3
Name: gobi (by gp12)
Date: May 15, 2007 at 06:01:22 Pacific
Reply:

hi,
i mean identical files in the same format but with different dates alone:
eg:
rush_Life_20070506.txt
rush_Life_20070508.txt
rush_Life_20070412.txt

I need to take only files for current month and rename them one by one to current date.
hope u got my view.

thanks

gobi


0

Response Number 4
Name: ghostdog
Date: May 15, 2007 at 20:58:47 Pacific
Reply:

if you don't mind, you can try this
[code]
month=`date '+%m'`
current=`date '+%Y%m%d'`
awk -v mth=$month -v cur=$current ' BEGIN{ FS="[.]"}
{
n=split($1,arr,"_")
if ( substr(arr[3],5,2) == mth ){
arr[3] = cur
result = arr[1]
for (i = 2; i <= n; i++)
result = result "_" arr[i]
result=result"."$NF
cmd = "mv " "\047" $0 "\047" " " "\047" result "\047"
print cmd
## system(cmd) ##uncomment to execute

}
}' "file"
[/code]


0

Response Number 5
Name: gobi (by gp12)
Date: May 17, 2007 at 19:56:53 Pacific
Reply:

hi,
Thank u very much for ur help.
it workssssss fineeeeee


gobi


0

Related Posts

See More



Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Renaming the file

Rename the files date stamp only. www.computing.net/answers/unix/rename-the-files-date-stamp-only/7489.html

How to rename a file based 1st line www.computing.net/answers/unix/how-to-rename-a-file-based-1st-line/8125.html

Renaming file using Perl www.computing.net/answers/unix/renaming-file-using-perl/7484.html