2

So, let's say I use find to search a directory for folders.

find "/home/user/Documents" -type d -print

While this does list all the directories in said location, I only want to get the paths of the bottom level directories. (Ex: Directories that have no subdirectories/folders)

Is there an easy way to do this using find?

1

1 Answer 1

0

I only want to get the paths of the bottom level directories

Use the -links 2 command line option.

You can use -links if your filesystem is POSIX compliant (i.e. a directory has a link for each subdirectory in it, a link from its parent and a link to itself, thus a count of 2 links if it has no subdirectories).

The following command should do what you want:

find dir -type d -links 2

However, it does not seems to work on Mac OS X (as @Piotr mentioned). Here is another version that is slower, but does work on Mac OS X. It is based on his version, with a correction to handle whitespace in directory names:

find . -type d -exec sh -c '(ls -p "{}"|grep />/dev/null)||echo "{}"' \;

Source: Use GNU find to show only the leaf directories answer by Sylvain Defresne

1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .