LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   How to find symlink? (https://www.linuxquestions.org/questions/linux-software-2/how-to-find-symlink-565475/)

kaz2100 06-29-2007 05:19 PM

How to find symlink?
 
Hya,

What is the easiest and fastest way to find symlink(s)?

I know that
Code:

find / -lname linktarget
works. But it takes longer than usual search, especially when file system is huge.

Is there any better way?

Happy Penguins!

Electro 06-29-2007 05:44 PM

If using ext3, enable its b-tree feature. It should speed up searching. Another way is contribute to slocate to include an option to search by file and directory types. The find utility is the normal search tool. slocate is a database search tool that needs to be updated when files or directories are created or moved.

pixellany 06-29-2007 05:49 PM

ls -lR <dirname>|grep "^l"

I can't immediately figure out how to make this show the directory in which it finds each match.

dr34m3r 06-30-2007 02:15 AM

ls -lR <dirname> |grep "^l\|:$"

prints the directory name first and then symlinks in the directory

pixellany 06-30-2007 09:07 AM

Quote:

Originally Posted by dr34m3r
ls -lR <dirname> |grep "^l\|:$"

prints the directory name first and then symlinks in the directory

I have never seen syntax like this before---I assume that "|" is escaped so it won't act as a pipe, but then what does it mean? (Alternation operator?)

backreference!!--a little bell is ringing---is this a backreference?

Note that this prints ALL the directories in the search path, plus the links it find in each one. I guess you could then write more code to strip out the one's which contain no links......

dr34m3r 06-30-2007 07:14 PM

Quote:

Originally Posted by pixellany
I have never seen syntax like this before---I assume that "|" is escaped so it won't act as a pipe, but then what does it mean? (Alternation operator?)

backreference!!--a little bell is ringing---is this a backreference?

I dont think this is not a backreference. It just a simple OR in bash (although I may be wrong!!)

Quote:

Originally Posted by pixellany
Note that this prints ALL the directories in the search path, plus the links it find in each one. I guess you could then write more code to strip out the one's which contain no links......

this bash script should do the job perfectly. the script takes the search directory as agrument. Tell me what you think. I know this can be made better, but this is what I could think off top of my head.

Code:

#!/bin/bash
IFS=$'\n'
prevLine=
printDir=

for line in `ls -lR $1 |grep "^l\|:$" ` ; do
        if [ -z $prevLine ] ; then
                prevLine=$line
        else
                if [ "${prevLine:$((1-2))}" == ":" ] && [ "${line:0:1}" == "l" ]; then
                        if [ -z $printDir ]; then
                                echo $prevLine
                                printDir=printed
                        fi
                        echo $line
                else
                        prevLine=$line
                        printDir=
                fi

        fi
done


kaz2100 07-02-2007 06:40 AM

Hya,

Um... I guess life is not easy.

Quote:

I dont think this is not a backreference. It just a simple OR in bash (although I may be wrong!!)
I guess is is OR in gnu grep, (although I may be wrong!)

Happy Penguins!

pixellany 07-02-2007 07:46 AM

Quote:

Originally Posted by kaz2100
Hya,

Um... I guess life is not easy.

I guess is is OR in gnu grep, (although I may be wrong!)

Happy Penguins!

You guess right---they actually call it the "alternation operator". I was being dense in that post....the code was obvious once I thought about it. (":$" means ":" at the end of the line. "|" is escaped because it is parentheses to keep bash from seeing it as a pipe--except that the quotes mean "read literally"--hence the escape to give special meaning to "|" (but a different special meaning than if it had be left unquoted...) )
Whew!!!---just one example of why shell scripting takes a few days to learn....;)

kaz2100 07-02-2007 12:57 PM

Quote:

Originally Posted by pixellany
Raise your hand if you: A) had a vacuum tube collection in HS, B) know what IS a vacuum tube, C) know what is (was) magnetic core memory, D) know what is(was) an IBM keypunch, or E) know how to build radios with acid-core solder

I do not know how to raise hand here, however
A) No.
B) No.
C) Yes, it used to be state-of-art, space occupying heavy industry product. Several KB (kilo bit??) range.
D) Yes, it was Important Bunch of Machine for key-puncher. One card (boarding pass size) per one line. No backspace, delete keys.
E) Yes. Some of these radios work without external battery. Coil, variable condenser, antenna, ear piece and crystal. (http://en.wikipedia.org/wiki/Crystal_radio)

Happy Penguins!

kaz2100 07-02-2007 01:07 PM

By the way,

Such a radio does not use any "semi-conductor" at all. Only conductor, inductor, capacitor and reactor. Not even resistor.

Happy Penguins!

OldManRiver 09-25-2014 12:41 PM

Better?
 
Quote:

Originally Posted by dr34m3r (Post 2806259)
this bash script should do the job perfectly. the script takes the search directory as agrument. Tell me what you think. I know this can be made better, but this is what I could think off top of my head.

Code:

#!/bin/bash
IFS=$'\n'
prevLine=
printDir=

for line in `ls -lR $1 |grep "^l\|:$" ` ; do
        if [ -z $prevLine ] ; then
                prevLine=$line
        else
                if [ "${prevLine:$((1-2))}" == ":" ] && [ "${line:0:1}" == "l" ]; then
                        if [ -z $printDir ]; then
                                echo $prevLine
                                printDir=printed
                        fi
                        echo $line
                else
                        prevLine=$line
                        printDir=
                fi

        fi
done



Doesn't the command:

`find -xtype l -exec ls -l {} \;`

work better than:

`ls -lR $1 |grep "^l\|:$"`

I think it processes much less junk!

Cheers!

OMR


All times are GMT -5. The time now is 07:50 AM.