I am working on a script that, among other things, extracts a list of all items in a directory (files and subdirs) for archival but it needs to skip remote symlinks (i.e. on other file systems). The script was migrated from Solaris where find . -mount seems to have excluded symlinks to other file systems.

The find man page for -mount says

-mount Don't  descend directories on other filesystems.  An alternate name for -xdev, for compatibility with some other versions of find.

I tried -xdev but that didn't work either.

As a result of the difference, my remote symlinks get deleted.

Any idea how I can fix this as a one-liner to eliminate remote symlinks from being returned?

  • 1
    I'd expect -xdev not to exclude symlinks to other filesystems, except with -L. In other words, it sounds like Solaris is buggy here. What is your exact command line? Feb 5 '13 at 22:43
  • find . -mount -print
    – amphibient
    Feb 5 '13 at 22:44
  • What is your exact command line? find . -mount -print doesn't delete anything.
    – Mikel
    Mar 8 '13 at 2:48

The Solaris behavior is buggy. -mount a.k.a. -xdev does not mean that symbolic links that point outside the starting filesystem should be excluded: this behavior is not even described in the Solaris documentation, and other implementations don't do this. What -xdev means is that when find comes to a mount point in its recursive directory traversal, it skips the mount point altogether.

I don't think there's any easy way to filter find matches by the device containing the target of a symbolic link. Here's a method that relies on the stat utility on Linux (it won't work on Solaris).

find /some/path -xdev \( ! -type l -o -exec sh -c '[ "$0" = "$(stat -L -fc %t "$1")" ]' "$(stat -fc %t /some/path)" {} \; \) -print

I.e. print everything that isn't a symbolic link, and for symbolic links, print only if the filesystem number of the target matches the filesystem number of the starting point. Dangling symbolic links are excluded and trigger an error message; add -o -xtype l after ! -type l to include them, and add -xtype l -o after -xdev to exclude them silently.

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.