Using script to automatically clear the symbolic links of missing link files in the specified folder

Time:2020-9-22

Using script to automatically clear the symbolic links of missing link files in the specified folder

The script can clear the symbolic link of the view link file under the specified folder.

Often you need to set up a link to the corresponding file folder in Linux, or you need to set up a link to the corresponding file on the desktop, so that you can often access the file in the corresponding file folder.

In this way, it is much more convenient to visit, but after using up, many symbolic links are left. These links need to be manually confirmed by the user to confirm whether they can be deleted, which brings a lot of inconvenience to the use of Linux. You can use this script to free your hands. You only need to execute the script to clear the unused symbolic links in the corresponding folder.

#!/bin/bash
#A file that can test broken links and output the file they point to
#So that they can provide the output to xargs for processing:)
#For example, broken- link.sh  /somedir /someotherdir|xargs rm
#
#The method below, however, is a better one:
#
#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 in the / proc file system and any dead loop links!
##############################################################
#If no parameters are passed into the script, use the
#The current directory. Otherwise, the parameter passed in is used as the directory
#To search
####################
[ $# -eq 0 ] && directorys=`pwd` || [email protected]

#Write the function linkchk to check whether the directory or file passed in is a link,
#And determine whether these files or directories exist. Then print the files they point to
#If the element passed in contains subdirectories,
#Then put the subdirectory into the linkcheck function, so as to achieve 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
}
#Each parameter passed to the script is sent to the linkchk function for processing,
#Check if a directory is available. If not, print the error message and
#Use 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 name
[email protected]:/work/bash/src$ touch name
#Create a symbolic link for name
[email protected]:/work/bash/src$ ln -s name aaa
#Delete the name file, AAA will become a symbolic link of the missing link file
[email protected]:/work/bash/src$ rm name
#View AAA to execute the symbolic link file of name in the current directory
[email protected]:/work/bash/src$ ls -l
Total dosage 44
Lrwxrwxrwx 1 Andrew Andrew 4 Feb 1 13:20 AAA > name
-Rwxrwxr-x 1 Andrew Andrew 8656 Jan 30 14:46 a.out
-Rw-rw-r -- 1 Andrew Andrew 1887 February 1 13:08 broken_ link.sh
-Rw-rw-r -- 1 Andrew Andrew 322 January 29 13:08 echo_ unique.sh
-Rw-rw-r -- 1 Andrew Andrew 1513 January 29 15:55 escape_ charater.sh
-Rw-rw-r -- 1 Andrew Andrew 279 January 30 13:48 exit_ example.sh
-Rw-rw-r -- 1 Andrew Andrew 199 February 11:52 if_ else_ more.sh
-Rw-rw-r -- 1 Andrew Andrew 1946 January 30 21:03 if_ true.sh
-Rw-rw-r -- 1 Andrew Andrew 337 January 29 14:02 single_ quotation_ mark.sh
-Rw-rw-r -- 1 Andrew Andrew 864 February 1 12:00 test. C
#Call the script to clear the symbolic link of the linked file in the current folder
[email protected]:/work/bash/src$ bash broken_link.sh ./ | xargs rm
[email protected]:/work/bash/src$ ls -l
Total dosage 44
-Rwxrwxr-x 1 Andrew Andrew 8656 Jan 30 14:46 a.out
-Rw-rw-r -- 1 Andrew Andrew 1887 February 1 13:08 broken_ link.sh
-Rw-rw-r -- 1 Andrew Andrew 322 January 29 13:08 echo_ unique.sh
-Rw-rw-r -- 1 Andrew Andrew 1513 January 29 15:55 escape_ charater.sh
-Rw-rw-r -- 1 Andrew Andrew 279 January 30 13:48 exit_ example.sh
-Rw-rw-r -- 1 Andrew Andrew 199 February 11:52 if_ else_ more.sh
-Rw-rw-r -- 1 Andrew Andrew 1946 January 30 21:03 if_ true.sh
-Rw-rw-r -- 1 Andrew Andrew 337 January 29 14:02 single_ quotation_ mark.sh
-Rw-rw-r -- 1 Andrew Andrew 864 February 1 12:00 test. C

summary

The above is a small series to introduce the use of scripts to automatically clear the missing link files under the specified folder symbolic links, I hope to help you!

Recommended Today

PHP 12th week function learning record

sha1() effect sha1()Function to evaluate the value of a stringSHA-1Hash. usage sha1(string,raw) case <?php $str = “Hello”; echo sha1($str); ?> result f7ff9e8b7bb2e09b70935a5d785e0cc5d9d0abf0 sha1_file() effect sha1_file()Function calculation fileSHA-1Hash. usage sha1_file(file,raw) case <?php $filename = “test.txt”; $sha1file = sha1_file($filename); echo $sha1file; ?> result aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d similar_text() effect similar_text()Function to calculate the similarity between two strings. usage similar_text(string1,string2,percent) case […]