Shell Command for Symbolic Link Targets


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Shell Command for Symbolic Link Targets
# 1  
Old 02-20-2008
Shell Command for Symbolic Link Targets

**DUP post in Shell Programming and Scripting ***
Hello,

I'm still learning the ins and outs of how to combine multiple Unix commands together (specifically AIX), but I'm looking to do the following (only on local files):

1. Find all symbolic links that are not set up using absolute pathnames
2. Find all symbolic links that are broken (ie, target does not exist).

My original idea was to find all symbolic links, pipe it to a file and then read the file, but we'd like to do it as a single command without creating new files.

I've only been able to come up with the following (which I believe shows all the local symbolic links):
find / -type l \( -fstype jfs -o -fstype jfs2 \)

Thanks for any and all help.

Ben

edit: I think I may have found the answer to #2, is it correct?
find / -type l | (while read FN ; do test -e "$FN" || ls -l "$FN"; done)

Last edited by bdizenhouse; 02-21-2008 at 10:12 AM..
# 2  
Old 02-21-2008
I have now been asked to also provide a count of the results.

So I think I found the following solutions:
#2
find / -type l | (while read FN ; do test -e "$FN" || ls -l "$FN"; done)
and
find / -type l | (while read FN ; do test -e "$FN" || ls -l "$FN"; done) | grep -c '[a-z]'

#1
find / -type l | (while read FN ; do test -e "$FN" | file -h "$FN" | grep 'symbolic link to [^/]' ; done)
and
find / -type l | (while read FN ; do test -e "$FN" | file -h "$FN" | grep 'symbolic link to [^/]' ; done) | grep -c 'symbolic link to [^/]'

Are these correct? I want to make sure I'm only looking at local filesystems only. Also how can I suppress the messages like "ls: 0653-341 The file xxx does not exist." or "find: 0652-019 The status on xxx is not valid."


Thanks for any and all help.

Ben
# 3  
Old 02-21-2008
I will include some more general aspects in my reply:

Quote:
Originally Posted by bdizenhouse
I have now been asked to also provide a count of the results.
You have line-oriented output, yes? "wc -l" counts lines ...

Quote:
I want to make sure I'm only looking at local filesystems only.
You chose a way of iteratively selecting all the local ones: fstype=jfs, fstype=jfs2, ... . This is possible but such problems can be solved from both sides, though: either by including everything you need or by excluding everything you do not need. "All local filesystems" is the same as "all non-remote filesystems", yes?:

find ....... ! -fstype nfs

Means "all filesystems except nfs", which should be what you want. This would include not only jfs and jfs2 but also some more obscure FS types, which also exist.

Quote:
Also how can I suppress the messages like "ls: 0653-341 The file xxx does not exist." or "find: 0652-019 The status on xxx is not valid.
This comes because you issue "find" under your user id and your user is probably not allowed to access everything. This would probably be different if you issue the command as root user. Anyways, the error messages go to a separate I/O channel in UNIX (to <stderr>), which only per default points to the same output device as <stdout>. You can safely redirect the error messages (and only them) with putting your valuable output in jeopardy:

find ..... 2> /dev/null

redirects the outcome of the <stderr> file descriptor to /dev/null, the big bit bucket instead of /dev/yourpseudoterminal, where it clutters your screen.

I hope this helps and, btw.: we have a great and very active AIX special interest group here in the forum, which i by now would like to shamelessly advertise. I am looking forward to see you there.

bakunin
# 4  
Old 02-21-2008
Shell Commands for Symbolic Links

Thank you very much for the indepth solution.

I'm still very new to the Unix world, and where I am, we mainly deal with AIX, so I will definately check out the forums here.

So I've come to the conclusion to use the following 2 commands:

(find all non-absolute symbolic links)
find / -type l ! -fstype nfs 2> /dev/null | (while read FN ; do test -e "$FN" | file -h "$FN" | grep 'symbolic link to [^/]' ; done) | wc -l

(final all broken symbolic links)
find / -type l ! -fstype nfs 2> /dev/null | (while read FN ; do test -e "$FN" || ls -l "$FN" 2> /dev/null; done) | wc -l

Thanks very much for the help!
ben
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Symbolic link

Hi, trying to understand more about symblic link, when I compiled a program called "match" in one folder ~/downloadsoftware/I want this program to be accessible like a system command by putting a symbolic link in /usr/bin/ Not by setting the $PATH method in .bashrc at this time. What I did is:... (4 Replies)
Discussion started by: yifangt
4 Replies

2. UNIX for Dummies Questions & Answers

[Solved] Symbolic link not allowed or link target not accessible

Hi everybody, I read about treads realted to this issue but they did not resovle issue given below. Please help me resolve issue given below I have html file under /srv/www/htdocs/actual_folder ls actual_folder/ test.html and following link works... (0 Replies)
Discussion started by: newbielgn
0 Replies

3. Shell Programming and Scripting

ls on a symbolic link

Hi all, Can anyone please confirm if the command below is the only way that I can get what the symbolic link is set to? mnlxd110(oracle)/db/posd2/dba$: ls -l | grep "^l" lrwxrwxrwx 1 oracle dba 28 Aug 9 2011 bdump -> diag/rdbms/posp1/posp1/trace mnlxd110(oracle)/db/posd2/dba$:... (7 Replies)
Discussion started by: newbie_01
7 Replies

4. Shell Programming and Scripting

Symbolic Link Help

Hello All, This may be a silly question to some but I am really stuck. Is there a way to reverse the following; sudo rm /bin/sh sudo ln -s /bin/bash /bin/sh It was part of a driver compile/installation procedure by Digi for Ubuntu stating that dash isn't supported and a symbolic link... (2 Replies)
Discussion started by: LAVco
2 Replies

5. Shell Programming and Scripting

symbolic link

Hi, i am trying to create sym links on sles 11 , but it seems i am doing something wrong. oracle@tests:/u01/app/oracle/oradata/ACIS> pwd /u01/app/oracle/oradata/ACIS oracle@tests:/u01/app/oracle/oradata/ACIS> ln -s /db/ACIS/dbase/dbf/ /u01/app/oracle/oradata/ACIS/... (2 Replies)
Discussion started by: tonijel
2 Replies

6. Solaris

Testing targets of link files > ln -s

Hi all Ive been given the task to write a script that tests that certain link files work, i.e. the physical directory the link points too, is actually there. Now, before I go down the route of ls -l | awk ...... or using test or find, is there a far more simpler command that I can use ? ... (2 Replies)
Discussion started by: sbk1972
2 Replies

7. UNIX for Dummies Questions & Answers

how does symbolic link execute command with option or parameter

Hello World~:) $ rbash $ cd .. rbash: cd: restricted $ ls -l /bin/rbash lrwxrwxrwx 1 root root 4 2008-11-02 15:56 /bin/rbash -> bash rbash is a symbolic link to bash but why does rbash execute 'bash -r' instead of 'bash' i want to know how symbolic link executes command with option or... (4 Replies)
Discussion started by: lifegeek
4 Replies

8. Shell Programming and Scripting

Shell script -- symbolic link

Hi , pwd -P will give the physical directory name for symbolic names. is there any command to get the value of symbolic link ? if it's symbolic link , I have to get the physical directory name? any ideas to code in shell ? Thanks (3 Replies)
Discussion started by: talashil
3 Replies

9. UNIX for Dummies Questions & Answers

need some info about symbolic link and hard link

hello folks how y'all doin well i have some questions about symbolic link and hard link hope some one answer me i open terminal and join as root and i wrote ln -s blah blah then i wrote ls i see red file called blah blah but didn't understand what is this can some one explain and... (2 Replies)
Discussion started by: detective linux
2 Replies

10. UNIX for Dummies Questions & Answers

symbolic Link

question abt symbolic link ... i'm doing the following ... ln -s x.sh ./scripts/y.sh and cat ./scripts/y.sh it is giving following error cat: cannot open y.sh Any reason u an think of ? But it is working fine when i goto scripts directory and cretae the symbolic link. ... (1 Reply)
Discussion started by: bhargav
1 Replies
Login or Register to Ask a Question