Use GNU find to show only the leaf directories

I'm trying to use GNU find to find only the directories that contain no other directories, but may or may not contain regular files.

My best guess so far has been:

find dir -type d \( -not -exec ls -dA ';' \)

but this just gets me a long list of "."

Thanks!

You can use -links if your filesystem is POSIX compliant (ie, a directory has a link for each subdirectory in it, a link from its parent and a link to self, thus a count of 2 link 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 mentionned). Here is another version that is slower, but does work on Mac OS X. It is based on his version, with correction to handle whitespace in directory names:

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

bash - Use GNU find to show only the leaf directories, You can use -links if your filesystem is POSIX compliant (ie, a directory has a link for each subdirectory in it, a link from its parent and a link to self, thus a count of  The GNU find utility is one of the most useful commands you’ll ever get to know. At first glance the options and syntax can seem arcane. But, with a little practice, you’ll be able to locate any file on your system with almost no trouble at all. To help you get started, we’ve got 10 ways you can use find to get what you’re looking for.

find all end subdirectories in a tree, I'm trying to use GNU find to find only the directories that contain no other directories, but may or may not contain regular files. My best guess so far has been:  Use GNU find to show only the leaf directories. I'm trying to use GNU find to find only the directories that contain no other directories, but may or may not contain regular files. My best guess so far has been: but this just gets me a long list of ".".

Use GNU find to show only the leaf directories - bash - iOS, https://stackoverflow.com/questions/4269798/use-gnu-find-to-show-only-the-leaf-​directories find . -type d -exec sh -c '(ls -p "{}"|grep />/dev/null)||echo "{}"' \;. GNU find searches the directory tree rooted at each given file name by evaluating the given expression from left to right, according to the rules of precedence (see section OPERATORS), until the outcome is known (the left hand side is false for and operations, true for or), at which point find moves on to the next file name.

What about this one ? It's portable and it doesn't depend on finnicky linking counts. Note however that it's important to put root/folder without the trailing /.

find root/folder -type d | awk '{ if (length($0)<length(prev) || substr($0,1,length(prev))!=prev) print prev; prev=($0 "/") } END { print prev }'

find all end subdirectories in a tree, I'm trying to use GNU find to find only the directories that contain no other directories, but may or may not contain regular files. My best guess so far has been:  I'm using find to all files in directory, so I get a list of paths. However, I need only file names. i.e. I get ./dir1/dir2/file.txt and I want to get file.txt. In GNU find you can use -printf parameter for that, e.g.: Clearly the answer, but it lacks detail. – Jason McCreary Sep 20 '13 at 15:31. If your find doesn't have a -printf option you

Here is solution which works on Linux and OS X:

find . -type d -execdir bash -c '[ "$(find {} -mindepth 1 -type d)" ] || echo $PWD/{}' \; 

or:

find . -type d -execdir sh -c 'test -z "$(find "{}" -mindepth 1 -type d)" && echo $PWD/{}' \;

Use GNU find to show only the leaf directories, Here is how to control which directories find searches, and how it searches them. -maxdepth 0 ' means only apply the tests and actions to the command The ' -​depth ' option is a POSIX feature, so it is better to use that. entries in the directory are non-directories (leaf files in the directory tree). If instead of echo, we use ls, the shell is still what is expanding the list of filenames.The shell is the reason to get a list of directories in the PWD. The -d option to ls makes it list the present directory entry instead of the contents of each directory (as presented by default).

Finding Files, Here is how to control which directories find searches, and how it searches them. -maxdepth 0 ' means only apply the tests and actions to the command line arguments. The ' -depth ' option is a POSIX feature, so it is better to use that. rest of the entries in the directory are non-directories (leaf files in the directory tree). Linux or UNIX-like system use the ls command to list files and directories. However, ls does not have an option to list only directories. You can use combination of ls command and grep command to list directory names only. You can use the find command too. In this quick tutorial you will learn how to list only directories in Linux or UNIX.

Directories (Finding Files), The only exception to this behaviour is when a file specified on the command GNU find frequently stats files during the processing of the command line itself, of the entries in the directory are non-directories ('leaf' files in the directory tree). To ignore a directory and the files under it, use -prune; see an  4.5 Searching Directories for Prerequisites. For large systems, it is often desirable to put sources in a separate directory from the binaries. The directory search features of make facilitate this by searching several directories automatically to find a prerequisite.

Linux find command help and examples, GNU find searches the directory tree rooted at each given file name by or `/' is generally safer if you use wildcards in the list of start points). The only exception to this behaviour is when a file specified on the command line is a sym​‐ rest of the entries in the directory are non-directories (`leaf' files in the directory tree). When find is examining a directory, after it has statted 2 fewer subdirectories than the directory’s link count, it knows that the rest of the entries in the directory are non-directories (leaf files in the directory tree). If only the files’ names need to be examined, there is no need to stat them; this gives a significant increase in

Comments
  • When using -exec, the {} argument is expanded to the path of the currently inspected filesystem object (file / directory / ...). So you should have used the following command to print the directories : find dir -type d \( -not -exec ls -dA {} \; \)
  • Same question on Super User: Using "find" to list only directories with no more childs
  • See also: List all leaf subdirectories in linux.
  • @SylvainDefresne, any idea if it will work on NetApp file system over NFS?
  • I just used the first version (-links 2) on an NetApp over NFS. So the answer is yes.
  • Similarly, the simple soln doesn't seem to work in Cygwin (windows 7), but the extended OSx version does
  • in my btrfs system directories have link count 1, so this doesn't work.
  • The replacement string {} should be single-quoted to sh -c, not double quoted, since filenames might contain characters treated specially under double quotes (such as $).