1. Introduction

The ubiquitous find command enables file searching across filesystem boundaries. However, sometimes we might want to exclude a certain filesystem from our search.

In this tutorial, we’ll explore ways to prevent find from searching for files in particular filesystems like the Network FileSystem (NFS) when performing recursive operations. First, we briefly refresh our knowledge about the structure of filesystems. Next, we talk about excluding certain mount points when searching with find. Finally, we discuss two options for avoiding most or particular filesystem types.

We tested the code in this tutorial on Debian 11 (Bullseye) with GNU Bash 5.1.4. It should work in most POSIX-compliant environments unless otherwise specified.

2. Filesystem Structure

Under Linux, / root is the starting point of the filesystem organizational structure as defined in the Filesystem Hierarchy Standard (FHS):

$ ls /
bin   initrd.img      libx32      proc  sys
boot  initrd.img.old  lost+found  root  tmp
dev   lib             media       run   usr
etc   lib32           mnt         sbin  var
home  lib64           opt         srv   vmlinuz

In fact, the specific / root filesystem is specified as a kernel parameter.

Further, we can use paths in that filesystem as mount points for others. Commonly, these reside under some default locations:

However, there are also systems with separate /tmp, /var, and other partitions. In fact, we can employ any accessible path as a mount point as long as it’s free.

To see a formatted list of mount points and their associated filesystems with their types, we can leverage the df command with its –output option:

$ df --output=source,fstype,target
Filesystem     Type     Mounted on
udev           devtmpfs /dev
tmpfs          tmpfs    /run
/dev/sda1      ext4     /
[...]

Moreover, we can see each partition Type as well. Let’s see how to apply this information for our purposes.

3. Exclude by Mount Point

Considering filesystems are always associated with mount points, we can use this to our advantage. For example, we might want to exclude a certain partition from the (default) recursive search.

Let’s locate the partition mount point first:

$ df --output=source,fstype,target
Filesystem     Type     Mounted on
udev           devtmpfs /dev
tmpfs          tmpfs    /run
/dev/sda1      ext4     /
tmpfs          tmpfs    /dev/shm
tmpfs          tmpfs    /run/lock
/dev/sda2      ext4     /home
/dev/sda3      ext4     /tmp
/dev/sda4      ext4     /var
tmpfs          tmpfs    /run/user/0
tmpfs          tmpfs    /run/user/1000
xost:/nfs      nfs4     /mnt/nfsx
web:/nfs       nfs4     /mnt/nsfw

In our hypothetical, we don’t want to go through either the /mnt/nfsx or /mnt/nsfw NFS mounts. To achieve this with find, we can use the standard -not operator for -path:

$ find / -not -path '/mnt/nfsx/*' -and -not -path '/mnt/nsfw/*'

While this does work, it has several drawbacks:

  • unless we know the original filesystem, device, or mount point, manual analysis of the df output or similar is more or less our only choice
  • exclusion is based on a string match (/mnt/nsfw/* won’t match ./nsfw even if they expand to the same path)
  • wildcards are too general and can often lead to unwanted exclusions (/mnt/nfsw* also excludes /mnt/nfsw2/*)
  • unless we specifically exclude them, the root paths (/mnt/nfsx and /mnt/nsfw, no trailing slash) remain in the output

Considering these potential problems, let’s explore another option.

4. Exclude by Filesystem

Like others, the find command comes in two main versions: standard and GNU. While the former has certain filters, the latter provides exclusive features that facilitate even more fine-grained searching.

4.1. Other Filesystems via xdev

To avoid any filesystem other than the one of the initial path, the standard POSIX find supports the -xdev switch:

$ find / -xdev

In short, the -xdev flag of the find command will print but not traverse paths that are mount points. In our earlier example, both /mnt/nfsx and /mnt/nsfw will be listed, but the search wouldn’t descend below these directories.

While this is a way to exclude NFS mounts, it also includes any other non-starting filesystem.

4.2. Specific Filesystem Types via fstype

One of the GNU find features is the filesystem type filter fstype:

$ find / -fstype nfs4

In this case, we only search through nfs4 mounts. To get a complete list of filesystem types supported by the current kernel, we can check the second column of the /proc/filesystems file:

$ cat /proc/filesystems
[...]
        ext3
        ext2
        ext4
[...]
        xfs
        jfs
        msdos
        vfat
        minix
        hfs
        hfsplus
        qnx4
        ufs
        btrfs
nodev   binfmt_misc
        udf
        iso9660
nodev   nfs
nodev   nfs4
        squashfs

Notably, nodev in the first column means the given type needs no physical devices on the system.

Combining -fstype with -not, we can exclude certain filesystem types during a search with find:

$ find / -not -fstype nfs4

Unfortunately, we can’t specify more than one filesystem type criteria. Still, we can use -fstype and multiple -path operators to gain more control.

5. Summary

In this article, we explored ways to limit the filesystems find traverses.

In conclusion, depending on our situation, we can exclude specific paths and filesystem types or avoid all non-starting filesystems.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.