macOS AppleScript Bash – How to Find a Nested Folder

applescriptbashmacos

I need to find a folder nested in a folder.

example:

/Volumes/"4T Virtual Machines"/outerfolder/innerfolder

mac $ pwd
/Volumes/4T Virtual Machines
mac $ ls -lR outerfolder/
total 0
-rw-r--r--  1 mac  staff     0B Aug 28 18:02 file1
-rw-r--r--  1 mac  staff     0B Aug 28 18:02 file2
drwxr-xr-x  5 mac  staff   170B Aug 28 18:04 innerfolder/
drwxr-xr-x  3 mac  staff   102B Aug 28 18:07 otherfolder/

outerfolder//innerfolder:
total 0
-rw-r--r--  1 mac  staff     0B Aug 28 18:04 aaa
-rw-r--r--  1 mac  staff     0B Aug 28 18:04 bbb
-rw-r--r--  1 mac  staff     0B Aug 28 18:04 ccc

outerfolder//otherfolder:
total 0
-rw-r--r--  1 mac  staff     0B Aug 28 18:07 justskip
mac $ 

Let's have two cases the outerfolder is at the root of the external drive and the outerfolder isn't at the root of the external drive. I should be able to modify one solution to the other.

I'm writing in applescript on macOS yosemite or later, but potentially invoking a Unix command from applescript. So the solution may be in all applescript or Unix command called from applescript. The Unix command approach

Here is the Unix command I have for finding the outer folder:

find /Volumes -type d -maxdepth 1 ! -name Volumes -exec find {} -type d -maxdepth 1 -name "Virtual Box" \; 

I tired a third nested find, but errored out

 find /Volumes -type d -maxdepth 1 ! -name Volumes -exec find {} -type d -maxdepth 1 -name "Virtual Box" -exec find {}  -type d -maxdepth 1 ! -name inner  \;
find: -exec: no terminating ";" or "+"
find: -exec: no terminating ";" or "+"
find: -exec: no terminating ";" or "+"
mac RC=1 ?  $ 

Best Answer

I'm not quite sure of what you are looking for but the following will list all directories named "Virtual Box" starting with the deepest nested to the least.

find /Volumes -type d ! -name Volumes -name "Virtual Box" 2>/dev/null

EDIT: OK, understanding your requirement a bit better we can use the path predicate to match outerfolder/innerfolder

find /Volumes -type d -path '*outerfolder/innerfolder'