Register
It is currently Fri Apr 18, 2014 5:16 pm

Handle multiple files from multiple sub-directories....


All times are UTC - 6 hours


Post new topic Reply to topic  [ 4 posts ] 
Author Message
 PostPosted: Tue Oct 18, 2011 5:05 pm   

Joined: Mon Jul 04, 2011 9:29 pm
Posts: 3
Location: Richmond, Canada
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


Top
 Profile WWW  
 PostPosted: Tue Oct 18, 2011 7:22 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 512
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


Top
 Profile  
 PostPosted: Wed Oct 19, 2011 2:00 am   
User avatar

Joined: Tue Apr 27, 2010 2:28 pm
Posts: 172
Location: Czech Republic
Code:
for run in $(seq 1 $n) ; do
    handle ./SF[1-4]/AAA/file$run.ppm
done


Top
 Profile  
 PostPosted: Sun Oct 23, 2011 4:47 pm   
User avatar

Joined: Wed Jun 08, 2011 8:27 am
Posts: 189
Location: outer Shpongolia
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
...


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 6 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP