What Is "Ln -L" (--Logical) for?
I Can Read in the Ln Man Page: -L, --Logical Make Hard Links to Symbolic Link References I Read Somewhere That Ln -L Could Be Used to Re-Link Files That Were...
I can read in the ln man page:
-L, --logical
make hard links to symbolic link references
I read somewhere that ln -L could be used to re-link files that were deleted but which are still open, using the /proc filesystem. For example:
ln -L /proc/1234/fd/12 /tmp/my-file
But I'm getting ENOENT: No such file or directory. If I try on a different filesystem, I get instead Invalid cross-device link.
If I can't use ln -L to recover deleted files, then what could it be used for?
2 Answers
GNU utilities are primarily documented with info pages. From the GNU ln info page:
‘-L’
‘--logical’
If -s is not in effect, and the source file is a symbolic link,
create the hard link to the file referred to by the symbolic link,
rather than the symbolic link itself.
So this simply dereferences symbolic links given as source arguments.
Well, a little bit more newbie-friendly answer...
Must Read
Some basics beforehand
A simple view on how files are stored on UNIX/Linux systems is: There's a directory entry consisting of the name you see with ls -l and an Inode number (you may see with ls -i). The Inode contains the actual information where your data is stored on the filesystem (among other things like ownership, permissions, more Inodes if neccessary, and so on):
(Time for some UTF-8 fun... ;-) )
Simple view:
┌─────────────────┐ ┌───────┐ ┌─────────────┐
│ directory entry │ ─► │ Inode │ ─► │ data blocks │
└─────────────────┘ └───────┘ └─────────────┘
Now to the difference between Hard and symbolic link:
A hard link is simply a directory entry that points to the same Inode as an already existing one, whereas a symbolic link is just a special file that contains the name of annother file (stored directly within the Inode, if the pathname is small enough to fit in). This is the reason, why
- Hard links to the same file cannot have different file access permissions (since these are stored within the Inode)
- Hard links must reside within the same file system
Expanded simple view
┌─────────────────┐
│ hard link │ ───────┐
└─────────────────┘ ▼
┌─────────────────┐ ┌───────┐ ┌─────────────┐
│ example_file │ ─► │ Inode │ ─► │ data blocks │
└─────────────────┘ └───────┘ └─────────────┘
▲
└───────────────────────┐
│
┌─────────────────┐ ┌─────────┴──────────┐
│ symbolic link │ ─► │ filename reference │
└─────────────────┘ └────────────────────┘
Now back to the -L option with -s absent: It allows you, to create a hard link of a file where a symbolic link points to (Like "hard link" in the example above).