Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I'm pretty new to bash scripting, but I really want to wrap my head around it.
What I'm trying to do is: From directory "A": Go in to all subdirectories and rename all files within icrementally according to the directory name. SO:
I'm trying this script which I hacked together, but it is tripping over spaces in filename. Is there any way to retain the spaces? Or should I just replace them with underscores?
Code:
#!/bin/sh
for i in $(ls -d */ | awk '{print $0}'); do
cd $i
for j in ls;
do
sName =$(ls $i | awk '{print $i NR".png"}')
rename $j $sName
done
cd ../
done
#!/bin/sh
for i in $(ls -d */ | awk '{print $0}'); do
cd $i
ifs=$IFS
IFS='\
'
for j in $(ls); do
sName =$(ls $i | awk '{print $i NR".png"}')
rename $j $sName
done
IFS=$ifs
cd ../
done
If you want more information you can read up on ifs (internal field separator) over here http://mindspill.net/computing/linux...haracters.html (note here i'm saving and reverting the IFS variable back in case it had something 'nonstandard' in it... if there's nothing special you can just do IFS='\[enter]' and unset IFS)
Great, hopefully that is exactly what I needed! I've already found the Field Separator in awk extremely helpful in my projects. It's the little things (like this) that make Linux so amazing... but I'll tell you a secret: I'm actually using OSX at the moment! Thanks for the reference as well: hopefully next time I won't have to ask.
Quote:
Originally Posted by rweaver
try this instead:
Code:
#!/bin/sh
for i in $(ls -d */ | awk '{print $0}'); do
cd $i
ifs=$IFS
IFS='\
'
for j in $(ls); do
sName =$(ls $i | awk '{print $i NR".png"}')
rename $j $sName
done
IFS=$ifs
cd ../
done
If you want more information you can read up on ifs (internal field separator) over here http://mindspill.net/computing/linux...haracters.html (note here i'm saving and reverting the IFS variable back in case it had something 'nonstandard' in it... if there's nothing special you can just do IFS='\[enter]' and unset IFS)
Hey, that's very interesting. I'd never thought about the pitfalls of ls, I'd just seen it done so many times: especially where awk is concerned. The pitfalls article is great as well, any other recommendations for a fledgling bash-er?
Parsing the output of ls will probably bring you trouble in the long term.
Actually using the ifs will solve most if not all the problems you typically run into with parsing ls... to be fair though, even the ifs is a bit of a kludge work-around (although useful in many areas.)
Find is definitely a better solution and using read isn't a bad idea either.
As is also replacing spaces in filenames with underscores. The default in *nix is that params are space separated, so just about all the tools/cmds assume that. It'll definitely simplify your life to get rid of them. You'll notice that all the files (I've ever seen) issued as part of Unix do not have spaces in them...
Understood, I personally never use spaces. I'm working with someone else's data, on OSX. I'm generating XHTML from the filenames, too, so I'd have to change back to spaces later, anyway.
Quote:
Originally Posted by chrism01
As is also replacing spaces in filenames with underscores. The default in *nix is that params are space separated, so just about all the tools/cmds assume that. It'll definitely simplify your life to get rid of them. You'll notice that all the files (I've ever seen) issued as part of Unix do not have spaces in them...
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.