Simpler Navigation for Servers and Operating Systems - Please Update Your Bookmarks
Completed: a much simpler Servers and Operating Systems section of the Community. We combined many of the older boards, so you won't have to click through so many levels to get at the information you need. Check the consolidated boards here as many sub-forums are now single boards.
If you have bookmarked forums or discussion boards in Servers and Operating Systems, we suggest you check and update them as needed.
General
cancel
Showing results for 
Search instead for 
Did you mean: 

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
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
TwoProc
Honored Contributor

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

I agree with Bill and System Administrator - to find out if you've got a file pointing to your files you've got to search EACH and EVERY filesystem on your server.

I'd say do that very thing and spool it all to a file, and take a look at it manually.

To find all the softlinks in all the file systems you'll need something like...

for i in `bdf | awk '/dev/{ print $6 }'
do
find $i -xdev -type l -exec ls -al {} \;
done > mylinkfilelisting

Then just vi this file and - providing you don't have 10,000 symbolic file links (you probably have at most 300 or so in the whole system) you'll be able to quickly identify which symbolic links are linked to your files.

To search for hard links, your job is just a little easier, b/c as someone said you'll only need to search the same file system that you're on - and you need to search for the same inode.

I'm going to presume that all of the files you want to search for links on are in the current directory (so for each database file destination directory you'll have to repeat).

for i in `ls -il | awk '{ print $1 }'
do
find `bdf . | awk '/dev/{ print $6}'` -xdev -inum $i -exec ls -al {} \;
done > myhardlinkslisting

OK, there's one more, read the manpage for "find" command and look at the "-linkedto" option. If I'm reading this right you could search all file systems and it would return both soft and hard file links.

So given that you're in a directory that has datafiles you want searched this could possibly work (I've not checked this out - so you need to):

for i in `bdf | awk '/dev/{ print $6 }'
do
for j in *
do
find $i -xdev -linkedto `pwd`/$j -exec ls -al {} \;
done > mylinkedfiles

We are the people our parents warned us about --Jimmy Buffett