41

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

56

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 psuedo-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 / -name /path/to/ignore -prune -o -name .vimrc
| improve this answer | |
  • does that work for grep too? – Flotsam N. Jetsam Jan 5 '11 at 15:36
  • 1
    I don't think grep has such an option. I usually used find pipe to grep as shown in this answer: superuser.com/questions/80033/… . Lately, I've been using ack (betterthangrep.com) instead, but ack doesn't seem to have an option to search only local drives. – Doug Harris Jan 5 '11 at 15:43
  • My edit was rejected to this, so just making it a comment. Basically the first example is wrong, because you need to add -prune to it or it will still traverse the undesired file systems. The second example I believe was meant to be -path instead of -name so it will ignore the path. FWIW... the last example does work, it stays on the "current filesystem" so doesn't traverse others. – rogerdpack Apr 16 '19 at 17:48
  • This answer needs an edit... it's now 2020, and this just does not work on macOS any longer. – Seamus Oct 13 at 8:22
  • This is current up to MacOS Mojave. I have not installed Catalina yet so I cannot update. If somebody else would read the find man page and submit an edit, I'd appreciate it. – Doug Harris Oct 13 at 14:37
24

man find shows:

-xdev Don't descend directories on other filesystems.

| improve this answer | |
  • 3
    -xdev is the same as -mount FWIW... :) – rogerdpack Apr 16 '19 at 17:48
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.

| improve this answer | |

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.