Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I detect whether a symlink is broken in Bash?

Tags:

bash

symlink

I run find and iterate through the results with [ \( -L $F \) ] to collect certain symbolic links.

I am wondering if there is an easy way to determine if the link is broken (points to a non-existent file) in this scenario.

Here is my code:

FILES=`find /target/ | grep -v '\.disabled$' | sort`  for F in $FILES; do     if [ -L $F ]; then         DO THINGS     fi done 
like image 983
zoltanctoth Avatar asked Nov 08 '11 10:11

zoltanctoth


People also ask

How do you check if a symlink is broken?

3. Finding Broken Symlinks. The -H, -L and -P options control how symbolic links are treated, and when omitted, use -P as the default. When -P is used and find examines or prints information from a symbolic link, the details are taken from the properties of the symbolic link itself.

How do I know if a symbolic link is working?

Use the ls -l command to check whether a given file is a symbolic link, and to find the file or directory that symbolic link point to. The first character “l”, indicates that the file is a symlink.

How can you find the broken link in Linux?

Using the Find Command The find command allows you to report and delete dead soft links on your system easily as well. To find broken links present in any other directory on your system, just replace the . (dot) character with the directory path.


1 Answers

# test if symlink is broken (by seeing if it links to an existing file) if [ ! -e "$F" ] ; then     # code if the symlink is broken fi 
like image 149
Roger Avatar answered Oct 06 '22 23:10

Roger