2

Is there a way to create file references that won't be broken when files and directories are renamed or moved? For example, references that refer to files by something like an inode rather than a name?

  • Dear Gavin, would you please provide an example for the problem that you have so we can help more efficiently. – user88036 Sep 14 '18 at 20:21
  • See my edits is that what you mean? – Fabby Sep 14 '18 at 21:55
2

Yes, several ways.

  1. Hard links: ln file1 file2. Now file2 will be another name for file1, and no matter what you rename file1 to, or even if you delete it, file2 will always still work to access it (unless you rename/delete it too, obviously). This only works for files, not directories, and all of the links have to be on the same filesystem.
  2. Bind mounts: mount --bind file1 file2. This works like hard links, except it works for directories too, and it doesn't have the same-filesystem restriction. The downside is it's a privileged operation, so you need to be root (or be in your own user and mount namespaces).
  3. File descriptors: exec {foo}</some/file. Once you do that, /proc/self/fd/$foo will be a "magic" symlink to the file (it's "magic" in the sense that it won't break like regular symlinks would, and is possible because of the special /proc pseudo-filesystem). This works for both files and directories, doesn't need any special privileges, and is automatically inherited and usable by child processes. The downsides are that it only lasts until the process that did it exits, and that you don't have much control over the path to it.
  4. name_to_handle_at/open_by_handle_at: This meets your literal requirement, but is more complicated than any of the above and is a privileged operation. The only advantage is that the handle is regular data and doesn't require any state to be held.
0

On an EXT filesystem you can use the debugfs command to recreate a link directly to an inode:

debugfs -w /dev/mapper/vg0-root -R 'link <16> myfile'

Which will create a "file" named myfile and pointing to inode 16.

However, if you delete that file, it'll still be gone, but if it's moved, it'll remain the same.

to get the inode of a file, have a look here...

Source

  • 1
    You should only use debugfs -w on a FS that is not mounted. When you do that link, you're creating a new directory entry for that file as if you had done ln /other/path/to/that/file myfile, but not incrementing the link count for that file which means you end up with a fs in an inconsistent state. If you rm and directory entry for that file, the link count will decrease, if it drops to zero, the inode will be considered free even though there are still one directory entry pointing to it! – Stéphane Chazelas Sep 18 '18 at 14:49
  • Bad idea? delete answer? @StéphaneChazelas – Fabby Sep 18 '18 at 15:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.