Find folder within folder, then find other folder in same dir

Hi all I'm new to your forum but not new to shells. I'm having a little trouble though as it's been quite some time since I scripted. Here's what I'm trying to do:

I'm trying to search a directory named '/var/root/Applications' for another directory 'fooBar'. The "Applications" directory contains several directories within with numbers instead of names. The directory 'fooBar' is inside of one of these directories. So for example: /var/root/Applications/2348970343/fooBar. I need to find 'fooBar' and then use that directory that contains it /var/root/Applications/2348970343/, and find a directory named "Documents" in that same directory. ALL of the numbered directories contain a folder named "Documents" so searching for that won't help. I need to find the 'Documents' folder within the same folder that contains 'fooBar'

find /var/root/Applications/ -type d "fooBar* -maxdepth 1 2>/dev/null

That code effectively returns the folder containing 'fooBar' for me. But from there I can't wrap my mind around getting the folder 'Documents' where 'fooBar' is contained. Thank you for your help in advance. :b:

find /var/root/Applications/ -type d "fooBar* -maxdepth 1 2>/dev/null | while read dir
do
   [ -d "$dir/Documents" ] && echo $dir/Documents
done

or

for dir in /var/root/Applications/*/fooBar
do
 [ -d "$dir/Documents" ] && echo $dir/Documents
done
1 Like

Ok awesome, now that I can find that 'Documents' folder, I need to search it for a filetype (.rar) and copy those to a folder. When I know where the 'Documents' folder is I normally do this:

find /var/root/Applications/known_folder/Documents/ -type f   "*.rar" | xargs cp -n - p -t /to/my/directory/ 

I dunno how to combine your search with my normal search. Thanks again.

edit: how bout this

for dir in /var/root/Applications/*/fooBar
do
 [ -d "$dir/Documents" ] && find "$dir"/Documents -type f *.rar | xargs blah blah blah?
done

Close :

for dir in /var/root/Applications/*/fooBar
do
 [ -d "$dir/Documents" ] && find "$dir"/Documents -type f *.rar 
done | xargs cp -n -p -t /to/my/directory/

Ok so the 'Documents' folder is in the same folder as fooBar but its not a subdirectory of fooBar. When I type what you've offered in terminal, it returns nothing. I look at it and it appears your asking me to have it test for 'fooBar/Documents' when in fact its /this/way/to/fooBar which is also /this/way/to/Documents. Documents and fooBar are in the "to" folder using this example.

ls -a /this/way/to

would produce about 50 obfuscated dirnames, inside of one of these dirs theres a subdir called Documents and a file named fooBar.

OK how about this
(requires bash or ksh shell)

for dir in /var/root/Applications/*/fooBar
do
    dir=${dir%/fooBar}
    [ -d "$dir/Documents" ] && find "$dir"/Documents -type f *.rar 
done | xargs cp -n -p -t /to/my/directory/

Eek, sadly I'm trying to do this on my iPhone which does not have bash nor korn, just plain ol' sh.
Also I'm getting paths must precede expression error...

find /var/root/Applications/*/fooBar/Documents  -type f -name "*.rar" -exec cp -n -p -t {}  /to/my/directory/ \;

No bash or ksh, do you have dirname:

for dir in /var/root/Applications/*/fooBar
do
    dir=`dirname $dir`
    [ -d "$dir/Documents" ] && find "$dir"/Documents -type f -name "*.rar"
done | xargs cp -n -p -t /to/my/directory/

@rdcwayx see post #5, also -t option of cp specifies the target directory

Documents is not a directory of fooBar. It's located in the same directory as fooBar. The reason I search for fooBar and not directory is because it is unique as all applications in the /var/root/Applications folder contain a Documents folder. I need to find the folder that has fooBar in it, then go to the folder named Documents which is also in it. Then I need to copy files from it once I've found it

---------- Post updated at 11:10 PM ---------- Previous update was at 08:14 PM ----------

I've used dirname before so yes that is present. I will test this later Chubler thank you as always man.