31

Using /bin/find /root -name '*.csv' returns:

/root/small_devices.csv
/root/locating/located_201606291341.csv
/root/locating/located_201606301411.csv
/root/locating/g_cache.csv
/root/locating/located_201606291747.csv
/root/locating/located_201607031511.csv
/root/locating/located_201606291746.csv
/root/locating/located_201607031510.csv
/root/locating/located_201606301412.csv
/root/locating/located_201606301415.csv
/root/locating/located_201607031512.csv

I don't actually want all the files under /root/locating/, so the expected output is simply /root/small_devices.csv.

Is there an efficient way of using `find' non-recursively?

I'm using CentOS if it matters.

2
  • 5
    Why can't you just do something like echo /root/*.csv ? – Stephen Harris Jul 27 '16 at 13:59
  • 1
    @StephenHarris Primarily because I didn't think of that :) – DeepSpace Jul 27 '16 at 14:00
47

You can do that with -maxdepth option

/bin/find /root -maxdepth 1 -name '*.csv'

From man find

-maxdepth levels

Descend at most levels (a non-negative integer) levels of directories below the starting-points.

-maxdepth 0

means only apply the tests and actions to the starting-points themselves.

1
  • 3
    You may sometimes want to also specify -mindepth 1. For example, find /csv -maxdepth 1 -name '*csv' includes /csv (which has a depth of 0) in its output, whereas find /csv -mindepth 1 -maxdepth 1 -name '*csv' does not. – ma11hew28 Sep 29 '20 at 19:04
10

With standard find:

find /root ! -path /root -prune -type f -name '*.csv'

This will prune (remove) all directories in /root from the search, except for the /root directory itself, and continue with printing the filenames of any regular file that matches *.csv.

With GNU find (and any other find implementation that understands -maxdepth):

find /root -maxdepth 1 -type f -name '*.csv'
3
  • 1
    See also -depth 1 (equivalent to GNU's -mindepth 1 -maxdepth 1) with FreeBSD find. – Stéphane Chazelas Jul 27 '16 at 10:51
  • @StéphaneChazelas I won't mention -depth, it doesn't do what we want on OpenBSD (it's the same as -d, "visit directories in post-order"). That find seems to have -maxdepth though... – Kusalananda Jul 27 '16 at 10:59
  • 1
    Yes, it looks like it's only FreeBSD (and derivatives like Apple OS/X), not NetBSD nor OpenBSD. Shame as -depth -2, -depth +1, -depth 1 that follow the same format as a lot of other predicates makes more sense than -mindepth, -maxdepth IMO. – Stéphane Chazelas Jul 27 '16 at 11:08
1

With FreeBSD find (used by macOS) you can use the -depth primary.

find . -depth 1 -name '*.csv'

-depth n is equivalent to -mindepth n -maxdepth n, as commented by Stéphane Chazelas.

find(1) - FreeBSD explains:

-depth n
        True if the depth of the file relative to the starting point of
        the traversal is n.
-maxdepth n
        Always true; descend at most n directory levels below the command
        line arguments.  If any -maxdepth primary is specified, it ap-
        plies to the entire expression even if it would not normally be
        evaluated.  "-maxdepth 0" limits the whole search to the command
        line arguments.
-mindepth n Always true; do not apply any tests or actions at levels less than n. If any -mindepth primary is specified, it applies to the entire expression even if it would not normally be evaluated. "-mindepth 1" processes all but the command line arguments.

GNU find doesn't support -depth n.

2
  • 1
    Note that it's specific to the FreeBSD implementation of find (also found on macOS I believe). – Stéphane Chazelas Sep 29 '20 at 15:49
  • @StéphaneChazelas, thank you. I just updated my answer. – ma11hew28 Sep 29 '20 at 18:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.