Link to home
Start Free TrialLog in
Avatar of SME_Paddington
SME_PaddingtonFlag for United Kingdom of Great Britain and Northern Ireland

asked on

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

SOLUTION
Avatar of woolmilkporc
woolmilkporc
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
Avatar of SME_Paddington

ASKER

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
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

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.