LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-01-2006, 12:36 PM   #1
jfrankman
LQ Newbie
 
Registered: Dec 2005
Posts: 1

Rep: Reputation: 0
How to get a file's symlinks


When I perform a ls on a directory I get the following:

----rwxr-x 1 JFRANKMAN 0 1599570 Apr 22 2006 axis.jar

If I understand this correctly, the "1" after the permissions section and before the ownership section shows that there is one link attached to the axis.jar file. If this is correct how can I find the location of the link that points to the axis.jar file? In other words is there a way to find all links for a given file?
 
Old 11-01-2006, 01:09 PM   #2
uselpa
Senior Member
 
Registered: Oct 2004
Location: Luxemburg
Distribution: Slackware, OS X
Posts: 1,507

Rep: Reputation: 46
This "1" is the number of hardlinks, not symlinks. You need to understand the concepts of inodes, hardlinks and symlinks. Take a look at this for example.
 
Old 11-01-2006, 01:22 PM   #3
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,331
Blog Entries: 55

Rep: Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530
In other words is there a way to find all links for a given file?
Only thing comes up is bruteforce FS traversal. Crude example follows. Finish off by narrowing down search area, apply regex or iname search, or use a grep pipe.
Code:
find /some/dir -type l | while read f; do i=(`stat -c "%N" "${f}"`)
case "${i[2]:1:3}" in ../*) prefix=${f%/*};; esac echo "${f//*\//}: \
`readlink -f "${prefix}/${i[2]:1:$[${#i[2]}-2]}"`"; done
 
Old 11-01-2006, 01:25 PM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
And to answer the question about finding the (hard-)links...
ls -li will give you the files inode, which you then can use
with
find / -inum <number>


Cheers,
Tink
 
Old 11-01-2006, 10:07 PM   #5
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,331
Blog Entries: 55

Rep: Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530
ls -li will give you the files inode, which you then can use with find / -inum <number>
I have a file in /etc with inode number 88216 and at least two relative symlinks, but "find /etc -inum 88216" doesn't show a darn thing?
 
Old 11-01-2006, 10:22 PM   #6
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
That's because symlinks and hard-links aren't the same?


Cheers,
Tink
 
Old 11-02-2006, 04:52 AM   #7
timmeke
Senior Member
 
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515

Rep: Reputation: 61
Indeed. Hardlinks share inodes, symlinks don't use inodes at all. That's why hardlinks cannot cross filesystem boundaries and symlinks can.

In short, think of symlinks as small "text" files that only hold the path to where the symlinks point. If the linked target is gone, then the link points to nothing. The "path" stored can be anywhere, in any filesystem.

When a file is created on disk (ie "touch"), it gets one hardlink in the directory where you created the file. This hardlink is nothing else than an entry in the directory's table of contents that lists both the filename and the inode of the file on disk.
When another hardlink is created (so another entry in some directory's table of contents, with a reference to the same inode/physical file), the inode reference is "shared" between both directories.
When any hardlink is deleted, the number of links (viewable by ls) decreases. If it reaches 0, then the file is "erased" from disk and no longer accessible.
 
Old 11-02-2006, 06:00 AM   #8
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,331
Blog Entries: 55

Rep: Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530
That's because symlinks and hard-links aren't the same?
So yours just isnt the definitive answer, right?

Last edited by unSpawn; 11-02-2006 at 06:04 AM.
 
Old 11-02-2006, 06:01 AM   #9
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670
You can use find to produce a list of symbolic links and targets:
find /path/to/dir -type l -printf "%p\t%l\n"
 
Old 11-02-2006, 06:15 AM   #10
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,331
Blog Entries: 55

Rep: Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530
find /path/to/dir -type l -printf "%p\t%l\n"
Shouldn't that be %h/%l\n so you can do "find /some/dir -type l -printf "%h/%l\n" 2>/dev/null | xargs -iL readlink -f 'L'"? Looks like a less convoluted version of post #3.
 
Old 11-02-2006, 12:45 PM   #11
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Quote:
Originally Posted by unSpawn
That's because symlinks and hard-links aren't the same?
So yours just isnt the definitive answer, right?
It's the answer that relates to the reference count in the
output of ls. He didn't know they aren't symlinks. ;}


Cheers,
Tink
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to clear a file's content? iclinux Linux - Newbie 7 03-30-2009 03:01 PM
how to search file's content with shell? arcow Linux - Software 2 02-08-2006 11:00 AM
Help! tar file's name is truncated auden Linux - Newbie 1 03-02-2005 10:09 PM
file's extension??? amanjsingh Linux - Newbie 2 04-17-2004 10:22 AM
Critical system file's. Tarts Linux - Security 16 10-09-2003 11:06 AM


All times are GMT -5. The time now is 05:10 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration