Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check whether two file names point to the same physical file

Tags:

People also ask

How can you tell if two files are hard link to each other?

The way to recognize a hard link is to look at the second field in a long file listing. If it's a "1", the file has no hard links. If it's 2 or greater (and not a directory), the same file exists somewhere else in the file system.

Can multiple inodes point to the same file?

Each file in a filesystem has a unique inode number. Inode numbers are guaranteed to be unique only within a filesystem (i.e., the same inode numbers may be used by different filesystems, which is the reason that hard links may not cross filesystem boundaries).


I have a program that accepts two file names as arguments: it reads the first file in order to create the second file. How can I ensure that the program won't overwrite the first file?

Restrictions:

  • The method must keep working when the file system supports (soft or hard) links
  • File permissions are fixed and it is only required that the first file is readable and the second file writeable
  • It should preferably be platform-neutral (although Linux is the primary target)