Skip navigation

Bash 'find' bug

1143 Views 20 Replies Latest reply: Apr 26, 2012 5:39 AM by BobHarris RSS
1 2 Previous Next
Neville Hillyer Level 4 Level 4 (1,845 points)
Currently Being Moderated
Apr 24, 2012 3:11 PM

-


My Bash manual for 'Find' says:

 

'-depth  Always true; same as the -d option.'

 

My experience is that -depth works but -d does not.

 

Is this a known issue?

 

If not can others confirm this behaviour?

 

  -

Mac OS X (10.5.8), Two 733 MHz G4s with Leopard !
  • MrHoffman Level 6 Level 6 (11,720 points)
    Currently Being Moderated
    Apr 24, 2012 5:19 PM (in response to Neville Hillyer)

    Could be a bug, or it could be a command syntax error.  Please post the particular find command you are testing; the -d syntax is a slightly weird one, as it can be both an option, and a primary.  (With that command, somebody here can also test it against something newer than 10.5, too.)

     

    My preference on OS X is mdfind over find.  The mdfind command syntax is (yet) more cryptic (than find), but it's gonzo-fast; it's the command-line interface into Spotlight.

     

    And in general, bash command syntax does vary somewhat by platform, and by the version of the particular tool.   Use the man page to determine the particular syntax for the particular platform.  And there are the occasional bugs.

  • BobHarris Level 6 Level 6 (12,505 points)
    Currently Being Moderated
    Apr 24, 2012 5:31 PM (in response to Neville Hillyer)

    I got identical results using

     

    /usr/bin/find -d path >tmp.1
    /usr/bin/find path -d >tmp.2
    /usr/bin/find path -depth >tmp.3
    

     

    And I got different results from the above using

     

    /usr/bin/find path >tmp.4
    
    cksum tmp.[1234]
    2923241020 29127374 tmp.1     # match
    2923241020 29127374 tmp.2     # match
    2923241020 29127374 tmp.3     # match
    618095947 29127374 tmp.4      # different
    

     

    So I cannot reproduce any errors.

     

    The above was a 10.7.3 system

     

    Using a 10.5.8 system

     

    cksum tmp.[1234]
    4233427378 28718 tmp.1     # match
    # the find path -d option was not valid; must be something newer than 10.5.8
    4233427378 28718 tmp.3     # match
    2987672313 28718 tmp.4     # different
    

     

    Message was edited by: BobHarris

    MacBook Pro, Mac OS X (10.7.3), 27" iMac, MacBook, MacMini, etc...
  • etresoft Level 7 Level 7 (23,905 points)
    Currently Being Moderated
    Apr 24, 2012 6:39 PM (in response to Neville Hillyer)

    find is not a bash builtin. It is a separate program.

     

    Also, which "-depth" are you looking at? There are two. "-depth n" limits the find to n directories deep. Both "-d" and "-depth" perform a depth-first traversal. The "-depth n" is a much more commonly used option. For what it's worth, "-d" and "-depth" do seem equivalent on my system.

  • BobHarris Level 6 Level 6 (12,505 points)
    Currently Being Moderated
    Apr 25, 2012 5:33 AM (in response to Neville Hillyer)

    The version of bash has nothing to do with the 'find' command.  They are 2 separate programs.

     

    "find /bin -d ..." syntax is not supported in the Mac OS X 10.5.8 'find' command.

     

    That syntax "IS" supported in 10.7.3

     

    In 10.5.8, you would need to use

     

    find -d /bin -name bash
    

    or

    find /bin -depth -name bash
    

     

    If staying on Mac OS X 10.5.8 is important, but you also need the "find /bin -d ..." syntax, you could most likely locate the open source 'find' copy and build a newer version for your system.  There are also packages such as MacPorts.org and Fink.sf.net which have Mac OS X ported open source packages available.

     

    Personally, I would just avoid the "find /bin -d ..." syntax, as there are valid work arounds.

    MacBook Pro, Mac OS X (10.7.3), 27" iMac, MacBook, MacMini, etc...
  • BobHarris Level 6 Level 6 (12,505 points)
    Currently Being Moderated
    Apr 25, 2012 8:12 AM (in response to Neville Hillyer)

    Is this common?

    'find' is in a class all by itself.  But it is so useful and powerful, nothing has ever replaced it :-)

     

    If you need to look down a directory tree, then 'find' is the best first pass tool at finding files.

     

    Instead of -depth 0, you could use -prune

     

    if find /bin/bash -prune >/dev/null 2>&1; then
         echo /bin/bash exists
    fi
    

     

    The above form could also include other 'find' selection operators.

     

    If you know the directory, then you can also use

     

    if [[ -d /bin/bash ]]; then
        echo /bin/bash exists
    fi
    

     

    You can use ls -l to capture the time of a file and with different ls options you can choose the modification (default with -l), or the last accessed time (atime), or the last metadata mod time (aka ctime).  ls -l can also give you size.

     

    You can also use the 'stat' command to extract various file metadata information, such as time and size information.

    MacBook Pro, Mac OS X (10.7.3), 27" iMac, MacBook, MacMini, etc...
  • twtwtw Level 5 Level 5 (4,580 points)
    Currently Being Moderated
    Apr 25, 2012 8:35 AM (in response to Neville Hillyer)

    It's not entirely clear what your goal is (you havn't been specific), but if you're trying to find folders on the desktop that conform to particular naming conventions, here's a couple of alternate options to find that you can consider:

     

    use mdfind. (the following finds folders recursively on the desktop whose name begins with 'a'):

     

    mdfind "kMDItemContentType=='public.folder' && kMDItemFSName=='a*'" -onlyin ~/Desktop

     

    use applescript. (the following finds folders non-recursively on the desktop whose name begins with 'a'):

     

    tell application "System Events"

              set theFolders to every folder of desktop folder whose name begins with "a"

    end tell

  • twtwtw Level 5 Level 5 (4,580 points)
    Currently Being Moderated
    Apr 25, 2012 10:33 AM (in response to Neville Hillyer)

    mdfind has keys for size and date (kMDItemFSSize and kMDItemFSCreationDate or kMDItemFSContentChangeDate - I don't know if they are the 'necessary' ones since you haven't told us what you're doing).  You can't specify a particular depth, so far as I know; mdfind always searches the entire hierarchy starting at the folder you specify.  you'd have to filter the results if you only wanted it to go to a specific depth.  see the query programming syntax and metadata attributes references.

     

    As far as applescript goes - frankly - sometimes it's just easier to do stuff in applescript than in the shell.

  • BobHarris Level 6 Level 6 (12,505 points)
    Currently Being Moderated
    Apr 25, 2012 11:00 AM (in response to Neville Hillyer)
    find ~/Desktop/untitled\ folder* -prune  -Bmin 1 -empty -exec rmdir {} \;
    

     

    This works for me.

    MacBook Pro, Mac OS X (10.7.3), 27" iMac, MacBook, MacMini, etc...
1 2 Previous Next

Actions

More Like This

  • Retrieving data ...

Bookmarked By (0)

Legend

  • This solved my question - 10 points
  • This helped me - 5 points
This site contains user submitted content, comments and opinions and is for informational purposes only. Apple disclaims any and all liability for the acts, omissions and conduct of any third parties in connection with or related to your use of the site. All postings and use of the content on this site are subject to the Apple Support Communities Terms of Use.