LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-06-2012, 07:37 PM   #1
pierceogden
LQ Newbie
 
Registered: Apr 2012
Posts: 10

Rep: Reputation: Disabled
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!!!
 
Old 04-06-2012, 07:54 PM   #2
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
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} ...
 
Old 04-06-2012, 08:03 PM   #3
pierceogden
LQ Newbie
 
Registered: Apr 2012
Posts: 10

Original Poster
Rep: Reputation: Disabled
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

Last edited by pierceogden; 04-06-2012 at 08:04 PM.
 
Old 04-06-2012, 08:18 PM   #4
pierceogden
LQ Newbie
 
Registered: Apr 2012
Posts: 10

Original Poster
Rep: Reputation: Disabled
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?
 
Old 04-06-2012, 08:22 PM   #5
pierceogden
LQ Newbie
 
Registered: Apr 2012
Posts: 10

Original Poster
Rep: Reputation: Disabled
I am getting a "no such file or direcotry" error with the loop above
 
Old 04-06-2012, 08:30 PM   #6
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
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.
 
Old 04-06-2012, 08:47 PM   #7
pierceogden
LQ Newbie
 
Registered: Apr 2012
Posts: 10

Original Poster
Rep: Reputation: Disabled
Thanks!!!!!!!!!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How can I find all directories with subdirectories with find? Ujjain Linux - Software 1 06-11-2011 05:57 PM
Sort Many Files in Subdirectories into Directories by File Type garyozzy Programming 5 02-15-2011 03:18 PM
[SOLVED] command to find a specific word in directories and subdirectories? siranjeevi Linux - Newbie 4 06-09-2010 09:19 AM
copy directories and subdirectories Red Squirrel Linux - Newbie 2 03-06-2004 09:18 PM
How can i delete Directories and Subdirectories? OrganicX Linux - Newbie 15 03-05-2003 08:48 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:55 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration