• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 890
  • Last Modified:

Shell Script to remove all symbolic links (symlinks) to non existent files and directories

Dear Experts (and Farzanj),

I need to write a script, which removes all symbolic links (symlinks) which point toward non existent objects such as: files and directories, in a directory [given as the argument].

Using the script, the directory should be given as the script argument:
$ script-name directory-name

The person who provides the correct solution will be awarded respectably.

Kind regards, SME_Paddington

#!/bin/bash

echo "The directory in which symlinks are to be removed:"
echo $@

Open in new window

0
SME_Paddington
Asked:
SME_Paddington
5 Solutions
 
woolmilkporcCommented:
For testing:

find $1 -type l -ls | awk '{print $(NF-2) , $NF}' | while read link target
   do [[ ! -e $target ]] && echo Link $link to non-existent Target $target needs to be removed; done

To actually remove the dead links:

find $1 -type l -ls | awk '{print $(NF-2) , $NF}' | while read link target
   do [[ ! -e $target ]] && rm $link; done

wmp
0
 
simon3270Commented:
One problem with the above script is that if the target file is a relative path, it is relative to the directory containing the symbolic link.  For example, if fred is a file in dir1, and george is a symbolic link, also in dir1, pointing to fred, the "-ls" in find gives:

  27820128  0 lrwxrwxrwx 1 simon users  4 May 11 09:51 ./dir1/george -> fred

You would need to cd to "dirname $link" before testing for the existence of fred.
0
 
SME_PaddingtonAuthor Commented:
Dear Experts,

I do not mind if I need to be in the directory, containing the target directory when it needs to be run.

So for example:
$ ls $PWD
script.sh
Documents
Downloads
$ ./script.sh Documents

If this is not what you are referring to, could you please kindly explain it to me.

Kind Regards
0
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 
simon3270Commented:
The problem was if Documents here contains a subdirectory, then symbolic links in that subdirectory might be handled incorrectly.  A fixed script is:

find $1 -type l -ls | awk '{print $(NF-2) , $NF}' | while read link target; do
   if (cd $(dirname $link) ; test ! -e $target); then
     echo $link points to non-existent $target
   fi
done

Open in new window

Just replace the echo with a rm command, as wmp did.

We've assumed that none of your file names contain spaces - is this true?
0
 
TintinCommented:
Much easier to use the -L option with find.

Note I've commented out the 'rm', so you can run it first and verify the results.
#!/bin/bash

echo "The directory in which symlinks are to be removed:"
echo $1

for file in $(find -L $1 -type l)
do
  echo "$file is a dead symbolic link"
  #rm -f $file
done

Open in new window

0
 
simon3270Commented:
Neat, Tintin, but the "find" in RedHat, for example, doesn't support -L.

It also still has a problem with filenames with spaces (which a "Document" directory is likely to have).  Easiest might be to move it around:
find -L $1 -type l | while read file
do
  echo "$file is a dead symbolic link"
  #rm -f "$file"
done

Open in new window

0
 
farzanjCommented:
Hi there,

If  your find command version is new enough you can delete all bad symlinks with a single command

 
find $1 -type l ! -execdir test -e {} \; -delete

Open in new window


where $1 is the directory path

Likewise you can simply print bad links
 
find . -type l ! -execdir test -e {} \; -print

Open in new window


If you have an old version of find, it the above commands would not work.  Let me know.
0
 
SME_PaddingtonAuthor Commented:
Dear Experts,

I am very impressed. Sorry I was not able to reply instantaneously - I was at work, and we were having to perform remote batch installs using ftp scripts - as far as this thread is concerned there are multiple acceptable solutions. As you will see from the results below, there are two different kinds of results, although I am not sure which is the entirely [100%] correct result, I am planning on allocating a spread of points to all of you, because nearly all solutions were valid.

A 5 way split has been performed in the interests of fairness.
Thank you very much guys! I will be posting another question today, and hope you can help.
Each result below is the respective posting above, so top to bottom.

Kind regards, SME_Paddington.
G1:~$ ./ftest.sh
The directory in which symlinks are to be removed:

./.pulse/2c8ccce2cef5ef0a3939b5eb00000017-runtime points to non-existent /tmp/pulse-pv2tPhMUJSTb


G1:~$ pico ftest.sh
G1:~$ ./ftest.sh
The directory in which symlinks are to be removed:

./.pulse/2c8ccce2cef5ef0a3939b5eb00000017-runtime is a dead symbolic link
./.Private/ECRYPTFS_FNEK_ENCRYPTED.FWYHkmMYQFEeBURTAIdjbi1muFEu9YvycVrR2KvXg-t3PTfIbHLyI8dWTk--/ECRYPTFS_FNEK_ENCRYPTED.FYYHkmMYQFEeBURTAIdjbi1muFEu9YvycVrRidd7O5PVRPlUzd6gV1Y5d4Hp0N9PApXxKCsbBZMPeMr5AwpB3s63UojZWE40CIAM is a dead symbolic link
./.Private/ECRYPTFS_FNEK_ENCRYPTED.FWYHkmMYQFEeBURTAIdjbi1muFEu9YvycVrRvsKBmD3QbjclzVPMVO1tHU-- is a dead symbolic link
./.Private/ECRYPTFS_FNEK_ENCRYPTED.FWYHkmMYQFEeBURTAIdjbi1muFEu9YvycVrRFfjiOPJuNNwlwS6tc98vF--- is a dead symbolic link
G1:~$ ./ftest.sh Documents
The directory in which symlinks are to be removed:
Documents

G1:~$ pico ftest.sh
G1:~$ ./ftest.sh
The directory in which symlinks are to be removed:

./.pulse/2c8ccce2cef5ef0a3939b5eb00000017-runtime is a dead symbolic link
./.Private/ECRYPTFS_FNEK_ENCRYPTED.FWYHkmMYQFEeBURTAIdjbi1muFEu9YvycVrR2KvXg-t3PTfIbHLyI8dWTk--/ECRYPTFS_FNEK_ENCRYPTED.FYYHkmMYQFEeBURTAIdjbi1muFEu9YvycVrRidd7O5PVRPlUzd6gV1Y5d4Hp0N9PApXxKCsbBZMPeMr5AwpB3s63UojZWE40CIAM is a dead symbolic link
./.Private/ECRYPTFS_FNEK_ENCRYPTED.FWYHkmMYQFEeBURTAIdjbi1muFEu9YvycVrRvsKBmD3QbjclzVPMVO1tHU-- is a dead symbolic link
./.Private/ECRYPTFS_FNEK_ENCRYPTED.FWYHkmMYQFEeBURTAIdjbi1muFEu9YvycVrRFfjiOPJuNNwlwS6tc98vF--- is a dead symbolic link
G1:~$ pico ftest.sh
G1:~$ ./ftest.sh
The directory in which symlinks are to be removed:

Link ./.pulse/2c8ccce2cef5ef0a3939b5eb00000017-runtime to non-existent Target /tmp/pulse-pv2tPhMUJSTb needs to be removed


G1:~$ pico ftest.sh
G1:~$ ./ftest.sh
The directory in which symlinks are to be removed:

./.pulse/2c8ccce2cef5ef0a3939b5eb00000017-runtime is a dead symbolic link
./.Private/ECRYPTFS_FNEK_ENCRYPTED.FWYHkmMYQFEeBURTAIdjbi1muFEu9YvycVrR2KvXg-t3PTfIbHLyI8dWTk--/ECRYPTFS_FNEK_ENCRYPTED.FYYHkmMYQFEeBURTAIdjbi1muFEu9YvycVrRidd7O5PVRPlUzd6gV1Y5d4Hp0N9PApXxKCsbBZMPeMr5AwpB3s63UojZWE40CIAM is a dead symbolic link
./.Private/ECRYPTFS_FNEK_ENCRYPTED.FWYHkmMYQFEeBURTAIdjbi1muFEu9YvycVrRvsKBmD3QbjclzVPMVO1tHU-- is a dead symbolic link
./.Private/ECRYPTFS_FNEK_ENCRYPTED.FWYHkmMYQFEeBURTAIdjbi1muFEu9YvycVrRFfjiOPJuNNwlwS6tc98vF--- is a dead symbolic link


G1:~$ find . -type l ! -execdir test -e {} \; -print
./.pulse/2c8ccce2cef5ef0a3939b5eb00000017-runtime
G1:~$

Open in new window

0
 
SME_PaddingtonAuthor Commented:
Excellent guys, I am impressed and amazed at your collective wealth of knowledge.

Tintin's solution has been graded as the best, due to its usability and the fact that I personally am not using Red Hat.

(Ubuntu Linux 10.4)

Simon your style of code from what I can tell was based upon Tintin's although, in terms of portability - excellent. As I do not have Red Hat systems on my home corporate network, I cannot test it, but excellent idea, and it is the best, in terms of portability if I was using Red Hat.

If I could choose two best solutions, I would. But as I cannot, the award goes to Tintin.

Warmest regards, and thank you all very much.
I will be posting another question today.
0

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now