Little batch script to rename directories

Hi folks,

I'd like to rename some special folders on my harddisk. All the folders are looking this way:

[PPP] - ( this text is variable ) - [PPP]

Now I want to exchange all the [PPP] with [TTT] for example, the rest should be left untouched. With my very basics in batch programming I made the following script:

for FILE in "`find / -type d -name "?PPP]*" -print`"
do
        OLDNAME=`echo -n "$FILE"`
        NEWNAME=`echo -n "$FILE" | sed "s/PPP/TTT/g"`
        mv $OLDNAME $NEWNAME
done

Unfortunately, this seems not to work. When I do an "echo $OLDNAME" then I get a string with all the oldnamed directories without \n (newline).

Thanks for every helpful input in this :-)
LVL 1
SmudoAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

hongjunCommented:
try this

for FILE in `find / -type d -name "?PPP]*" -print`
0
SmudoAuthor Commented:
The header must be between quotes "". If it's not, it seems that the spaces in the dir-names are interpreted individually, means that mv gets parameters like

[
PPP
]

-

(
etc.

When I use the quotes, I get the following output, when I add an echo $OLDNAME and echo $NEWNAME on the next line:

./test/[PPP] - ( blu blu ) - [PPP]
./test/[PPP] - ( blo blo ) - [PPP]
./[PPP] - ( blah blah ) - [PPP]
./test/[TTT] - ( blu blu ) - [TTT]
./test/[TTT] - ( blo blo ) - [TTT]
./[TTT] - ( blah blah ) - [TTT]
mv: when moving multiple files, last argument must be a directory
Try `mv --help' for more information.

It's strange that all the [PPP] folders are listed on top. Shouldn't they be in order

./test/[PPP] - ( blu blu ) - [PPP]
./test/[TTT] - ( blu blu ) - [TTT]
etc. ?

0
TintinCommented:
Your problem is with quotes, or more specifically the lack of them.  If you have spaces in a string, you must quote then in order for it to be seen as a single string.  Also, using a for loop means that elements are split on whitespace (by default), so each part of the directory is split up.  You can use a for loop if you change the IFS value, but I think using a while loop is easier:


find / -type d -name "*\[PPP]*" -print` | while read dir
do
   newdir=`echo "$dir" | sed 's/PPP/TTT/g'`
   mv "$dir" "$newdir"
done
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rugdogCommented:
one-liner with perl's help:

find /  -name "\[PPP\]*\[PPP\]"|perl -ne'chomp;$o=$_;s/PPP/TTT/g;print "mv \"$o\" \"$_\"\n";'|sh
0
SmudoAuthor Commented:
Great job! Both worked, Tintin was a bit faster :-)

I just hate to work with spaces in filenames/directories.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux OS Dev

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.