LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Find directories that have two subdirectories in them-- PLEASE help (http://www.linuxquestions.org/questions/programming-9/find-directories-that-have-two-subdirectories-in-them-please-help-938551/)

pierceogden 04-06-2012 08:37 PM

Find directories that have two subdirectories in them-- PLEASE help
 
I have a directory architecture as follows

Code:

/top/$code1/$code2/grids/
/top/$code1/$code2/calibrate

there are mutliple different $code1 directories, and mutliple $code2 directories inside the code1 dirs. I am trying to find all the instances where the /top/$code1/$code2 directory contains the subdirectories <calibrate> and <grids>. if the $code2 directory has both, I want to run a command. right now i have a bunch of non-working code, which is as follows
Code:

find . \( -name "grids" -o -name "calibrate" \) -type d | sed 's#\(.*\)/.*#\1#' | sort -u
#maybe print only directories with files???
find . -name "calibrate" -prune -name "grids" -type d -print > outf3
#no work
find . grids -type d | find . calibrate -type d | grep -ev 'calibrate' | grep 'grids'

once a have a list of all the directories with those files I want to iterate through them and run commands. I can do the loop, just can't create a list of dir to go through. please help!!!

jschiwal 04-06-2012 08:54 PM

dirs=(/top/*/*/grids /top/*/*/calibrate)
find "${dirs[*]}"

Suppose you know the possible values for $code1 and $code2. You can use range expansion.
find /top/{dira,dirb,dirc}/{dird,dire,dirf}/{grids,calibrate} ...

pierceogden 04-06-2012 09:03 PM

if I have a list of codes1 , could i do that? codes2 is always #.#.pock like '2.0.pock' or '1.5.pock'? maybe be reading a file of codes1? there are 1000 or so codes1 dirs

pierceogden 04-06-2012 09:18 PM

Code:

while read code
do

dirs=(/top/$code/*.*.pock/grids /top/$code/*.*.pock/calibrate)

find "${dirs[*]}"
done < $1

will that recognize numbers where the * are?

pierceogden 04-06-2012 09:22 PM

I am getting a "no such file or direcotry" error with the loop above

Nominal Animal 04-06-2012 09:30 PM

In Bash:
Code:

find /top -type d -print0 | while read -rd '' DIR ; do
    [ -d "$DIR/grids/" ] || continue
    [ -d "$DIR/calibrate/" ] || continue

    # Directory "$DIR/" contains both "$DIR/grids/" and "$DIR/calibrate/".

done

If you only want to match only exactly two levels down, you can either use
Code:

find /top -mindepth 2 -maxdepth 2 -type d -print0 | while read -rd '' DIR ; do
    [ -d "$DIR/grids/" ] || continue
    [ -d "$DIR/calibrate/" ] || continue

    # Directory "$DIR/" contains both "$DIR/grids/" and "$DIR/calibrate/".

done

or an explicit loop,
Code:

for DIR in /top/*/*/ ; do
    [ -d "$DIR/grids/" ] || continue
    [ -d "$DIR/calibrate/" ] ||*continue

    # Directory "$DIR/" contains both "$DIR/grids/" and "$DIR/calibrate/".

done

In Bash, a * will match any name, except for names that begin with a dot. It will, for example, match both foo.bar and @Feghda_zooperdoPE94.453-324_Qwe file or directory names. It will not descend into a subdirectory, though; it will never match a slash (/) or anything containing a slash.

pierceogden 04-06-2012 09:47 PM

Thanks!!!!!!!!!


All times are GMT -5. The time now is 02:53 AM.