find / grep command without searching mounted shares

36

10

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

Flotsam N. Jetsam

Posted 2011-01-05T15:27:56.643

Reputation: 1 291

Answers

50

Use the -fstype local option to find:

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

Update:

The local psuedo-fstype is available in the version of find that comes with OS X, but is not in GNU find (fstypes recognized by GNU find).

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

find / -mount -name .vimrc

Doug Harris

Posted 2011-01-05T15:27:56.643

Reputation: 23 578

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 – 2019-04-16T17:48:07.403

does that work for grep too? – Flotsam N. Jetsam – 2011-01-05T15:36:26.057

1

I don't think grep has such an option. I usually used find pipe to grep as shown in this answer: http://superuser.com/questions/80033/command-line-wizardry-spaces-in-file-names-with-find-grep-xargs/80050#80050 . Lately, I've been using ack (http://betterthangrep.com/) instead, but ack doesn't seem to have an option to search only local drives.

– Doug Harris – 2011-01-05T15:43:11.150

21

man find shows:

-xdev Don't descend directories on other filesystems.

penguinjeff

Posted 2011-01-05T15:27:56.643

Reputation: 309

3-xdev is the same as -mount FWIW... :) – rogerdpack – 2019-04-16T17:48:17.623

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.

Shaun Saunders

Posted 2011-01-05T15:27:56.643

Reputation: 1