33

Say I need to do:

find / -name somefile.txt

and say root partition / is mounted on /dev/sda5; however, let's say I also have 250GB partitions (/dev/sda6, /dev/sda7) mounted in /media - AND another location that I cannot currently remember. Say, also, that I know the file I'm looking for is on /dev/sda5.

Obviously, the above command will also descend in /media and that other directory which represent the big partitions, wasting time in looking for the file in the wrong place.

Is there a way to instruct find (or other command) to search only / on /dev/sda5, and NOT to descend to directories if they are on different partitions?

2 Answers 2

49

Use the -xdev argument to find

-xdev Don’t descend directories on other filesystems.

3
  • 1
    Thanks much - just for reference, the syntax would be: <pre>find / -xdev -name somefile.txt</pre>
    – sdaau
    Commented Jun 11, 2010 at 13:43
  • 1
    For mac users: find -x / -name somefile.txt
    – CodeReaper
    Commented Jan 31, 2014 at 9:58
  • 1
    Searching the manpage for --one-file-system a la du did me no good either. At least they both begin with -x I suppose. And it means "cross devices" I think. Commented Jun 1, 2018 at 0:16
5

There are three different options, depending on your find implementation: -xdev, -mount and/or -x. Each one of them might behave slightly different, also depending on the implementation.

The POSIX standard defines the -xdev ”primary“:

it shall cause find not to continue descending past directories that have a different device ID

This is implemented in GNU's find (i.e. the findutils; docs).
GNU find also allows you to use -mount as ”an alternate name for -xdev, for compatibility with some other versions of find.

On BSD systems and macOS the option is -x instead, which is not a primary. They call -xdev to be ”deprecated“. You can, however, use -mount instead. [manpages of FreeBSD and macOS]

additional info

By the way, there are some open issues on issue trackers:
https://www.austingroupbugs.net/view.php?id=1133 (POSIX)
https://savannah.gnu.org/bugs/?54745 (GNU findutils)

You might find this question on Unix SE interesting:
https://unix.stackexchange.com/questions/479886/what-is-the-history-behind-the-name-of-the-xdev-option-in-find

tools other than find

If you're using ripgrep (rg) like rg --files, you can use the --one-file-system option, which does the same like find's -xdev option.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .