This question already has an answer here:
I read "most" of the proposed questions, but I believe they all miss the complexity of the right answer.
I want, only using "basic" commands (for maximum portability) (i.e., something that would work on AIX / Linux / etc., not just something using a recent nicety ^^), to find all the files (symlinks, hardlinks and combinations thereof) pointing to a specific file/dir.
Be careful to not rush to answer
find / -ls | grep .... : it will miss many cases.
See my links below, mixing hardlinks, symlinks, relative-and-absolute paths (and also play with symlinks "././././." possibilities)
- hardlinks and symlinks can be "nested", ad-inifinitum...
- some symlinks could be with the full path, others with a relative path,
- those paths (relative or absolute) could be very complex (ex: /tmp/././../etc/file )
- a symlink could lead to a file, which hardlink to another, which is a symlink to a third, which ends up [after some more iteration] to the final destination...
In the end, I "just" need to find out a way to know what is the "final destination" of any file/link (ie, which inode will be accessed in the end?). But it's really tough (unless some magical function will tell me "the final destination inode is : ...". That's what I need!)
I thought I could simply use '-H' or '-L' options of find, but (I'm probably dumb...) it didn't work... yet.
Any info welcomed (but please, using find/ls/etc, not some "nice utility only available on linux")
Try to create some different links to the "/tmp/A" directory, and find a way to find and list them all:
mkdir /tmp/A /tmp/B ln -s /tmp/A/ /tmp/B/D #absolute symlink, with a training "/" ln -s ../A /tmp/B/E #relative symlink ln -s /tmp/A/. /home/F #absolute symlink, with a trailing "/." ln -s ../tmp/A/./. /var/L #let's get craaaaaaazy! symlinks are "fun"... ln /tmp/B/D /etc/G #Hard-link to the symlink file /tmp/B/D, which itself symlink to /tmp/A/ witch ends up being the same as "/tmp/A" in most cases. ln /etc/G /etc/G2 #and a duplicate of the previous one, just to be sure [so /etc/G, /etc/G2, and /tmp/B/D have same inode, but it's a different inode from the final destination... :( ln -s etc/G /thatsenough #symlink again, for further chaining
and then some tries:
find -H / -ls # show inodes of the links and symlinks, not of the "final" directory find -L / -ls # same thing (I do try that on an OLD AIX ...) find -H / -inum 123456 -ls #if /tmp/A/. has inode 123456. This lists *only* hardlinks to /tmp/A, ie will only print a subset (and /tmp/A have to be a file in that case)
I expected to see the final inode (123456) in front of all the paths, in one of those invocation (I also added '-follow' to both), but I always see the inodes of the links, not of the "final destination" (i.e., /tmp/A)
What could I use to find out the "final inode" I end up accessing? [the OS manages it, but can a "simple" command tell me beforehand "through that file, you will open that final inode!"?]