Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

given the following structure:

oz123@debian:~/ $ tree .
.
├── a
│   ├── a1
│   ├── a2
│   └── a3
├── a1
│   ├── a11
│   ├── a12
│   └── a31
├── b
│   └── b1
│       ├── b11
│       │   └── b21
│       │       └── b31
│       ├── b12
│       └── b3
└── c

16 directories, 0 files

How do I find all the end nodes?

I found the following solutions which seems to be good, but I have to proof that there is not test case which will fail it.

The help page of the -links states:

You can also search for files that have a certain number of links, with ‘-links’. Directories normally have at least two hard links; their . entry is the second one. If they have subdirectories, each of those also has a hard link called .. to its parent directory. The . and .. directory entries are not normally searched unless they are mentioned on the find command line.

possible solution:

oz123@debian:~/ $ find .  -type d  -links 2
./a/a2
./a/a3
./a/a1
./c
./a1/a31
./a1/a11
./a1/a12
./b/b1/b12
./b/b1/b3
./b/b1/b11/b21/b31
  • Can anyone provide a better solution (without using pipes and sed, this has be performant ...)
  • Will it work on any filesystem?
share|improve this question
    
It will work on any UNIX filesystem, don't know how the vfat and ntfs-3g drivers emulate inode semantics. –  Hubert Kario Aug 12 '13 at 8:40
    
@HubertKario, a most important question will it work on NetApp filers over NFS? –  Oz123 Aug 12 '13 at 8:56
    
I'm not sure about NetApp, but it works over NFS between Linux boxes so I'd guess that yes. –  Hubert Kario Aug 13 '13 at 13:58
add comment

1 Answer

up vote 1 down vote accepted

http://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 "{}"' \;

Slower than

find .  -type d  -links 2

anyway.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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