Bash script rename files based on directory name
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: |-- Varian | |-- FB1-page132.pdf.png | |-- FB1-page133.pdf.png | |-- FB1-page134.pdf.png | |-- FB1-page135.pdf.png Becomes: |-- Varian | |-- Varian-1.png | |-- Varian-2.png | |-- Varian-3.png | |-- Varian-4.png 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 |
try this instead:
Code:
#!/bin/sh |
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:
|
Please, take a look at this: http://mywiki.wooledge.org/ParsingLs
Parsing the output of ls will probably bring you trouble in the long term. |
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?
Quote:
|
Quote:
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:
|
All times are GMT -5. The time now is 03:57 AM. |