1831483 Members
3350 Online
110025 Solutions
New Discussion

Need script help

 
SOLVED
Go to solution
Roy Schauwecker
Occasional Advisor

Need script help

I know what I want to do but not sure how to make it happen. Trying to write a script that will identify bad links. I'm sure there is some simple solution. Yes, quite a novice in this realm. This is actually my first post.

Step 1)
find /fntst2 -type l -exec ll -d {} \; >/tmplnk
(this locates my link and puts them out to a temporary file)

Step 2) I want to search this file and print out all of the lines that DO NOT match my search criteria...the bad links. How do I search this temporary file for multiple strings? i.e. - vgx001, vgx002, vgx003 and then print out the lines that do not match.

I know I could do a grep -v vgx001 file but that would pull up my valid links as well. Any help is greatly appreciated.
11 REPLIES 11
John Palmer
Honored Contributor

Re: Need script help

If you're looking for specific names then you could use grep...

find /fntst2 -type l -exec ll -d {} \; | grep -e name1 -e name2 -e name3

Or you could use file instead of ll to identify links that point nowhere...

find /fntst -type l -exec file {} \; | grep "cannot open"

Regards,
John
Pete Randall
Outstanding Contributor

Re: Need script help

Hi,

grep -vE 'vgx001|vgx002|vgx003' file

should print out the non-matching lines for you.


Pete

Pete
Roy Schauwecker
Occasional Advisor

Re: Need script help

Thanks. I'll use your idea of searching for the links that point to nothing. I'm actually interested in identifying links that are pointing to the wrong location...a volume group that they should not be pointing to. We have had some issues with this lately. What I want to do is plug in the valid volume groups and any line that doesn't match my list would be printed out to the teminal.
Pete Randall
Outstanding Contributor
Solution

Re: Need script help

Roy,

Really!! Try the grep -v!!! That's exactly what it does!!!! Print out the non-matches!!!!!

Trust me!

Pete

Pete
Roy Schauwecker
Occasional Advisor

Re: Need script help

Excuse my ignorance. The grep -e will actually exclude the lines that contain the search strings and print out the lines that are not a match?
Pete Randall
Outstanding Contributor

Re: Need script help

No, it's the grep -v that prints the non-matching lines, the E just allows the extended regular expression syntax. It's like what John was doing with multiple -e operands.

But it's -v you want - that will print the non match. From man grep:

" -v All lines but those matching are printed."


Pete

Pete
Roy Schauwecker
Occasional Advisor

Re: Need script help

Thanks to John and Pete you have both been a great help. I'm new to this site and these forums. What a great source of information.

Im using John's suggestion to locate bad links and Pete's suggestion to locate improperly allocated links.
Michael Steele_2
Honored Contributor

Re: Need script help

find /fmtst2 -type -l -print | perl -nle '-e || print' | while read a
do
rm $a
done

(* Can't test right now. :-( *)
Support Fatherhood - Stop Family Law
Michael Steele_2
Honored Contributor

Re: Need script help

Note: This will remove bad symbollic links: (* I believe *)

find /fmtst2 -type -l -print | perl -nle '-e || print' | while read a
do
rm $a
done

Note: This will just find them. (* TESTED *)

find /fmtst2 -type -l -print | perl -nle '-e || print' > /tmp/file
Support Fatherhood - Stop Family Law
Jordan Bean
Honored Contributor

Re: Need script help


#!/usr/bin/sh
# search and destroy broken
# symlinks in current dir.
find . -type l -xdev | while read link junk
do
[ -e "$link" ] || rm $link
done

Jordan Bean
Honored Contributor

Re: Need script help

Probably more efficient to do this if you have hundreds of links to remove.

#!/usr/bin/sh
# search and destroy broken
# symlinks in current dir.
find . -type l -xdev | while read link
do
[ -e "$link" ] || echo \"$link\"
done | xargs rm


The quotes should account for any whitespaces in the file names.