Search code examples
bashcommandfind

search with find command until first match


I just need to search for a specific directory that can be anywhere is there a way to run this command until the first match? Thanx! Im now ussing

find / -noleaf -name 'experiment' -type d | wc -l

Solution

  • As Rudolf Mühlbauer mentions, the -quit option tells find to quit. The man page example is that
    find /tmp/foo /tmp/bar -print -quit
    will print only /tmp/foo.

    Slightly more generally, find may be the wrong tool for what you want to do. See man locate. For example, on my system,
    locate experiment | head -3
    produces

    /usr/lib/tc/experimental.dist
    /usr/share/doc/xorg/reference/experimental.html
    /usr/share/doc/xorg/reference/experimental.txt
    

    while locate -r 'experimental..$' produces (with 6 lines snipped for brevity)

    /usr/src/linux-headers-3.2.0-24-generic/include/config/experimental.h
     (snip)
    /usr/src/linux-headers-3.2.0-32-generic/include/config/experimental.h
    

    As noted in ShpielMeister's comment, the locate database is updated only periodically (eg, daily on Ubuntu Linux systems). When new files are a concern, say sudo updatedb, which in a second or two of work will update the database. Also see: ioflood.com blog about locate command