Knowledgebase

How to rename a file in linux..?

Posted by mrzippy, 04-01-2009, 11:58 AM
Arg. I have a list of files in a folder like this: /home/files/www.domain.com/easystats092008.txt And I need to rename them to this format: awstats092008.domain.com.txt I've played around with sed, rename, and a bunch of others.. but I just can't figure this out. Any ideas how to do this? Driving me crazy. Thanks! (BTW - server is CentOS 5.2 Last edited by mrzippy; 04-01-2009 at 12:08 PM.

Posted by stephanhughson, 04-02-2009, 02:01 AM
This script is REALLY bad, but does what you want... for i in $(cat a.txt); do a=`echo $i | cut -d'/' -f4` b=`echo $i | cut -d'/' -f5 | sed 's/easystats/awstats/' | cut -d. -f1` echo $b.$a.txt done Just put the directory listing in a.txt, or change it to "ls /whatever" instead of the cat command. This doesn't get rid of the www. but, but I'm guessing that you may actually need that. If you don't, another sed command would get rid of it I guess. There are many better ways to do this, you could look on the Internet for bash expression matching and do clever stuff in sed. My method is really quite rubbish...

Posted by ServerManagement, 04-02-2009, 08:42 AM
You can make a simple script to do this and customize it any way you want. Even a small perl script to take the output of 'ls' and do a 'foreach' statement.



Was this answer helpful?

Add to Favourites Add to Favourites

Print this Article Print this Article

Also Read
duportonline down !!! (Views: 622)


Language:

Client Login

Email

Password

Remember Me

Search