I want to count the number of files and folders contained within a given root folder, potentially excluding certain subfolders.
According to this accepted answer, the basic structure of the command should be
find <root> -path <exclude> -prune -o -name '*.txt' -print
Since I am interested in all files, regardless of name, I remove that flag and alter the print flag to my desired format.
find <root> -path <exclude> -prune -o -printf '%Pn'
The answer is not clear about it, but it turns out <root> and <exclude> cannot both be absolute filepaths. Instead, <exclude> must be given relative to <root>.
With that established, I first count the number of files and folders within the base/ directory, with no exclusions.
$ sudo find /path/to/base -mindepth 1 -printf '%Pn' | wc -l
1000
I then count the number of files and folders within subdir/, a subdirectory of base/. Again, I exclude nothing.
$ sudo find /path/to/base/subdir -mindepth 1 -printf '%Pn' | wc -l
50
Now, if I employ the mechanism from the previously noted answer to count files and folders within base/ while excluding those in base/subdir/, I should get 1000-50 = 950 files and folders returned.
Naturally, that’s not what happens:
$ sudo find /path/to/base -mindepth 1 -path subdir -prune -o -printf '%Pn' | wc -l
1000
Clearly, the folder subdir/ is not being excluded. I have tried all combinations of trailing slashes on /path/to/base and leading and trailing slashes on subdir, but none resolve the issue. Beyond that, I don’t know what I’m supposed to do.
I’m well into my second week of trying to write a single find command in this OS that’s “usable by humans.” How do I make find exclude a directory?