How can you see the actual hard link by ls?
You can find inode number for your file with
ls -i
and
ls -l
shows references count (number of hardlinks to a particular inode)
after you found inode number, you can search for all files with same inode:
find . -inum NUM
will show filenames for inode NUM in current dir (.)
There isn't really a well-defined answer to your question. Unlike symlinks, hardlinks are indistinguishable from the "original file".
Directory entries consist of a filename and a pointer to an inode. The inode in turn contains the file metadata and (pointers to) the actual file contents). Creating a hard link creates another filename + reference to the same inode. These references are unidirectional (in typical filesystems, at least) -- the inode only keeps a reference count. There is no intrinsic way to find out which is the "original" filename.
By the way, this is why the system call to "delete" a file is called unlink
. It just removes a hardlink. The inode an attached data are deleted only if the inode's reference count drops to 0.
The only way to find the other references to a given inode is to exhaustively search over the file system checking which files refer to the inode in question. You can use 'test A -ef B' from the shell to perform this check.
UNIX has hard links and symbolic links (made with "ln"
and "ln -s"
respectively). Symbolic links are simply a file that contains the real path to another file and can cross filesystems.
Hard links have been around since the earliest days of UNIX (that I can remember anyway, and that's going back quite a while). They are two directory entries that reference the exact same underlying data. The data in a file is specified by its inode
. Each file on a file system points to an inode but there's no requirement that each file point to a unique inode - that's where hard links come from.
Since inodes are unique only for a given filesystem, there's a limitation that hard links must be on the same filesystem (unlike symbolic links). Note that, unlike symbolic links, there is no privileged file - they are all equal. The data area will only be released when all the files using that inode are deleted (and all processes close it as well, but that's a different issue).
You can use the "ls -i"
command to get the inode of a particular file. You can then use the "find <filesystemroot> -inum <inode>"
command to find all files on the filesystem with that given inode.
Here's a script which does exactly that. You invoke it with:
findhardlinks ~/jquery.js
and it will find all files on that filesystem which are hard links for that file:
pax@daemonspawn:~# ./findhardlinks /home/pax/jquery.js
Processing '/home/pax/jquery.js'
'/home/pax/jquery.js' has inode 5211995 on mount point '/'
/home/common/jquery-1.2.6.min.js
/home/pax/jquery.js
Here's the script.
#!/bin/bash
if [[ $# -lt 1 ]] ; then
echo "Usage: findhardlinks <fileOrDirToFindFor> ..."
exit 1
fi
while [[ $# -ge 1 ]] ; do
echo "Processing '$1'"
if [[ ! -r "$1" ]] ; then
echo " '$1' is not accessible"
else
numlinks=$(ls -ld "$1" | awk '{print $2}')
inode=$(ls -id "$1" | awk '{print $1}' | head -1l)
device=$(df "$1" | tail -1l | awk '{print $6}')
echo " '$1' has inode ${inode} on mount point '${device}'"
find ${device} -inum ${inode} 2>/dev/null | sed 's/^/ /'
fi
shift
done