52

When I used the find command, I almost always need to search the local drives. But, I almost always have super large network shares mounted and these are included in the search. Is there an easy way to exclude those in the find command, grep and other similar commands? Example:

find / -name .vimrc

3 Answers3

72

If you're using GNU find (as is used on most Linux systems), you'll want to use -mount:

find / -mount -name .vimrc

OS X/MacOS provides the local pseudo-fstype. This not in GNU find (fstypes recognized by GNU find).

Use the -fstype local option to find on MacOS:

find / -fstype local -name .vimrc

If you want to exclude only specific paths, you could use -prune:

find / -path "/path/to/ignore" -prune -o -name .vimrc

Update: MacOS Catalina (Jan 2022)

I'm two major releases behind the latest MacOS, but some has changed.

First, MacOS now supports the -mount option. The man page for find says "The same thing as -xdev, for GNU find compatibility."

Second, -fstype local still seems to work. The man page Indiates that it "matches any file system physically mounted on the system where the find is being executed".

I did a test with two drives mounted in /Volumes, one a USB flash drive (which I think counts as physically mounted) and the other a network drive. I ran find /Volumes <option> -name '*txt'. Running with -mount returned no results immediately since there were no such files on the current filesystem. Running with -fstype local found a .txt file on the USB drive very quickly and then seemed to traverse the full mounted drive without returning any files (even though some do exist).

Doug Harris
  • 28,397
27

man find shows:

-xdev Don't descend directories on other filesystems.

0

Original question was to find on local disk only, so for the sake of completeness, here's what I used;

for PART in `awk '(!/^#/ && $6 != "0" || $3 == "xfs" ) { print $2 }' /etc/fstab 2>/dev/null`; do find $PART -xdev -name .vimrc -print 2>/dev/null; done

As long as your fstab is set up properly it should only search the local disks; ie, cifs mounts should have that final flag set to 0. I included the OR for xfs filesystems when we started going to RHEL7, they should be set to 0 also as they aren't meant to do the disk reorg after so many restarts.

Hope that helps.