s
Working with Links
In a Linux file system, it is very useful to be able to access a single file from different locations
This discourages you from copying a file to different locations, where subsequently different versions of the file may come to exist.
In a Linux file system, you can use “links” for this purpose
A “link” appears to be a regular file, but it’s more like a pointer that exists in one location to show you how to get to another location
Understanding "inodes" to help understand "links" in a file system:
To understand links in a file system, you first have to understand what an inode is.
An inodeis a data structure that contains metadata about a file.
When the file system stores a new file on the hard disk, it stores not only the contents (data) of the file, but also extra properties like the name of the file, the creation date, its permissions, the owner of the file, and more.
All this information (except the name of the file and the contents of the file) isstored in the inodeof the file.
The ls -l command will display some of the inode contents, as seen in this screenshot
Example:
root@rhel53 ~# ls -ld /home/project42/
drwxr-xr-x 4 root pro42 4.0K Mar 27 14:29 /home/project42/
inode table:
The inode table contains all of the inodesand is created when you create the file system (with mkfs).
Use “df –i” command to see how many inodes are used and free on mounted file systems:
Example:
root@rhel53 ~# df -i
Filesystem InodesIUsedIFreeIUse% Mounted on
/dev/mapper/VolGroup00-LogVol00
4947968 115326 4832642 3% /
/dev/hda1 26104 45 26059 1% /boot
tmpfs 64417 1 64416 1% /dev/shm
/dev/sda1 262144 2207 259937 1% /home/project42
/dev/sdb1 74400 5519 68881 8% /home/project33
/dev/sdb5 0 0 0 - /home/sales
/dev/sdb6 100744 11 100733 1% /home/research
In the df -iscreenshot above you can see the inodeusage for several mounted file systems.
You don't see numbers for /dev/sdb5 because it is a fat file system
inode number:
Each inodehas a unique number (the inode number). You can see the inodenumbers withthe ls -li command:
Example:
paul@RHELv4u4:~/test$ touch file1
paul@RHELv4u4:~/test$ touch file2
paul@RHELv4u4:~/test$ touch file3
paul@RHELv4u4:~/test$ ls -li
total 12
817266 -rw-rw-r-- 1 paulpaul 0 Feb 5 15:38 file1
817267 -rw-rw-r-- 1 paulpaul 0 Feb 5 15:38 file2
817268 -rw-rw-r-- 1 paulpaul 0 Feb 5 15:38 file3
paul@RHELv4u4:~/test$
These three files were created one after the other and got three different inodes(the firstcolumn).
All the information you see with this ls command resides in the inode, except forthe filename (which is contained in the directory).
inode and file contents:
Let's put some data in one of the files.
Example:
paul@RHELv4u4:~/test$ ls -li
total 16
817266 -rw-rw-r-- 1 paulpaul 0 Feb 5 15:38 file1
817270 -rw-rw-r-- 1 paulpaul 92 Feb 5 15:42 file2
817268 -rw-rw-r-- 1 paulpaul 0 Feb 5 15:38 file3
paul@RHELv4u4:~/test$ cat file2
It is winter now and it is very cold.
We do not like the cold, we prefer hot summer nights.
paul@RHELv4u4:~/test$
The data that is displayed by the cat command is not in the inode, but somewhere else onthe disk.
The inodecontains a pointer to that data.
about “directories”
A directory is a table
A directory is a special kind of file that contains a table which maps filenames to inodes.
Listing our current directory with ls -aliwill display the contents of the directory file.
Example:
paul@RHELv4u4:~/test$ ls -ali
total 32
817262 drwxrwxr-x 2 paulpaul 4096 Feb 5 15:42 .
800768 drwx------16 paulpaul 4096 Feb 5 15:42 ..
817266 -rw-rw-r-- 1 paulpaul 0 Feb 5 15:38 file1
817270 -rw-rw-r-- 1 paulpaul 92 Feb 5 15:42 file2
817268 -rw-rw-r-- 1 paulpaul 0 Feb 5 15:38 file3
paul@RHELv4u4:~/test$
Continuing with Links:
There are 2 different types of links:
“symbolic link”:
--- the most flexible link type used
--- points to any other file and directory, no matter where itis located
--- with symbolic links, there is a difference between the original file and the link. . .If you remove the original file, the symbolic link won’t work anymore and thus is invalid
--- Symbolic links (sometimes called soft links) do not link to inodes, but create a name toname mapping.
--- Symbolic links are created with ln -s.
--- As you can see below, the symboliclink gets an
inode of its own.
Symbolic (soft) Link Example:
paul@RHELv4u4:~/test$ ln -s file2 symlink_to_file2
paul@RHELv4u4:~/test$ ls -li
total 32
817273 -rw-rw-r-- 1 paulpaul 13 Feb 5 17:06 file1
817270 -rw-rw-r-- 2 paulpaul 106 Feb 5 17:04 file2
817268 -rw-rw-r-- 1 paulpaul 0 Feb 5 15:38 file3
817270 -rw-rw-r-- 2 paulpaul 106 Feb 5 17:04 hardlink_to_file2
817267 lrwxrwxrwx 1 paulpaul 5 Feb 5 16:55 symlink_to_file2 -> file2
paul@RHELv4u4:~/test$
--- Permissions on a symbolic link have no meaning, since the permissions of the target apply.
“hard link”:
--- can be used only to point to a file that exists on the same device
--- more like an additional name you’d give
to a file
--- to understand “hard links”, you have to appreciate how Linux file systems work with inodes
--- The “inode” is the administration of a file:
To get to a file, the file system reads the file’s inode in the file system metadata, and from there it learns how to access the block where the actual data of the file is stored
To get to the inode, the file system uses the filename that exists somewhere in a directory
With hard links, you only need the original filename to create the hard link. Once it has been created, it isn’t needed anymore, and the original filename can be removed
--- hard links have some seriouslimitations, so normally, you’ll use symbolic links
--- When we create a hard link to a file with ln, an extra entry is added in the directory. A newfile name is mapped to an existing inode.
Hard Link Example:
paul@RHELv4u4:~/test$ ln file2 hardlink_to_file2
paul@RHELv4u4:~/test$ ls -li
total 24
817266 -rw-rw-r-- 1 paulpaul 0 Feb 5 15:38 file1
817270 -rw-rw-r-- 2 paulpaul 92 Feb 5 15:42 file2
817268 -rw-rw-r-- 1 paulpaul 0 Feb 5 15:38 file3
817270 -rw-rw-r-- 2 paulpaul 92 Feb 5 15:42 hardlink_to_file2
paul@RHELv4u4:~/test$
Both files have the same inode, so they will always have the same permissions and the sameowner.
Both files will have the same content.
Actually, both files are equal now, meaningyou can safely remove the original file, the hardlinked file will remain.
The inode containsa counter, counting the number of hard links to itself. When the counter drops to zero, thenthe inode is emptied.
finding hard links:
You can use the find command to look for files with a certain inode.
The screenshot belowshows how to search for all filenames that point to inode817270. Remember: that an inodenumber is unique to its partition.
To Create a Link:
use the “ln” command
use the –s option to create a symbolic link . . without this option, you’ll automatically create a hard link
1st put the name of the original file directly after the “ln” command
2nd, specify the name of the link you want to create
Example: ln –s /etc/passwd ~/users [create a symbolic link with the name users in your home directory .. this link points to the original file “/etc/paswd”]
Exercises:
creating links
Links exercise