LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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, 11:36 AM   #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, 12:09 PM   #2
uselpa
Senior Member
 
Registered: Oct 2004
Location: Luxemburg
Distribution: Slackware, OS X
Posts: 1,507

Rep: Reputation: 47
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, 12:22 PM   #3
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
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, 12:25 PM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
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, 09:07 PM   #5
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
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, 09:22 PM   #6
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

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


Cheers,
Tink
 
Old 11-02-2006, 03: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, 05:00 AM   #8
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
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 05:04 AM.
 
Old 11-02-2006, 05:01 AM   #9
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
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, 05:15 AM   #10
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
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, 11:45 AM   #11
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
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



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 02:01 PM
how to search file's content with shell? arcow Linux - Software 2 02-08-2006 10:00 AM
Help! tar file's name is truncated auden Linux - Newbie 1 03-02-2005 09:09 PM
file's extension??? amanjsingh Linux - Newbie 2 04-17-2004 09:22 AM
Critical system file's. Tarts Linux - Security 16 10-09-2003 10:06 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:46 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
Open Source Consulting | Domain Registration