SoFunction
Updated on 2025-04-14

Use scripts to automatically clear symbolic links of missing link files in specified folders

Use scripts to automatically clear symbolic links of missing link files in specified folders

The script can be cleared and viewed the symbolic links of the link file under the specified folder.

When using Linux, you often create symbolic links for many files or programs, so you don’t have to go to the corresponding folder every time to find the corresponding files, but just create symbolic links for files that you need to access frequently. This way, you can put the files you often need to access on the desktop or specify them to another folder.

This way, it is much more convenient to access, but after use, many symbolic links are often left behind. These links require users to manually confirm whether they can be deleted, which brings a lot of inconvenience to the use of Linux. Using this script can free your hands. Just execute the script to clear useless symbolic links in the corresponding folder.

#!/bin/bash
# A file that can test the symbolic links that have been broken and can output the files they point to# So that they can provide output to xargs for processing :)# For example. /somedir /someotherdir|xargs rm#
#The following method, no matter what, is a better method:#
#find "somedir" -type l -print0|\
#xargs -r0 file|\
#grep "broken symbolic"|
#sed -e 's/^\|: *broken symbolic.*$/"/g'
#
#But this is not a pure bash script, at least not now.#Note: Beware of using it in the /proc file system and any dead loop links!##############################################################
#If no parameters are passed into the script, then use#Current directory. Otherwise, the passed parameters are used as the directory#Come search.####################
[ $# -eq 0 ] && directorys=`pwd` || directorys=$@

# Write the function linkchk to check whether the passed directory or file is a link.# and determine whether these files or directories exist. Then print the file they point to.#If the passed in element contains a subdirectory,#Then put the subdirectories into the linkcheck function to process, which achieves the purpose of recursion.##########
linkchk () {
  for element in $1/*; do
    [ -h "$element" -a ! -e "$element" ] && echo \"$element\"
    [ -d "$element" ] && linkchk $element
    # Of course, '-h' is used to test symbolic links, and '-d' is used to test directories.  done
}
#Send each parameter passed to the script to the linkchk function for processing,# Check if there are any directories available. If not, print the error message and#User information.################
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
# Create a new file nameandrew@andrew:/work/bash/src$ touch name
# Create symbolic link for nameandrew@andrew:/work/bash/src$ ln -s name aaa
# Delete the name file, aaa will become a symbolic link to the missing link fileandrew@andrew:/work/bash/src$ rm name
# View the symbolic link file of aaa that executes the name in the current directoryandrew@andrew:/work/bash/src$ ls -l
Total dosage 44
lrwxrwxrwx 1 andrew andrew  4 2moon  1 13:20 aaa -> name
-rwxrwxr-x 1 andrew andrew 8656 1moon 30 14:46 
-rw-rw-r-- 1 andrew andrew 1887 2moon  1 13:08 broken_link.sh
-rw-rw-r-- 1 andrew andrew 322 1moon 29 13:08 echo_unique.sh
-rw-rw-r-- 1 andrew andrew 1513 1moon 29 15:55 escape_charater.sh
-rw-rw-r-- 1 andrew andrew 279 1moon 30 13:48 exit_example.sh
-rw-rw-r-- 1 andrew andrew 199 2moon  1 11:52 if_else_more.sh
-rw-rw-r-- 1 andrew andrew 1946 1moon 30 21:03 if_true.sh
-rw-rw-r-- 1 andrew andrew 337 1moon 29 14:02 single_quotation_mark.sh
-rw-rw-r-- 1 andrew andrew 864 2moon  1 12:00 
# Call the script to clear the symbolic link of the link file in the current folderandrew@andrew:/work/bash/src$ bash broken_link.sh ./ | xargs rm
andrew@andrew:/work/bash/src$ ls -l
Total dosage 44
-rwxrwxr-x 1 andrew andrew 8656 1moon 30 14:46 
-rw-rw-r-- 1 andrew andrew 1887 2moon  1 13:08 broken_link.sh
-rw-rw-r-- 1 andrew andrew 322 1moon 29 13:08 echo_unique.sh
-rw-rw-r-- 1 andrew andrew 1513 1moon 29 15:55 escape_charater.sh
-rw-rw-r-- 1 andrew andrew 279 1moon 30 13:48 exit_example.sh
-rw-rw-r-- 1 andrew andrew 199 2moon  1 11:52 if_else_more.sh
-rw-rw-r-- 1 andrew andrew 1946 1moon 30 21:03 if_true.sh
-rw-rw-r-- 1 andrew andrew 337 1moon 29 14:02 single_quotation_mark.sh
-rw-rw-r-- 1 andrew andrew 864 2moon  1 12:00 

Summarize

The above is the editor’s introduction to the use of scripts to automatically clear the symbolic links of missing link files in the specified folder. I hope it will be helpful to everyone!