I'm trying to write a cross-platform command line that will find all local files on a file system.
My intention is to use the find
command, using only POSIX parameters. Is there a way to skip files on remote filesystems, while restricted to only POSIX commands, and no programming?
My current command line is
find -type f -print
but that obviously lists every regular file, not just local ones.
I am aware of the -local
flag, but as far as I can tell, it's not POSIX or otherwise part of a cross-platform standard. locate
doesn't appear to be cross-platform either. I don't think the -xdev
flag is what I'm looking for, either, as I want to search across all locally mounted file systems – if /
and /home
are both local, and I run find / -xdev -print
, find
should skip /home
(unless I'm misunderstanding stuff).
For what it's worth, the following URLs have been useful resources…
Best Answer
I don't believe that there is any way to determine whether a filesystem is remote or local using only POSIX interfaces. POSIX doesn't even mandate a
mount
command, and all references to file systems, as far as I know, are replete with the words "implementation-defined".The
-local
primary to thefind
command is not much help, since it is only available on Solaris, and it depends on the existence of a configuration file which lists remote filesystem types.POSIX does mandate a
df
command and requires a specific output format if that command is used with the-P
flag; in that format, there are six space-separated fields, of which the first is the "name of the file system, in an implementation-defined format" and the last is the mount point.What you need is a list of all the mount points corresponding to local filesystems. Here, I think you're going to have to fall back to a heuristic; which is to exclude filesystem names which "look like" remote filesystems. Some examples:
That's not an exhaustive list, and it comes completely without guarantees.
If you then filter those lines out of the output of
df
and then remove the first five columns, you could then feed the list to thefind
command with the-xdev
primary.Even if that sounds plausible, there is actually no guarantee that a filesystem is actually "mounted" (see AFS) or that a filesystem is either entirely local or entirely remote (a fuse-based filesystem can arbitrarily redirect individual files). So proceed with caution, is my advice.