The -prune
primary tells find
not to recurse under a directory.
find -L -path ~/.wine/dosdevices -prune -o -type f -name 'My Favorite Movie.*' -print
If you want to use -lname
in your condition, you can't use the -L
option, because -L
causes most predicates to act on the target of the link, including -lname
. My recommendation in that case would be to use both your home directory and the hard disk root as the roots of your search.
find ~ /mnt/hdd -xtype f -name 'My Favorite Movie.*'
You might run find ~ -type l …
to gather a list of symbolic links and use them as additional roots.
( IFS=$'\n'; set -f;
find ~ $(find ~ -type l -lname '/mnt/hdd/*') \
-xtype f -name 'My Favorite Movie.*' )
If you really want to recurse under symbolic links, you can exclude specific symbolic links by target instead of by name. However you can only exclude a finite list, not a pattern or subtree this way.
find -L \( -samefile /exclude/this -o -samefile ~/and/that \) -prune -o \
-type f -name 'My Favorite Movie.*' -print
You can use other criteria such as ! -writable
(files that you don't have write permission to), but I don't think GNU find has a way to recurse under symbolic links only if their target text matches a certain expression.
You could build a find
command that does almost what you want except for not excluding enough symbolic links, run find2perl to convert the query to a Perl script, and tweak the Perl script a bit. There are many switches of GNU find that find2perl doesn't recognize, however.
With GNU find (the implementation on non-embedded Linux and Cygwin):
find /search/location -type l -xtype d
With find implementations that lack the -xtype
primary, you can use two invocations of find
, one to filter symbolic links and one to filter the ones that point to directories:
find /search/location -type l -exec sh -c 'find "$@" -L -type d -print' _ {} +
or you can call the test
program:
find /search/location -type l -exec test {} \; -print
Alternatively, if you have zsh, it's just a matter of two glob qualifiers (@
= is a symbolic link, -
= the following qualifiers act on the link target, /
= is a directory):
print -lr /search/location/**/*(@-/)
Best Answer
Hard links. That's the only type for which its even possible to count. Pretty much every find option is something fairly trivially obtained for each file; the hard link count comes from
stat
(and is also displayed byls
, by the way).(To count symlinks, you'd have to examine every symlink and check where it points. But that's not even possible—symlinks can cross filesystems, and all of them may not be mounted. Or could be mounted in different locations, which could leave the symlinks pointing the different things. You could count only the ones in the files find searches through, but that's very non-trivial for a file match flag.)