“find” exclude by fstype strange behavior

filesystemsfindproc

I try to have "find" command exclude "/proc" filesystem.
I would prefer a portable solution to be able to use it on non GNU based hosts (oldest AIX for instance, where "find" do not have the "-path" option)

Here's what I've tried (on a Linux host) and a partial filtered output:

$  find /  ! -fstype proc   >/dev/null
find: `/proc/9475/task/9535/fdinfo/7': No such file or directory
find: `/proc/10856': No such file or directory
find: `/proc/10856': No such file or directory
find: `/proc/10858': No such file or directory
find: `/proc/10858': No such file or directory
find: `/proc/15650': No such file or directory
find: `/proc/15650': No such file or directory
find: `/proc/15654': No such file or directory
find: `/proc/15654': No such file or directory
find: `/proc/15656': No such file or directory
find: `/proc/15656': No such file or directory
find: `/proc/15657': No such file or directory
find: `/proc/15657': No such file or directory
find: `/proc/15663': No such file or directory
etc ....

I don't understand why "find" keep walking inside the "/proc" file system despite of the "! -fstype proc" switch?

Best Answer

As terdon explained, find will still enter the /proc directory, but it will not return any pathnames from that filesystem type.

To remove any search path as soon as the proc filesystem type is detected, use

find / -fstype proc -prune -o -print

However, since you're wanting to do this on an old find, the -fstype option may not be available. If -path (a standard option of find) is also not available, then it begins to be a bit difficult to come with suggestions as the implementation is clearly not POSIX compliant.

One option could be to use -xdev which would cause find to stay strictly in the same filesystem as the starting path. You would then have to run one find for each filesystem that you want to examine. There could also be an option called -x (as on BSD systems) which acts like -xdev.

If that too is not possible, then you may have to resort to using -type d -name proc -prune to disregard all directories that are called proc.

Related Question