4

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.

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

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.

7

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'
  • 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

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.