Link to a Specific Inode

Link to a specific inode

Copy from /proc/pid/fd/file descriptor

Use lsof to find the pid and the file descriptor.

The links to a file

Because there is nothing special about the first hard link (soft links are different, they're really just special files containing the textual target) to a file, it's just a directory entry that refers to the inode behind it.

This is a consequence of the decision to separate data (inode) from references to the data (directory entry).

So don't think of the file being one directory entry, with other files linked to it. Think instead of the data in the file being the thing, with as many directory entries as you need referring to it. When you hard-link directory entry a to an existing b, you're really linking it to the inode behind b, and they are equals in every sense of the word. The data is only ever destroyed when the final hard link to it is disconnected.

Does creating a hard link create a new inode?

The same method used to show ln -s creates a new inode can be used to verify ln alone does not. Creating a hard link creates a directory entry pointing to the very same inode (here number 26477281).

$ touch foo 
$ ls -li foo
26477281 -rw-r--r-- 1 jlliagre jlliagre 0 Nov 10 21:39 foo
$ ln foo bar
$ ls -li foo bar
26477281 -rw-r--r-- 2 jlliagre jlliagre 0 Nov 10 21:39 bar
26477281 -rw-r--r-- 2 jlliagre jlliagre 0 Nov 10 21:39 foo

Note that the link count was changed from 1 to 2 after the hard link creation.

What is the difference between a symbolic link and a hard link?

Underneath the file system, files are represented by inodes. (Or is it multiple inodes? Not sure.)

A file in the file system is basically a link to an inode.

A hard link, then, just creates another file with a link to the same underlying inode.

When you delete a file, it removes one link to the underlying inode. The inode is only deleted (or deletable/over-writable) when all links to the inode have been deleted.

A symbolic link is a link to another name in the file system.

Once a hard link has been made the link is to the inode. Deleting, renaming, or moving the original file will not affect the hard link as it links to the underlying inode. Any changes to the data on the inode is reflected in all files that refer to that inode.

Note: Hard links are only valid within the same File System. Symbolic links can span file systems as they are simply the name of another file.

What is there behind a symbolic link?

It is not about UNIX/Linux but about filesystem implementation - but yes, Unix/Linux uses inodes at kernel level and filesystem implementations have inodes (at least virtual ones).

In the general, symbolic links are simply files (btw, directories are also files), that have:

  • the flag file-type in the "inode" that tells to the system this file is a "symbolic link"
  • file-content: path to the target - in other words: a symbolic link is simply a file which contains a filename with a flag in the inode.

Virtual filesystems can have symbolic links too, so, check FUSE or some other filesystem implementation sources. (ext2/ext3/ufs..etc)

So,

Is the answer an inode in UNIX/Linux?

depends on filesystem implementation, but yes, generally the inode contains a "file-type" (and owners, access rights, timestamps, size, pointers to data blocks). There are filesystems that don't have inodes (in a physical implementation) but have only "virtual inodes" for maintaining compatibility with the kernel.

If yes, then will the inode number be same for target and links?

No. Usually, the symlink is a file with its own inode, (with file-type, own data blocks, etc.)

If yes, can the link inode can have permissions different from that of target's
inode(if one exists)?

This is about how symlink files are handled. Usually, the kernel doesn't allow changes to the symlink permissions - and symlinks always have default permissions. You could write your own filesystem that would allow different permissions for symlinks, but you would get into trouble because common programs like chmod don't change permissions on symlinks themselves, so making such a filesystem would be pointless anyway)

To understand the difference between hard links and symlinks, you should understand directories first.

Directories are files (with differentiated by a flag in the inode) that tell the kernel, "handle this file as a map of file-name to inode_number". Hard-links are simply file names that map to the same inode. So if the directory-file contains:

file_a: 1000
file_b: 1001
file_c: 1000

the above means, in this directory, are 3 files:

  • file_a described by inode 1000
  • file_b described by inode 1001 and
  • file_c again described by inode 1000 (so it is a hard link with file_a, not hardlink to file_a - because it is impossible to tell which filename came first - they are identical).

This is the main difference to symlinks, where the inode of file_b (inode 1001) could have content "file_a" and a flag meaning "this is a symlink". In this case, file_b would be a symlink pointing to file_a.

Linux: what is link

First You should be know what is Inode ? to understand advantage of link type let me

An inode is an entry in inodetable, containing information ( the metadata ) about a regular file and directory. An inode is a data structure on a traditional Linux file system such as ext3 or ext4.
Inode number also called as index number , it consists following attributes.

  • File types ( executable, block special etc)
  • Permissions ( read, write etc)
  • UID ( Owner )
  • GID ( Group )
  • FileSize
  • Time stamps including last access, last modification and last
    inode number change.
  • File deletion time
  • Number of links ( soft/hard )
  • Location of file on harddisk.
  • Some other metadata about file.

to Display Inode use this command+flags

ls –il

What is a link?

A link is simply a way to refer to the contents of a file.

Types of link:

  1. Hardlink(Another name for the file/inode in the disk)
  2. Softlink/symbolic link (Pointer to the file location)

How to create Links in linux ?

  1. Hard link ln existingfile newfile

    Note : Hardlinksare not allowed for directories

  2. Soft link ln –s existingfile newfile

A soft link will have a different Inode number than the source file, which will be having a pointer to the source file but hard link will be using the same Inode number as the source file.
Soft link is like shortcut in windows. It doesn’t contain any information about the destination file or contents of the file, instead of that, it simply contains the pointer to the location of the destination file.
Soft Links You can make links for files & folder & you can create link (shortcut) on different partition & got different inode number from original.
If real copy is deleted the link will not work.
Hard Links
For files only & you cannot create on different partition ( it should be on same partition ) & got same inode number as original
If therealcopy is deleted the link will work( because it act as original file )



Related Topics



Leave a reply



Submit