BashScripts.org
http://bashscripts.org/forum/

Handle multiple files from multiple sub-directories....
http://bashscripts.org/forum/viewtopic.php?f=8&t=1475
Page 1 of 1

Author:  jiapei100 [ Tue Oct 18, 2011 5:05 pm ]
Post subject:  Handle multiple files from multiple sub-directories....

Hi, all:

I need a script to handle 4 files from 4 different sub-directories. Well, a bit complex. Let me give you an example:

current directory:

./SF1/AAA/file1.ppm
./SF1/AAA/file2.ppm
...
./SF1/AAA/fileN.ppm

./SF2/AAA/file1.ppm
./SF2/AAA/file2.ppm
...
./SF2/AAA/fileN.ppm

./SF3/AAA/file1.ppm
./SF3/AAA/file2.ppm
...
./SF3/AAA/fileN.ppm

./SF4/AAA/file1.ppm
./SF4/AAA/file2.ppm
...
./SF4/AAA/fileN.ppm



I'd love to carry out some command on 4 files for each run:
./SF1/AAA/file1.ppm
./SF2/AAA/file1.ppm
./SF3/AAA/file1.ppm
./SF4/AAA/file1.ppm

and clearly, there will be N run in total.



Can anybody give me some suggestions?


Best Regards
Pei

Author:  Watael [ Tue Oct 18, 2011 7:22 pm ]
Post subject:  Re: Handle multiple files from multiple sub-directories....

Hi,

use find, something like that
Code:
find ./SF{1..4}/AAA -type f -name file1.ppm
or a for loop
Code:
for f in ./SF{1..4}/AAA/*; do if [ -f "$f" -a "$f" = "file1.ppm" ]; then echo "$f"; fi; done

Author:  choroba [ Wed Oct 19, 2011 2:00 am ]
Post subject:  Re: Handle multiple files from multiple sub-directories....

Code:
for run in $(seq 1 $n) ; do
    handle ./SF[1-4]/AAA/file$run.ppm
done

Author:  jsz [ Sun Oct 23, 2011 4:47 pm ]
Post subject:  Re: Handle multiple files from multiple sub-directories....

I think that what you're searching for is globbing.

Code:
for f in ./SF*/AAA/file1.ppm; do
    # do something here with the variable "$f"
    # example: echo "$f"
done


The asterisk in « SF* » means that there is something after « SF », so if you have a directory called SFabcd it'll catch it too.

If you want to specify that what follows « SF » is a digit:
Code:
for f in ./SF[[:digit:]]/AAA/file1.ppm; do
...


If you want to specify instead that there is only one character (which can be a digit) after it:
Code:
for f in ./SF?/AAA/file1.ppm; do
...


If you want to be precise and say that what follows it is a number, you'd use extglob:

Code:
shopt -s extglob

for f in ./SF+([[:digit:]])/AAA/file1.ppm; do
...

Page 1 of 1 All times are UTC - 6 hours
© 2000, 2002, 2005, 2007 phpBB Group • http://www.phpbb.com