Register
It is currently Tue Sep 30, 2014 11:53 am

Mass renaming


All times are UTC - 6 hours


Post new topic Reply to topic  [ 11 posts ] 
Author Message
 PostPosted: Tue Nov 17, 2009 8:07 pm   

Joined: Wed Nov 26, 2008 7:43 pm
Posts: 50
Location: Texas
Hi everyone,

How can I get a directory with files with names:
C1
C2
C3
...

renamed to
Chapter1
Chapter2
Chapter3

Thanks in advance,
Ed


Top
 Profile  
 PostPosted: Wed Nov 18, 2009 1:20 am   

Joined: Sun Nov 15, 2009 12:10 am
Posts: 2
Code:
ls C[1-9]* > file

while  read line ; do
        line=$(echo $line | sed  's/^C/chapter/g' );
        mkdir $line ;
done < file



this script will search for all the files which start with C and a number after it , then it will replace the C with chapter.
does this solve it ?


Top
 Profile  
 PostPosted: Wed Nov 18, 2009 5:05 am   
Moderator
User avatar

Joined: Thu Oct 11, 2007 7:12 am
Posts: 229
Location: London - UK
Code:
for ifile in C[1-9]*
do
  newfile=$(echo $ifile | sed  's/^C/chapter/g')
  mv "$ifile" "$newfile"
done


Top
 Profile  
 PostPosted: Wed Nov 18, 2009 5:17 am   

Joined: Mon Nov 17, 2008 7:25 am
Posts: 221
You can do something like this if you want to avoid the file
Code:
find -xdev -type f -iname C\* -exec sh -c 'mv $1 $(echo ${1/C/Chapter})' {} {} \;


What this does is the following:
find -xdev -type f -iname C\* <--- this searches for everything starting with a capital C.
-exec <-- tells find to execute a command when it finds a file matching the above conditions.
sh -c 'mv $1 $(echo ${1/C/Chapter})' {} {} \; <-- this tells find that it should run that particular command.
Breaking down the "sh" command it does the following:
sh -c <--- tells sh to run the string as a command
mv $1 $(echo ${1/C/Chapter}) <-- move the file $1 to the file $1, but replace every occurance of C to Chapter in $1
{} {} \; <-- this is the magic :) {} is what find stores the found filename in, \; is the ending delimiter for the -exec find paramater.

Hope this clarifies everything in this more or less magical oneliner

Best regards
Fredrik Eriksson


Top
 Profile  
 PostPosted: Wed Nov 18, 2009 2:18 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 549
very nice fredrik, although `echo` is not really useful


Top
 Profile  
 PostPosted: Wed Nov 18, 2009 8:45 pm   

Joined: Wed Nov 26, 2008 7:43 pm
Posts: 50
Location: Texas
Hi everyone,

Thanks for the scripts :). I'm trying to run this in windows with mingw but I get the following error. Anyone knows what's wrong?

C:\Users\Viper\Documents\Downloads\Music>sh rename.sh
rename.sh: line 3: syntax error near unexpected token `$'do\r''
rename.sh: line 3: `do'


Top
 Profile  
 PostPosted: Wed Nov 18, 2009 8:58 pm   

Joined: Wed Nov 26, 2008 7:43 pm
Posts: 50
Location: Texas
nm about the question

I use dos2unix and problem fixed.


Top
 Profile  
 PostPosted: Thu Nov 19, 2009 2:00 am   

Joined: Mon Nov 17, 2008 7:25 am
Posts: 221
Watael wrote:
very nice fredrik, although `echo` is not really useful


Agreed, I wrote it in somewhat of a hurry :) you can skip that part and just do ${1/C/Chapter}

Best regards
Fredrik Eriksson


Top
 Profile  
 PostPosted: Sun Nov 22, 2009 9:54 am   

Joined: Thu Oct 09, 2008 3:26 am
Posts: 15
Location: Columbus, OH
A tad late, but I'm surprised you didn't pipe a cat into sed;

Code:
cat file | sed '/^C[0-9]/s/C/Chapter/'


Easier on the eyes. Yes?


Top
 Profile  
 PostPosted: Mon Nov 23, 2009 5:58 am   
Moderator
User avatar

Joined: Thu Oct 11, 2007 7:12 am
Posts: 229
Location: London - UK
gimp25 wrote:
A tad late, but I'm surprised you didn't pipe a cat into sed;

Code:
cat file | sed '/^C[0-9]/s/C/Chapter/'


Easier on the eyes. Yes?


This cunningly uses the useless use of cat :)

Code:
sed '/^C[0-9]/s/C/Chapter/' file


Top
 Profile  
 PostPosted: Mon Nov 23, 2009 9:44 am   

Joined: Thu Oct 09, 2008 3:26 am
Posts: 15
Location: Columbus, OH
Forgot that one. Thanks for the correction. Even more basic and thus, simpler. But don't forget that doing it that way will only output to stdout (monitor). A redirect to another file name would be necessary.


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 11 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 30 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP