[mdlug-newbie] question about scripting

Ingles, Raymond Raymond.Ingles at compuware.com
Fri Jan 6 08:31:52 EST 2012


There’s a page that covers the (many) ways to do it here:

 

http://stackoverflow.com/questions/4793892/recursively-rename-files-using-find-and-sed

 

I like keeping things conceptually simple, and easy-to-follow. One-liners are fun, but generally too clever to understand six months later. The way *I’d* handle it would be:

 

---------------

#!/bin/bash

 

IFS=`printf '\n\t'`

 

for FILE in $(find . -name "*.mp3")

do 

  echo mv $FILE `echo $FILE | tr ' ' '_' | tr '[A-Z]' '[a-z]'`

done

----------------

 

There’s a couple tricks there. The main one is the “IFS” bit; that sets the “Internal Field Separator” to just tab and newline, so that spaces in filenames are preserved. I put that at the head of pretty much all my scripts these days, as files with spaces in the name are very common.

 

As for the rest, it uses ‘find’ to do the recursion (you could change “*.mp3” to “*” to process all files) and then calls mv for each one. (Note the ‘echo’ before ‘mv’; right now I’m just debugging, so it prints out what it *would* do. Remove that first ‘echo’ to *actually* run the command.)

 

Sincerely,

 

Ray Ingles                                         (313) 227-2317

 

"The America I feel allegiance to isn't the America that requires

          compulsory displays of loyalty." - Ron Rosenbaum

 

From: mdlug-newbie-bounces at mdlug.org [mailto:mdlug-newbie-bounces at mdlug.org] On Behalf Of Mathew May
Sent: Thursday, January 05, 2012 4:37 PM
To: A place where there are no dumb (linux related) questions.
Subject: Re: [mdlug-newbie] question about scripting

 

Sorry,

The script I attached was a variant of the original one.  This is the original script:

#!/bin/bash

ls | while read -r FILE
do
  mv -v $FILE `echo $FILE | tr ' ' '_' | tr '[A-Z]' '[a-z]'`
done


Thanks,
Mat




The contents of this e-mail are intended for the named addressee only. It contains information that may be confidential. Unless you are the named addressee or an authorized designee, you may not copy or use it, or disclose it to anyone else. If you received it in error please notify us immediately and then destroy it.


From: "Mathew May" <mathewmay1 at wowway.com>
To: mdlug-newbie at mdlug.org
Sent: Thursday, January 5, 2012 4:23:40 PM
Subject: [mdlug-newbie] question about scripting

I am trying to write a script that will go through my current mp3 directory and change any space in a file name to an under score as well as change all upper case characters to lower case.  I am new to scripting and I have done pretty good so far, but now I am trying to make my script recursive.

Here is what I have so far:

#!/bin/bash

#ls -R | while read -r FILE
for FILE in `find /home/mmay3/Music/tori_amos *.mp3 -type f`
do
  mv -v $FILE `echo $FILE | tr ' ' '_' | tr '[A-Z]' '[a-z]'`
done

Any help would be greatly appreciated
Mat


_______________________________________________
mdlug-newbie mailing list
mdlug-newbie at mdlug.org
http://mdlug.org/mailman/listinfo/mdlug-newbie


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mdlug.org/pipermail/mdlug-newbie/attachments/20120106/47a4eecf/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: CPWRsig_04_11-03-2010.jpg
Type: image/jpeg
Size: 9440 bytes
Desc: CPWRsig_04_11-03-2010.jpg
URL: <http://mdlug.org/pipermail/mdlug-newbie/attachments/20120106/47a4eecf/attachment-0001.jpg>


More information about the mdlug-newbie mailing list