Operating System - HP-UX
1748108 Members
4887 Online
108758 Solutions
New Discussion юеВ

how to find if a file or directory is linked

 
SOLVED
Go to solution
Balaji_5
Advisor

how to find if a file or directory is linked

Is there any unix command that tells me whether a file or directory is linked from another location?
10 REPLIES 10
Camel_1
Valued Contributor

Re: how to find if a file or directory is linked

try this out

find . -type l -exec ll {} \;

Ninad_1
Honored Contributor
Solution

Re: how to find if a file or directory is linked

Hi,

Basically there are two types of links - hard link and soft link.
1) When you create hard link an inode will be assigned to the new name (link name you have created) linking to the original file.This can be observed in the ls -l filename output where you will see the number of links to the file ( Mind you these are the hard links). If you do a ls -il for both the files - you will see different inode numbers. Thus when you have ahrd links you can just know how many links are there to that file but you cant know which are the links
2) When you create a soft link - only a directory entry is made pointing to the file to which the link is made - thus no separate inode is assigned.Thus if you do a ls -l to the link you will see that it is showing as pointing to some file - e.g. -> /xyz/filename

Hope this helps.

Nad
Vibhor Kumar Agarwal
Esteemed Contributor

Re: how to find if a file or directory is linked

ls -l | sed -e 's/ \( *\)/ /g' | cut -d" " -f2,9

If the entry shows greater than 1 then the file has links.
One thing to keep in mind, by default a directory has 2 links.
Vibhor Kumar Agarwal
Amit Agarwal_1
Trusted Contributor

Re: how to find if a file or directory is linked

I would like to correct Ninad, by saying hard links points to a same inode, but soft links do not. Since directory entry for hardlinks refers to inode using the inode number, hard links can't exist across filesystems. The definition mentioned by Ninad are actaully in reverse.

Now to answer your question there is no perfect way to find the if there is any soft link pointing "to" a file. For hardlinks you can get the count of hardlinks by using 'll' command. The second column is what tells that. But once again, there is no way to get the list of all files/directories hard linked with it.

-Amit
Muthukumar_5
Honored Contributor

Re: how to find if a file or directory is linked

You can find that as,

1) file -h
2) ls -l

we can use inode filed (field 2) which displayed on executing ls -l.

# Soft Link
# ls -l /tmp/file2
lrwxrwxrwx 1 root sys 10 Jul 5 01:43 /tmp/file2 -> /tmp/file1

number of links count=1

# Hard Link
# ls -l /tmp/file2
-rw-rw-rw- 2 root sys 0 Jul 5 01:43 /tmp/file2

number of links count=2

hth.
Easy to suggest when don't know about the problem!
Balaji_5
Advisor

Re: how to find if a file or directory is linked

Looking at all the responses, I think it is not possible to find if a file/directory is linked. I want to delete a directory (with files), but just wanted to make sure it is not soft linked from outside... We do not use hard links.
Muthukumar_5
Honored Contributor

Re: how to find if a file or directory is linked

If it is soft linked then, ls -l will give the link to soft location. We can find it with file -h also.

I am not sure why you are saying that "you are unable to find soft link with ln -s or file -h command"

hth
Easy to suggest when don't know about the problem!
OldSchool
Honored Contributor

Re: how to find if a file or directory is linked

I believe he wants to go the other direction. Without knowing if soft links exist to a file, can he find them. Something like this:

cd $HOME
touch fsm.list
cd /var/temp
ln -s $HOME/fsm.list fixit

Now, can he look at fsm.list in $HOME and determine the /var/temp/fixit exists.

The only thing I can think of that might help is:

cd
find . -type l -exec ls -l {}\; > somefile

then examine "somefile" to see if it contains references to the files he wants to delete

Bill Hassell
Honored Contributor

Re: how to find if a file or directory is linked

The reverse direction is not traceable with any simple command. It's like saying: I wonder if anyone is pointing at me right now? You would have search the ENTIRE filesystem (exclude things like CDROMs and NFS) looking at the softlink contents (ie, the ll command) and grep'ing for the filename.

A softlink is unique in that it doesn't maintain any connection with the target. Move the target and the softlink now reports that it isn't found. A softlink can contain anything, not just a filename, partial path or fulpath.


Bill Hassell, sysadmin