SUMMARY: Finding hard links

From: Rick von Richter (rickv@mwh.com)
Date: Wed Mar 26 1997 - 14:15:51 CST


Thanks to all who responded. This is a good one to save.

Original question:

Does anyone know the find command to search the filesystem for hardlinked files. I have done it
before but didn't write down the command. Grrrrr....

==============================

Most people suggested the correct way to go using the find command with the -links qualifier.
After a few refinements, here is the best command I found to find hard links.

# find <dir> ! -type d -links +1 -ls | sort -n

- Replace <dir> with the directory you want to search.
- The ! -type d is used because you don't want directories to show up.
- The -links +1 will find all files that have MORE than 1 link. ALL files have a link count of
at least 1. Hardlinked files have a link count of at least two. (Do an 'ls -l' to see the link
count. The link count is the second column of 'ls -l')
- The -ls is used to view the inode number after find has found the file
- The sort -n will sort the list by inode number showing you which files are hardlinked together.
This will only work if your search includes the directories that contain all of the hardlinked
files. For example; if you do the above command replacing <dir> with /etc/init.d then you will see
a list of hardlinked files but you will not see the files they are hardlinked with. If you do the
same command but replace <dir> with /etc you will see the hardlinked files plus the other files
they are hardlinked to. Files that are hardlinked have the same inode number.

You can also append the above command with '| more' to see the output a page at a time.

NOTE: Files can only be hardlinked if they reside on the same filesystem (e.g. c0t3d0s0 etc...) In
other words, if /usr is mounted on c0t3d0s0 and /opt is mounted c0t1d0s4 then you cannot hardlink
/usr/myfile1 with /opt/myfile2. If you try this you will get an error message.
Example:
# touch /usr/myfile1
# ln /usr/myfile1 /opt/myfile2
ln: /opt/myfile2 is on a different file system <--- This is the error message you will see.

So if you do the above find command replacing <dir> with / you will see all hardlinked files on all
filesystems, including nfs mounted filesystems.

If you want to search the entire root filesystem but not go into any of the other filesystems you
can use the -mount parameter like this;

# find / -mount ! -type d -links +1 -ls | sort -n

This will list all the hardlinked files that are in the / filesystem. If your /, /usr, & /var
directories are on different filesystems then try the above command. It has a short output.

Some people suggested using '-type l' with the find command. This will find SOFT links. Know the
difference. Soft links can span different filesystems but are slower to access than hardlinks
because the system will read the location of a soft link the start the whole search process over
again to find the file it is softlinked with. When the system encounters a hardlinked file, it
simple goes to that inode and it finds the file immediately without having to traverse any
filesystem structures. Soft links are a lot easier to see than hard links in an 'ls -l' command
because they stick out like a sore thumb with that '->' stuff that shows up in an 'ls -l' output.

=========================================

Thanks to my responders;

Anthony.Worrall@reading.ac.uk (Anthony Worrall)
Michael Bennett mjb@liffe.com
djohnson@nbserv2.dseg.ti.com (Danny Johnson)
Matthew Stier <mstier@hotmail.com>
Roger Blayney <R.Blayney@greenwich.ac.uk>
Sophia Sameera Corsava <106625.3502@compuserve.com>
Steve Kives <skives@cantor.com>
Andrew Moffat <amof@SubaruSparcDev.subaru1.com>
Kevin.Sheehan@uniq.com.au (Kevin Sheehan)
Bruce Rossiter <AROSSITE@us.oracle.com>
Charles Gagnon <charles@Grafnetix.COM>

Good luck...

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  Rick von Richter | Phone: 619-552-6222
  Systems/Network Admin | Fax: 619-552-6221
  Maintenance Warehouse | Email: rickv@mwh.com
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     Science is true. Don't be misled by facts.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~



This archive was generated by hypermail 2.1.2 : Fri Sep 28 2001 - 23:11:49 CDT