bashunix

How to only find files in a given directory, and ignore subdirectories using bash


I'm running the find command to find certain files, but some files in sub-directories have the same name which I want to ignore.

I'm interested in files/patterns like this:

/dev/abc-scanner, /dev/abc-cash ....

The command:

find /dev/ -name 'abc-*'

What's being returned:

/dev/abc-scanner
/dev/abc-cash
...
...
...
/dev/.udev/names/abc-scanner
/dev/.udev/names/abc-cash

I want to ignore the latter files: /dev/.udev/...


Solution

  • If you just want to limit the find to the first level you can do:

     find /dev -maxdepth 1 -name 'abc-*'
    

    ... or if you particularly want to exclude the .udev directory, you can do:

     find /dev -name '.udev' -prune -o -name 'abc-*' -print
    

    Note: that /dev is the directory from the question; you will want to replace that with the directory you wish to search.

    Note2: -maxdepth is a global option and hence must be specified towards beginning