Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to determine if a given file has been linked?

Status
Not open for further replies.
Apr 22, 2002
48
US
Is there a way to determine if a given file has any links to it - symbolic or otherwise? If so, what are they?

I know the hard links show up in the count in "ls -l", but that too works only for hard links and doesn't tell where is the link.

Any help is appreciated.
 
Hi,
ls -L file

That is a CAPITAL L.

will tell you the information about the file the S0FT LINKED is pointing to, but you bring up an interesting point.

How do you find the other 'n' files which are HARD LINKED to each other?

I think doing an ls -iR / ( I think -i gives INODE numbers ) and sorting by INODE and finding duplicate INODE numbers ( meaning those are the same files ) is a bit extreme.

 
in "ls -l" output, the second column (numeric, just after the permissions), lists the number of hard links for the given file.

right there under your nose =)

it gets whacky when directories are involved, since subdirs of a given directory link back to the parent directory (my /home has over 100 hard links for example)
 
Thanks guys. I wasn't expecting so much activity over New Year's Eve.

MARSD - find only will find the files that are links. I was trying to find the target (links) starting from the source (files).

TDATGOD - ls -L indeed lists the file instead of the symbolic link but without the path info. Not of much use. AS for hard links, I had same thoughts about inode stuff.

CHAPTER11 - ls -l sure shows # of links. The attempt was to try find what (and where) those links are!

It doesn't seem trivial to me any more.

Thanks

 
The best you are going to do with linux, not sure about the
other *nixes, is a reverse lookup IMO:
find $path -type l | xargs ls -l | awk ' {print "symlink=",$9,"Pfile=", $11}'
man stat(2), to find out why.
 
First, get the inode number of the particular file.

If you want to find files only:

find ${MOUNTPOINT} -inum ${INODE}

Example:
[tt]icebox/root:/usr/local/bin$ ls -ail g*zip
76025 -rwxr-xr-x 4 root staff 104519 Jun 30 2000 gunzip
76025 -rwxr-xr-x 4 root staff 104519 Jun 30 2000 gzip
icebox/root:/usr/local/bin$ find /usr -inum 76025
/usr/local/bin/gunzip
/usr/local/bin/gzip
/usr/local/bin/zcat
/usr/local/bin/zcmp[/tt]

If you want to find links that include ..-links on directories, it becomes a bit hairier, and I'm not fully awake yet. If you want a script that will do that, let me know and I'll chew on it for a few if someone else doesn't fill you in first.
 
Cool. Learn something new everyday :)

I didn't realize you could find with INODE. I guess this works because you can't have HARD LINKS across mount points.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top