correct usage of find's -prune option

I know one of the more seasoned veterans probably opened this thread looking for their chance to refer me to the site's search feature and let me tell you.

I'VE LOOKED!!!! And I didn't find anything helpful...

So, I've got a windows background and I'm fond of its search feature which comes with the option to specify a single directory in its search path. Or I like being able to sort a directory in Windows and view the directories first, without having to sift through massive amounts of other files betwixt.

I've tried to duplicate this sort of application with unix's find command, using the -prune option, but I have been unsuccessful.

Can anybody provide insight into this matter for me?

usually, I'm working in the directory where I want to search and want to restrict the search to that location. e.g.

# find . -type d -prune
.

That's it--it usually returns just the current working directory as a result. I know I'm wrong, VERY WRONG. So skip the courtesy of telling me and just correct/inform me proper. :wink: A thousand thanks in advance if you can correctly inform me.

your friendly neighborhood programmar

Are you sure you need find? ls (or even printf) may be suffucient:

ls <dir>

Don't worry, I think everyone stumbles on this "bug" sooner or later.
-prune tells find to only look at the directories specified at the command line, in your case your current working directory, and nothing else. If you run it as

# find ./* -type -d -prune

it will check the contents of the current directory (and nothing below)

I see,
so the original poster wants (eventually) to list directories only.
And again, I suppose that find is not needed:

ls -d */

or (if connected to terminal):

ls -1d */

Or even without external commands (assuming builtin printf):

printf '%s\n' */

Otherwise,
if find is really needed, you may try something like this:

find . ! -name . -prune -type d

And if you have a recent find implementation (GNU, not sure for BSD?):

find -maxdepth 1 -type d

So I fell out of my chair when I realized how ./* made the world of difference. By the way, this performed exactly what I originally intended. Pludi, YOU'RE THE MAN!! Anybody who tells you otherwise probably has parents who are brother and sister. :smiley:

I do have a question for you though...

I understand that sh evaluates ./* to mean everything below the current working directory, but I would naturally assume (.) to include the same. So where did I go wrong? Can you understand the error in my comprehension? Iunno--I guess that's just the way I understood the man page.

Radoulov,

you gave some very good solutions too, namely the option negating anything named . (! -name .). Thanks!

I don't have maxdepth option, I have whichever version of find ships with Solaris 9.x

No. A singular point always means exactly the current directory, just like '..' always means the parent directory, and never the content of it. It's the same as if you'd specify the absolute path: find sees the path, recognizes it as a directory (matching your criteria), knows that it shouldn't descend and moves on to the next path on the command line (none).

If it helps: think of directories as files, where the contents are other files (it's more or less like this for the filesystem). When you want to search the "content" you'll have to state that.

Pludi,

That was educating. Particularly when you underscored that find recognized the current working directory as a directory itself and pruned itself.

So comparing . (cwd) vs ./* (contents of current working directory) really drives the point home when you see the difference in results. I guess I thought the contents of the path specified was implied, but now I know better!

Awesome, thanks for squaring me away.:o

P3@cE!

1 Like