Operating System - HP-UX
1833783 Members
2272 Online
110063 Solutions
New Discussion

Re: ls without broken links

 
SOLVED
Go to solution
Allanm
Super Advisor

ls without broken links


How can I do a ls output minus the broken links in the list.

Thanks.

4 REPLIES 4
James R. Ferguson
Acclaimed Contributor

Re: ls without broken links

Hi:

# perl -MCwd=realpath -MFile::Find -le 'find sub{print $File::Find::name," -> ",realpath($_) if -l && -e},@ARGV' /path

...will show only symbolic links whose targets are valid. You may pass one or more '/path' arguments as you see fit.

Regards!

...JRF...
Allanm
Super Advisor

Re: ls without broken links

Thanks for replying JRF!!, what I am looking at is just a regular ls output without the broken links at the present directory level.

Thanks,
Allan.
James R. Ferguson
Acclaimed Contributor

Re: ls without broken links

Hi (agtain) Allan:

OK, try this:

# cat .notbroken
#!/usr/bin/sh
typeset MYPATH=$1
ls -l ${MYPATH}|grep "^l"|awk '{print $(NF-2)}' | \
while read LINK
do
[ -e ${LINK} ] && ls -ld ${LINK}
done
exit

...run as:

# ./notbroken /path

Regards!

...JRF...
Dennis Handly
Acclaimed Contributor
Solution

Re: ls without broken links

You can use "ll -L" and exclude the bad files:
ll -L 2> /dev/null | fgrep -v -e ----------

The grep -v is needed for 11.11 and the stderr redirect is needed for 11.23 and 11.31.