Using scripts automatically remove the symbolic link missing link files in the specified folder

Scripts can be cleared under the specified folder, symbolic links on the visual link to the file.
When using Linux, often create a symbolic link for itself many files or programs, so you do not always go to the corresponding folder to find the corresponding file but only for frequently accessed files need to create a symbolic link to it, so you often need to be able to access files on the desktop, or assigned to another folder.
When this visit, a lot of convenience, but often left exhausted after many symbolic links that require the user to manually confirm whether you can delete, bring a lot of inconvenience for the use of linux, the script will be able to use your liberation hands only need to perform at the script, you can remove the symbolic link to the corresponding folder of no use.

#!/bin/bash
# 一个可以测试链接断掉的符号链接的文件,并且可以输出它们指向的文件
# 以便于它们可以把输出提供给xargs来进行处理 :)
# 比如. broken-link.sh /somedir /someotherdir|xargs rm
#
#下边的方法, 不管怎么说, 都是一种更好的办法:
#
#find "somedir" -type l -print0|\
#xargs -r0 file|\
#grep "broken symbolic"|
#sed -e 's/^\|: *broken symbolic.*$/"/g'
#
#但这不是一个纯粹的bash脚本, 最起码现在不是.
#注意: 谨防在/proc文件系统和任何死循环链接中使用!
##############################################################
#如果没有参数被传递到脚本中, 那么就使用
#当前目录. 否则就是用传递进来的参数作为目录
#来搜索.
####################
[ $# -eq 0 ] && directorys=`pwd` || directorys=$@

#编写函数linkchk用来检查传递进来的目录或文件是否是链接,
#并判断这些文件或目录是否存在. 然后打印它们所指向的文件.
#如果传递进来的元素包含子目录,
#那么把子目录也放到linkcheck函数中处理, 这样就达到了递归的目的.
##########
linkchk () {
    for element in $1/*; do
        [ -h "$element" -a ! -e "$element" ] && echo \"$element\"
        [ -d "$element" ] && linkchk $element
        # 当然, '-h'用来测试符号链接, '-d'用来测试目录.
    done
}
#把每个传递到脚本的参数都送到linkchk函数中进行处理,
#检查是否有可用目录. 如果没有, 那么就打印错误消息和
#使用信息.
################
for directory in $directorys; do
    if [ -d $directory ]
        then linkchk $directory
        else
            echo "$directory is not a directory"
            echo "Usage: $0 dir1 dir2 ..."
    fi
   done
exit 0
# 创建一个新文件 name
andrew@andrew:/work/bash/src$ touch name
# 为name创建符号链接
andrew@andrew:/work/bash/src$ ln -s name aaa
# 删除name文件, aaa将会变成丢失链接文件的符号链接
andrew@andrew:/work/bash/src$ rm name
# 查看aaa为执行当前目录下的name的符号链接文件
andrew@andrew:/work/bash/src$ ls -l
总用量 44
lrwxrwxrwx 1 andrew andrew    4 2月   1 13:20 aaa -> name
-rwxrwxr-x 1 andrew andrew 8656 1月  30 14:46 a.out
-rw-rw-r-- 1 andrew andrew 1887 2月   1 13:08 broken_link.sh
-rw-rw-r-- 1 andrew andrew  322 1月  29 13:08 echo_unique.sh
-rw-rw-r-- 1 andrew andrew 1513 1月  29 15:55 escape_charater.sh
-rw-rw-r-- 1 andrew andrew  279 1月  30 13:48 exit_example.sh
-rw-rw-r-- 1 andrew andrew  199 2月   1 11:52 if_else_more.sh
-rw-rw-r-- 1 andrew andrew 1946 1月  30 21:03 if_true.sh
-rw-rw-r-- 1 andrew andrew  337 1月  29 14:02 single_quotation_mark.sh
-rw-rw-r-- 1 andrew andrew  864 2月   1 12:00 test.c
# 调用脚本清除当前文件夹中,丢失链接文件的符号链接
andrew@andrew:/work/bash/src$ bash broken_link.sh ./ | xargs rm
andrew@andrew:/work/bash/src$ ls -l
总用量 44
-rwxrwxr-x 1 andrew andrew 8656 1月  30 14:46 a.out
-rw-rw-r-- 1 andrew andrew 1887 2月   1 13:08 broken_link.sh
-rw-rw-r-- 1 andrew andrew  322 1月  29 13:08 echo_unique.sh
-rw-rw-r-- 1 andrew andrew 1513 1月  29 15:55 escape_charater.sh
-rw-rw-r-- 1 andrew andrew  279 1月  30 13:48 exit_example.sh
-rw-rw-r-- 1 andrew andrew  199 2月   1 11:52 if_else_more.sh
-rw-rw-r-- 1 andrew andrew 1946 1月  30 21:03 if_true.sh
-rw-rw-r-- 1 andrew andrew  337 1月  29 14:02 single_quotation_mark.sh
-rw-rw-r-- 1 andrew andrew  864 2月   1 12:00 test.c
Published 369 original articles · won praise 148 · views 330 000 +

Guess you like

Origin blog.csdn.net/andrewgithub/article/details/104132832