Find followed links ignoring file system loops

find

When I try to find all files/directories, that link to the /tmp folder, I try

ls -di /tmp 

which gives me the inode 4194305 for /tmp

Now I would find all files linked to that inode with

find / -follow -path /media -prune -inum $(ls -di /tmp |cut -d" " -f1)

but this will give me thousands of files system loops detected warnings:

find: File system loop detected; `/sys/devices/platform/reg-dummy

/subsystem/devices/serial8250/tty/ttyS2/subsystem/ttyS0/device/firmware_node/subsystem/devices/LNXSYSTM:00/LNXSYBUS:00/PNP0C0C:00/input/input1/subsystem/event2/device/device/driver/PNP0C0C:00' is part of the same file system loop as `/sys/devices/platform/reg-dummy/subsystem/devices/serial8250/tty/ttyS2/subsystem/ttyS0/device/firmware_node/subsystem/devices/LNXSYSTM:00/LNXSYBUS:00/PNP0C0C:00'.
find: File system loop detected; `/sys/devices/platform/reg-dummy/subsystem/devices/serial8250/tty/ttyS2/subsystem/ttyS0/device/firmware_node/subsystem/devices/LNXSYSTM:00/LNXSYBUS:00/PNP0C0C:00/input/input1/subsystem/event2/device/device/driver/LNXPWRBN:00' is part of the same file system loop as `/sys/devices/platform/reg-dummy/subsystem/devices/serial8250/tty/ttyS2/subsystem/ttyS0/device/firmware_node/subsystem/devices/LNXSYSTM:00/LNXSYBUS:00/PNP0C0C:00/input/input1/subsystem/event2/device/device'.
find: File system loop detected; `/sys/devices/platform/reg-dummy/subsystem/devices/serial8250/tty/ttyS2/subsystem/ttyS0/device/firmware_node/subsystem/devices/LNXSYSTM:00/LNXSYBUS:00/PNP0C0C:00/input/input1/subsystem/event2/device/device/input/input2' is part of the same file system loop as `/sys/devices/platform/reg-dummy/subsystem/devices/serial8250/tty/ttyS2/subsystem/ttyS0/device/firmware_node/subsystem/devices/LNXSYSTM:00/LNXSYBUS:00/PNP0C0C:00/input/input1/subsystem/event2/device'.
find: File system loop detected; `/sys/devices/platform/reg-dummy/subsystem/devices/serial8250/tty/ttyS2/subsystem/ttyS0/device/firmware_node/subsystem/devices/LNXSYSTM:00/LNXSYBUS:00/PNP0C0C:00/input/input1/subsystem/event2/device/event2' is part of the same file system loop as `/sys/devices/platform/reg-dummy/subsystem/devices/serial8250/tty/ttyS2/subsystem/ttyS0/device/firmware_node/subsystem/devices/LNXSYSTM:00/LNXSYBUS:00/PNP0C0C:00/input/input1/subsystem/event2'.
find: File system loop detected; `/sys/devices/platform/reg-dummy/subsystem/devices/serial8250/tty/ttyS2/subsystem/ttyS0/device/firmware_node/subsystem/devices/LNXSYSTM:00/LNXSYBUS:00/PNP0C0C:00/input/input1/device' is part of the same file system loop as `/sys/devices/platform/reg-dummy/subsystem/devices/serial8250/tty/ttyS2/subsystem/ttyS0/device/firmware_node/subsystem/devices/LNXSYSTM:00/LNXSYBUS:00/PNP0C0C:00'.
find: File system loop detected; `/sys/devices/platform/reg-dummy/subsystem/devices/serial8250/tty/ttyS2/subsystem/ttyS0/device/firmware_node/subsystem/devices/LNXSYSTM:00/LNXSYBUS:00/PNP0C0C:00/input/input1/event1/subsystem/mice/subsystem' is part of the same file system loop as `/sys/devices/platform/reg-dummy/subsystem/devices/serial8250/tty/ttyS2/subsystem/ttyS0/device/firmware_node/subsystem/devices/LNXSYSTM:00/LNXSYBUS:00/PNP0C0C:00/input/input1/event1/subsystem'.

How do I ignore these errors?

Best Answer

The root of your problem is that you told find to follow symbolic links. That's rarely a good idea.

Instead, keep acting on the symbolic links, and filter the ones you want.

You're filtering files by their inode number, but this looks like a mistake: it looks like you're looking for files that are the same as /tmp, not files that have the same inode number as /tmp. Your command also finds files that have the same inode as /tmp but are located on a different filesystem.

Many shells (ksh, bash, zsh, dash) have a -ef operator to the test command (also spelled [ … ]) that tests whether two files are the same.

find / -path /media -prune -o -type l -exec ksh -c 'for x; do [ "$0" -ef "$x" ] && printf %s\\n "$x"; done' /tmp {} +

Alternatively, you can do this in zsh.

print -lr -- /^media/**/*(@e\''[[ $REPLY -ef /tmp ]]'\')
Related Question