It's easier than you thought and it relies on a simple trick with the find command - the -L option will dereference symbolic links, so the -type test will never match symlink... unless they are dead!
$ find -L /some/where -type l -delete
This will only delete symbolic links that are referencing a non-existent file, hence they can not be dereferenced to another type.
@oldbenko, the approach has a flaw, as Ravi correctly pointed.
In general, following symlinks in find commands is a bad approach due to loops in filesystems.
However, there is a better solution, which will not fall in the loop:
$ find /some/where -xtype l -delete
This will only delete symbolic links that are referencing a non-existent file, without running into any loop.
This command without the delete option creates a loop on my debian system.. Redirecting output does give a short list of files as expected in above command.
find: File system loop detected; ‘/home/ravi/.googleearth/instance-running-lock/task/3967/cwd/sys/devices/pci0000:00/0000:00:1f.3/subsystem/devices/0000:00:1c.1/driver/0000:00:1c.2/pci_bus/0000:08/subsystem/0000:09/device/0000:09:00.0/net/wlan0/subsystem/eth0/device/firmware_node/subsystem/devices/PNP0103:00/physical_node/subsystem/devices/PNP0C0B:02/driver/PNP0C0B:03/thermal_cooling/subsystem/cooling_device3/device/physical_node/subsystem/devices/cpu1/driver/cpu2/node0/memory19/subsystem/devices/memory33/subsystem’ is part of the same file system loop as ‘/home/ravi/.googleearth/instance-running-lock/task/3967/cwd/sys/devices/pci0000:00/0000:00:1f.3/subsystem/devices/0000:00:1c.1/driver/0000:00:1c.2/pci_bus/0000:08/subsystem/0000:09/device/0000:09:00.0/net/wlan0/subsystem/eth0/device/firmware_node/subsystem/devices/PNP0103:00/physical_node/subsystem/devices/PNP0C0B:02/driver/PNP0C0B:03/thermal_cooling/subsystem/cooling_device3/device/physical_node/subsystem/devices/cpu1/driver/cpu2/node0/memory19/subsystem’.
@Ravi_Shanker wrote:This command without the delete option creates a loop on my debian system.. Redirecting output does give a short list of files as expected in above command.
find: File system loop detected; [...]
It is not the find command that produces the loop. It is that it shows an existing loop ! Likely, the instance-running-lock/task points to some task under /proc . Therefore the loop exists and you will have issues with whichever run of find, if you dereference links (-L or -follow).
@oldbenko, the approach has a flaw, as Ravi correctly pointed.
In general, following symlinks in find commands is a bad approach due to loops in filesystems.
However, there is a better solution, which will not fall in the loop:
$ find /some/where -xtype l -delete
This will only delete symbolic links that are referencing a non-existent file, without running into any loop.
I argue that it's the loop that is the problem, not the command.
But thanks for suggesting an alternative (although this is GNU find specific, it's not found in BSD fileutils).
Red Hat
Learning Community
A collaborative learning environment, enabling open source skill development.