LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-21-2007, 04:06 AM   #1
zomane
Member
 
Registered: Sep 2005
Location: Austria
Distribution: Debian, CentOS, OpenBSD, FreeBSD
Posts: 52

Rep: Reputation: 16
recursive rename


Hello.Please help, how to rename recursive dirs, sub dirs and files in them.
This is the results of my experiments :
Code:
zarine@zomane:~/1a-script/recrensp$ find ./ -iname "*.bla" -exec rename -v -n 'y/A-Z/a-z/' "{}" \;
./DFg RgF/ffff dDd.bla renamed as ./dfg rgf/ffff ddd.bla
./DFg RgF/dDFd dfdf.bla renamed as ./dfg rgf/ddfd dfdf.bla
./rrr fff/ewrWE pppp.bla renamed as ./rrr fff/ewrwe pppp.bla
./rrr fff/WdgfAAArwe ppRGR.bla renamed as ./rrr fff/wdgfaaarwe pprgr.bla
./rrr fff/Wdg.QQrwe pppp.bla renamed as ./rrr fff/wdg.qqrwe pppp.bla
./rrr fff/sfsd.QQ.r rr.bla renamed as ./rrr fff/sfsd.qq.r rr.bla
./rrr fff/fsfs.HHHH sfsdf.bla renamed as ./rrr fff/fsfs.hhhh sfsdf.bla
./rrr fff/iiuySDy wwww.bla renamed as ./rrr fff/iiuysdy wwww.bla
./aaa fgdfg/eeeEEe qqqq.bla renamed as ./aaa fgdfg/eeeeee qqqq.bla
./aaa fgdfg/GGgg sss.bla renamed as ./aaa fgdfg/gggg sss.bla
./aaa fgdfg/ZXC SSS.bla renamed as ./aaa fgdfg/zxc sss.bla

zarine@zomane:~/1a-script/recrensp$ find ./ -iname "*.bla" -exec rename -v  'y/A-Z/a-z/' "{}" \;
Can't rename ./DFg RgF/ffff dDd.bla ./dfg rgf/ffff ddd.bla: No such file or directory
Can't rename ./DFg RgF/dDFd dfdf.bla ./dfg rgf/ddfd dfdf.bla: No such file or directory
./rrr fff/ewrWE pppp.bla renamed as ./rrr fff/ewrwe pppp.bla
./rrr fff/WdgfAAArwe ppRGR.bla renamed as ./rrr fff/wdgfaaarwe pprgr.bla
./rrr fff/Wdg.QQrwe pppp.bla renamed as ./rrr fff/wdg.qqrwe pppp.bla
./rrr fff/sfsd.QQ.r rr.bla renamed as ./rrr fff/sfsd.qq.r rr.bla
./rrr fff/fsfs.HHHH sfsdf.bla renamed as ./rrr fff/fsfs.hhhh sfsdf.bla
./rrr fff/iiuySDy wwww.bla renamed as ./rrr fff/iiuysdy wwww.bla
./aaa fgdfg/eeeEEe qqqq.bla renamed as ./aaa fgdfg/eeeeee qqqq.bla
./aaa fgdfg/GGgg sss.bla renamed as ./aaa fgdfg/gggg sss.bla
./aaa fgdfg/ZXC SSS.bla renamed as ./aaa fgdfg/zxc sss.bla

zarine@zomane:~/1a-script/recrensp$ find ./ -iname "*.bla" -exec rename -v  'y/a-z/A-Z/' "{}" \;
Can't rename ./DFg RgF/ffff dDd.bla ./DFG RGF/FFFF DDD.BLA: No such file or directory
Can't rename ./DFg RgF/dDFd dfdf.bla ./DFG RGF/DDFD DFDF.BLA: No such file or directory
Can't rename ./rrr fff/fsfs.hhhh sfsdf.bla ./RRR FFF/FSFS.HHHH SFSDF.BLA: No such file or directory
Can't rename ./rrr fff/sfsd.qq.r rr.bla ./RRR FFF/SFSD.QQ.R RR.BLA: No such file or directory
Can't rename ./rrr fff/ewrwe pppp.bla ./RRR FFF/EWRWE PPPP.BLA: No such file or directory
Can't rename ./rrr fff/wdgfaaarwe pprgr.bla ./RRR FFF/WDGFAAARWE PPRGR.BLA: No such file or directory
Can't rename ./rrr fff/wdg.qqrwe pppp.bla ./RRR FFF/WDG.QQRWE PPPP.BLA: No such file or directory
Can't rename ./rrr fff/iiuysdy wwww.bla ./RRR FFF/IIUYSDY WWWW.BLA: No such file or directory
Can't rename ./aaa fgdfg/eeeeee qqqq.bla ./AAA FGDFG/EEEEEE QQQQ.BLA: No such file or directory
Can't rename ./aaa fgdfg/zxc sss.bla ./AAA FGDFG/ZXC SSS.BLA: No such file or directory
Can't rename ./aaa fgdfg/gggg sss.bla ./AAA FGDFG/GGGG SSS.BLA: No such file or directory

zarine@zomane:~/1a-script/recrensp$ find ./ -iname "*.bla" -exec rename -v  's/\ //' "{}" \;
Can't rename ./DFg RgF/ffff dDd.bla ./DFgRgF/ffff dDd.bla: No such file or directory
Can't rename ./DFg RgF/dDFd dfdf.bla ./DFgRgF/dDFd dfdf.bla: No such file or directory
Can't rename ./rrr fff/fsfs.hhhh sfsdf.bla ./rrrfff/fsfs.hhhh sfsdf.bla: No such file or directory
Can't rename ./rrr fff/sfsd.qq.r rr.bla ./rrrfff/sfsd.qq.r rr.bla: No such file or directory
Can't rename ./rrr fff/ewrwe pppp.bla ./rrrfff/ewrwe pppp.bla: No such file or directory
Can't rename ./rrr fff/wdgfaaarwe pprgr.bla ./rrrfff/wdgfaaarwe pprgr.bla: No such file or directory
Can't rename ./rrr fff/wdg.qqrwe pppp.bla ./rrrfff/wdg.qqrwe pppp.bla: No such file or directory
Can't rename ./rrr fff/iiuysdy wwww.bla ./rrrfff/iiuysdy wwww.bla: No such file or directory
Can't rename ./aaa fgdfg/eeeeee qqqq.bla ./aaafgdfg/eeeeee qqqq.bla: No such file or directory
Can't rename ./aaa fgdfg/zxc sss.bla ./aaafgdfg/zxc sss.bla: No such file or directory
Can't rename ./aaa fgdfg/gggg sss.bla ./aaafgdfg/gggg sss.bla: No such file or directory

zarine@zomane:~/1a-script/recrensp$ for a in "`find ./ -iname "*.bla"`"; do b=`echo $a | sed 's/A-Z/a-z/g'`; mv "$a" "$b"; done
mv: cannot stat `./DFg RgF/ffff dDd.bla\n./DFg RgF/dDFd dfdf.bla\n./rrr fff/fsfs.hhhh sfsdf.bla\n./rrr fff/sfsd.qq.r rr.bla\n./rrr fff/ewrwe pppp.bla\n./rrr fff/wdgfaaarwe pprgr.bla\n./rrr fff/wdg.qqrwe pppp.bla\n./rrr fff/iiuysdy wwww.bla\n./aaa fgdfg/eeeeee qqqq.bla\n./aaa fgdfg/zxc sss.bla\n./aaa fgdfg/gggg sss.bla': No such file or directory
In this case "for a in "`find ./ -iname "*.bla"`"; do b=`echo $a | sed 's/A-Z/a-z/g'`; mv "$a" "$b"; done"
new lines( \n) are also quoted and of course the result is "No such file or directory", how to quote everything else without \n .
When I using rename ( perl script ) it renames some files but some not .Strange .

Last edited by zomane; 06-21-2007 at 04:17 AM.
 
Old 06-21-2007, 03:49 PM   #2
cfaj
Member
 
Registered: Dec 2003
Location: Toronto, Canada
Distribution: Mint, Mandriva
Posts: 221

Rep: Reputation: 31
Quote:
Originally Posted by zomane
Hello.Please help, how to rename recursive dirs, sub dirs and files in them.
This is the results of my experiments :
Code:
zarine@zomane:~/1a-script/recrensp$ find ./ -iname "*.bla" -exec rename -v -n 'y/A-Z/a-z/' "{}" \;

Quotes are not necessary around {}.
Quote:
...
In this case "for a in "`find ./ -iname "*.bla"`"; do b=`echo $a | sed 's/A-Z/a-z/g'`; mv "$a" "$b"; done"
new lines( \n) are also quoted and of course the result is "No

That will not work because you have quoted the output of find, thereby passing only one argument to for.

If you remove the quotes, it will fail if any filenames contain whitespace or other pathological characters.

The correct way is to pipe the output of find into a while ... read ... loop.

That sed command will only replace a literal "A-Z" with "a-z".
Use tr to convert to lowercase:

Code:
find ./ -iname "*.bla" |
 while IFS= read -r a
 do
   b=`printf "%s\n" "$a" | tr 'A-Z' 'a-z'`
   mv "$a" "$b"
 done"
If you are using ksh, you can save the calls to an external program with:

Code:
typeset -l b
find ./ -iname "*.bla" |
 while IFS= read -r a
 do
   b=$a
   mv "$a" "$b"
 done"
In bash, you can use a dynamically loadable builtin. See http://www.unixreview.com/documents/...6a/ur0606a.htm for details.

Quote:
such file or directory", how to quote everything else without \n .
When I using rename ( perl script ) it renames some files but some not .Strange .

The Perl script version of rename is unreliable.

 
Old 06-22-2007, 03:28 AM   #3
zomane
Member
 
Registered: Sep 2005
Location: Austria
Distribution: Debian, CentOS, OpenBSD, FreeBSD
Posts: 52

Original Poster
Rep: Reputation: 16
Thanks, now script reading line by line, but mv still says "No such file or directory"
I put your example in script :
Code:
#!/bin/bash
find ./ -iname "*.bla" |
 while IFS= read -r a
 do
   echo $a
   b=`printf "%s\n" "$a" | tr 'A-Z' 'a-z'`
   mv "$a" "$b"
 done
When I run the script the result is :

Code:
./DFg RgF/ffff dDd.bla
mv: cannot move `./DFg RgF/ffff dDd.bla' to `./dfg rgf/ffff ddd.bla': No such file or directory
./DFg RgF/dDFd dfdf.bla
mv: cannot move `./DFg RgF/dDFd dfdf.bla' to `./dfg rgf/ddfd dfdf.bla': No such file or directory
./rrr fff/fsfs.hhhh sfsdf.bla
mv: `./rrr fff/fsfs.hhhh sfsdf.bla' and `./rrr fff/fsfs.hhhh sfsdf.bla' are the same file
./rrr fff/sfsd.qq.r rr.bla
mv: `./rrr fff/sfsd.qq.r rr.bla' and `./rrr fff/sfsd.qq.r rr.bla' are the same file
.
After some time :
Ehhh, of course mv always will says "No such file or directory" in this case, dir "DFg RgF" exists, but "dfg rgf" not, first I must "mkdir" it .

Last edited by zomane; 06-22-2007 at 03:41 AM.
 
Old 07-03-2007, 08:36 AM   #4
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Use -depth when using find. This will go from the deepest to the shallowest. This will prevent you from renaming a directory which still has files yet to be renamed, which is what's happening now. The way you have it, a directory is renamed but then you try to rename a file within it using the old path name.
ta0kira
 
Old 07-03-2007, 11:59 AM   #5
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Code:
find -depth -iname "*.bla" | while read file; do \
  name=`basename $file` \
  newname=`echo $name | sed -r "s/[A-Z]/[a-z]/g"` \
  rename $name $newname $file \
done
ta0kira

Last edited by ta0kira; 07-03-2007 at 12:01 PM.
 
Old 07-03-2007, 12:08 PM   #6
cfaj
Member
 
Registered: Dec 2003
Location: Toronto, Canada
Distribution: Mint, Mandriva
Posts: 221

Rep: Reputation: 31
Quote:
Originally Posted by ta0kira
Code:
find -depth -iname "*.bla" | while read file; do \
  name=`basename $file` \
  newname=`echo $name | sed -r "s/[A-Z]/[a-z]/g"` \
  rename $name $newname $file \
done
ta0kira

I don't think so:

$ echo QWERTY | sed -r "s/[A-Z]/[a-z]/g"
[a-z][a-z][a-z][a-z][a-z][a-z]

 
Old 07-03-2007, 12:55 PM   #7
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Yeah, you're right. I was a little hasty with that one. I don't know what I was thinking.
ta0kira
 
Old 07-03-2007, 03:39 PM   #8
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Yes, tr does a better job like cfaj said. I remembered the way to translate with sed, but you need the whole set spelled out. In case the directories are also being renamed to lowercase, here is my modified version.
Code:
find `line` -depth | while read file; do
  name=`basename $file`
  newname=`echo $name | tr A-Z a-z`
  rename $name $newname $file
done
Paste that into the command line and press [enter], then type the base paths and press [enter] again.
ta0kira

PS cfaj: In your solution it looks like it will turn the entire path name to lowercase, which won't work. You can only rename the basename, to be qualified by the original dirname when renaming.

Last edited by ta0kira; 07-03-2007 at 03:58 PM.
 
Old 06-26-2010, 11:08 AM   #9
petaflot
LQ Newbie
 
Registered: Jun 2010
Posts: 9

Rep: Reputation: 0
Lightbulb FIXED: script for complex directory and file renaming

Fixnames is a GPL bash script that does recursive renaming a directories (top-level first) and files. it includes a --pretend option, full logging, simple detection of duplicate files, progress indicator.

it includes a sed line to make a tree a valid names on a fat/ntfs filesystem while removing multiple, trailing and leading spaces. it is well commented and should be easy to understand.

fixnames was developped for a mac->win migration which involved renaming of over 12k files
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
recursive grep xpucto Solaris / OpenSolaris 2 05-29-2007 09:57 AM
Recursive Find tekmann33 Linux - Newbie 6 05-24-2007 12:28 PM
Recursive Query JMagana Linux - Software 1 12-01-2004 07:46 PM
rm -r what is recursive wogga Linux - Software 3 05-28-2004 02:29 PM
chmod.....recursive help stateq2 Linux - General 3 03-28-2004 07:28 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:55 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration