I need to find a folder nested in a folder.


/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/

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

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 😱  $ 
  • So what you basically want to do is to find a folder innerfolder which is directly within outerfolder, right? – nohillside Aug 28 '19 at 21:47
  • yes. But not in the startup drive. – historystamp Aug 28 '19 at 21:58
  • What makes you think it matters on which drive it is? The filesystem in Unix systems has a common root for all volumes/drives – nohillside Aug 28 '19 at 22:14
  • because the specification says the file directories will be on an external drive. I guess it doesn't make any difference in reality. – historystamp Aug 28 '19 at 22:28

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'
  • 2>/dev/null I assume your trying to get rid of the errors, but it's getting rid of everything. – historystamp Aug 28 '19 at 22:40
  • I think -name /Volumes and -name Volumes are the same. What is the diff if not? – historystamp Aug 28 '19 at 22:43
  • @historystamp Well, 2>/dev/null is sending standard error to the bit bucket and nothing else. Actually, -name Volumes will exclude /Volumes from the output, my mistake, will edit – fd0 Aug 28 '19 at 23:33
  • Works now. I had a function named "find" which was confusing things. – historystamp Aug 28 '19 at 23:38
  • Thanks for the -path example. I tried using -path on my own but I got errors. Your solution works! – historystamp Aug 29 '19 at 18:16

Assuming innerfolder needs to be directly within outerfolder:

find /Volumes -type d -name "outerfolder" -exec test -d {}/"innerfolder" \; \
                                          -exec echo {}/"innerfolder" \;

(formatted for readabiliy, you can also put everything on one line and remove the trailing \ from the first line)


Running fd0's solution:

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

You get the annoying "Permission denied" messages. You could send all error messages to /dev/null, but that's too drastic to me.

find: /Volumes/4T Virtual Machines/.DocumentRevisions-V100: Permission denied
find: /Volumes/4T Virtual Machines/.TemporaryItems: Permission denied
find: /Volumes/4T Virtual Machines/.Trashes: Permission denied
/Volumes/4T Virtual Machines/outerfolder/innerfolder
/Volumes/4T Virtual Machines/outerfolder/otherfolder/outerfolder/innerfolder
mac RC=1 😱  $ 

I combined fd0 answer with a previous poster's way of getting rid of permission's errors.

 { LC_ALL=C find /Volumes -type d -path '*outerfolder/innerfolder'  3>&2 2>&1 1>&3 | grep --color=auto -v -e 'Permission denied' -e 'Operation not permitted' 1>&3; [ $? = 1 ];  } 3>&2 2>&1

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

The downside of using the find command is that it takes a long while should you include the timemachine backup disk.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .