LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Bash script rename files based on directory name (https://www.linuxquestions.org/questions/linux-general-1/bash-script-rename-files-based-on-directory-name-798122/)

cupofnestor 03-26-2010 03:38 PM

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
 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


rweaver 03-26-2010 04:02 PM

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)

cupofnestor 03-29-2010 08:27 AM

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 (Post 3913722)
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)


i92guboj 03-29-2010 08:29 AM

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.

cupofnestor 03-30-2010 08:10 AM

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:

Originally Posted by i92guboj (Post 3916698)
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.


rweaver 03-30-2010 12:18 PM

Quote:

Originally Posted by i92guboj (Post 3916698)
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.

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.

chrism01 03-30-2010 09:08 PM

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... ;)

cupofnestor 03-31-2010 08:20 AM

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 (Post 3918610)
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... ;)



All times are GMT -5. The time now is 03:57 AM.