7

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?

7

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.

  • -path (originally a BSD extension, GNU find initially had -wholename later changed to -path IIRC) is a relatively recent (2008) addition to POSIX. You can also use -exec test /proc = {} \;, though that would be very inefficient. – Stéphane Chazelas Sep 26 '18 at 21:18
  • the "-prune" approach was my initial option, but unfortunately "proc" file system type is "procfs" on AIX and "proc" un Linux And unfortunately, aix dont like this: $ find / \( -fstype proc -o -fstype procfs \) -prune | head give the following error: find: invalid filesystem type proc – Gilles Pion Sep 28 '18 at 7:15
  • @GillesPion Then you would have to rely on the name of the directory, unless you detect the operating system type beforehand and call find in specific ways for each operating system. – Kusalananda Sep 28 '18 at 7:22
5

The -fstype directive tells find not to return any results that are on that filesystem type. That, however, doesn't mean that it won't look at the /proc directory. It simply means that it won't return files found on a proc filesystem. However, it still needs to go through the /proc directory since there is no way of knowing whether something, a non-proc filesystem, has been mounted onto /proc/something. For instance, on my Arch, I find:

$ find /proc ! -fstype proc -printf '%p %F\n' 2>/dev/null 
/proc/sys/fs/binfmt_misc binfmt_misc
/proc/sys/fs/binfmt_misc/CLR binfmt_misc
/proc/sys/fs/binfmt_misc/register binfmt_misc
/proc/sys/fs/binfmt_misc/status binfmt_misc

This is because /proc/sys/fs/binfmt_misc/ is not a proc filesystem:

$ mount | grep proc
proc on /proc type proc (rw,nosuid,nodev,noexec,relatime)
systemd-1 on /proc/sys/fs/binfmt_misc type autofs (rw,relatime,fd=51,pgrp=1,timeout=0,minproto=5,maxproto=5,direct,pipe_ino=12642)
binfmt_misc on /proc/sys/fs/binfmt_misc type binfmt_misc (rw,relatime)

So there are two mountpoints under /proc which hold non-proc filesystems.

So, with this in mind, note that your question only shows error messages. None of those files have actually been returned by find, so it would seem to be behaving as expected. It goes through the files in /proc but doesn't consider them matches if they're on a proc filesystem.

  • instead of "won't return files found there" maybe "won't return files of type proc that are found there" ? – Jeff Schaller Sep 26 '18 at 17:25
  • @JeffSchaller I'm trying to weasel out of giving any details because I have only a vague understanding of /proc. Feel even more free than usual to make any improvements. – terdon Sep 26 '18 at 18:42
  • intermediate update: /proc/sys/fs/binfmt_misc may be a separate filesystem from /proc – Jeff Schaller Sep 26 '18 at 18:52
  • I'd add that the OP is seeing those errors because find enumerated /proc before investigating the file types; once it tried to stat them, they were gone (process exited). – Jeff Schaller Sep 26 '18 at 18:55
  • binfmt_misc is simply mounted on a directory inside /proc (you can see that with mount | grep binfmt; there's nothing special about it, you can simply mount any filesystem you like on any directory you like eg. mkdir -p fake_dir; echo systemd666 > fake_dir/comm; mount --bind fake_dir /proc/1 – mosvy Sep 26 '18 at 22:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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