Description: Candidates should be able to create and manage hard and symbolic links to a file.
Key Knowledge Areas:
Identify hard and/or soft links
Copying versus linking files
Use links to support system administration tasks
Terms and Utilities:
On a storage device, a file or directory is stored in a collection of blocks. Information about a file is held in an inode, which records information such as the owner, when the file was last accessed, how large it is, whether it is a directory or not, and who can read from or write to it.
A directory entry contains a name for a file or directory and a pointer to the inode where the information about the file or directory is stored.
The inode number is unique within a particular filesystem.
2228290 dir2228289 file1
-i switch print the index number of each file
Whats is link ? A link is simply an additional directory entry for a file or directory, allowing two or more names for the same thing.
There are two types of links : Hard Link and Soft Link.
A hard link is a directory entry that points to an inode, while a soft link or symbolic link is a directory entry that points to an inode that provides the name of another directory entry. Symbolic links are also called symlinks.
hard links point to an inode, and inodes are only unique within a particular file system, hard links cannot cross file systems(different partitions or hard disks).
You can create hard links only for files and not for directories. The exception is the special directory entries in a directory for the directory itself and for its parent (. and ..)
have same inodes number.
have different inodes numbers.
can’t cross the file system boundaries
can cross the file system
can’t link directories
allows you to link between directories
Links have actual file contents
contains the path for original file and not the contents
if the original file is removed, the link will still show you the contents of the file
Removing soft link doesn't affect anything but when the original file is removed, the link becomes a 'dangling' link that points to nonexistent file.
permissions will be updated if we change the permissions of source file
permissions will not be updated
we can use ln command to create both hard links and soft links
### For Hard Linkln [original filename] [link name]### For Soft linkln -s [original filename] [link name]
ln command to create additional hard links to an existing file (but not to a directory, even though the system sets up . and .. as hard links).
total 4drwxr-xr-x 2 root root 4096 Jan 29 08:14 dir-rw-r--r-- 1 root root 0 Jan 29 08:14 file1### see inodes2228290 dir2228289 file1### creating Hard Link[email protected]:~/sandbox# ln file1 HardLink### comparing inodes2228290 dir2228289 file12228291 file22228289 HardLink
ls -l command shows all the links with the link column showing the number of links:
total 4drwxr-xr-x 2 root root 4096 Jan 29 08:14 dir-rw-r--r-- 2 root root 0 Jan 29 08:14 file1-rw-r--r-- 2 root root 0 Jan 29 08:14 HardLink
look at that"2" infront of file1, it was "1" before creating HardLink.
ln command with the
-s option creates soft links. Soft links use file or directory names, which may be relative or absolute. If you are using relative names, you will usually want the current working directory to be the directory where you are creating the link. Otherwise, the link you create will be relative to another point in the file system.
[email protected]:~/sandbox# touch file2total 4drwxr-xr-x 2 root root 4096 Jan 29 08:14 dir-rw-r--r-- 2 root root 0 Jan 29 08:14 file1-rw-r--r-- 1 root root 0 Jan 29 09:05 file2-rw-r--r-- 2 root root 0 Jan 29 08:14 HardLink### see inodes[email protected]:~/sandbox# ls -litotal 42228290 drwxr-xr-x 2 root root 4096 Jan 29 08:14 dir2228289 -rw-r--r-- 2 root root 0 Jan 29 08:14 file12228291 -rw-r--r-- 1 root root 0 Jan 29 09:05 file22228289 -rw-r--r-- 2 root root 0 Jan 29 08:14 HardLink### creating soft link using relative pathroo[email protected]:~/sandbox# ln -s file2 SoftLink### see inodes2228290 dir2228289 file12228291 file22228289 HardLink2228292 SoftLink### creating soft link using absolute path,we have to be in current dir)[email protected]:~/sandbox# ln -s dir/dir2/myconfig SoftLink2myconf[email protected]:~/sandbox# ls -l | grep myconfiglrwxrwxrwx 1 root root 17 Feb 1 23:59 SoftLink2myconf -> dir/dir2/myconfig###creating soft link to a directory[email protected]:~/sandbox# ln -s dir/ soft2dir[email protected]:~/sandbox# ls -l | grep soft2dirlrwxrwxrwx 1 root root 4 Feb 2 00:04 soft2dir -> dir/
ls -l command shows all links with second column value 1 and the link points to the original file.
drwxr-xr-x 3 root root 4096 Feb 1 23:59 dir-rw-r--r-- 2 root root 0 Jan 29 08:14 file1-rw-r--r-- 1 root root 0 Jan 29 09:05 file2-rw-r--r-- 2 root root 0 Jan 29 08:14 HardLinklrwxrwxrwx 1 root root 4 Feb 2 00:04 soft2dir -> dir/lrwxrwxrwx 1 root root 5 Jan 29 09:06 SoftLink -> file2lrwxrwxrwx 1 root root 17 Feb 1 23:59 SoftLink2myconf -> dir/dir2/myconfig
Since hard links always point to an inode that represents a file, they are always valid. However, symlinks can be broken for many reasons, including:
Either the original file or the target of the link did not exist when the link was created
The target of a link is deleted or renamed.
Some element in the path to the target is removed or renamed.
Depending on what we want to accomplish, sometimes we will use links and sometimes it may be better to make a copy of a file.
The major difference is that links provide multiple names for a single file, while a copy creates two sets of identical data under two different names.
You would certainly use copies for backup and also for test purposes where you want to try out a new program without putting your operational data at risk.
You use links when we need an alias for a file (or directory), possibly to provide a more convenient or shorter path.
when we update a file, all the links to it see the update, which is not the case if you copy a file.
Links, especially symbolic links, are frequently used in Linux system administration.
1- Aliasing commands to a particular version
Commands are often aliased, so the user does not have to know a version number for the current command but can access other versions by longer names if necessary.
[email protected]:~/sandbox# which python/usr/bin/python[email protected]:~/sandbox# ls -l /usr/bin/pythonlrwxrwxrwx 1 root root 9 Nov 23 2017 /usr/bin/python -> python2.7
2-Command alias examples
Other uses come into play when multiple command names use the same underlying code, such as the various commands for stopping and for restarting a system. Sometimes, a new command name, such as
genisoimage, will replace an older command name, but the old name (mkisofs) is kept as a link to the new command.
[email protected]:~/sandbox# which halt/sbin/halt[email protected]:~/sandbox# ls -l /sbin/haltlrwxrwxrwx 1 root root 14 Nov 27 2018 /sbin/halt -> /bin/systemctl[email protected]:~/sandbox# which mkisofs/usr/bin/mkisofs[email protected]:~/sandbox# ls -l /usr/bin/mkisofslrwxrwxrwx 1 root root 11 Nov 26 2017 /usr/bin/mkisofs -> genisoimage
3- Library links
Library names are also managed extensively using symlinks, either to allow programs to link to a general name while getting the current version, or to manage systems such as 64-bit systems that are capable of running 32-bit programs.
[email protected]:~/sandbox# ls -l /usr/lib/...lrwxrwxrwx 1 root root 30 Jul 31 2018 libblas.so.3 -> /etc/alternatives/libblas.so.3lrwxrwxrwx 1 root root 18 Jul 9 2011 libfsplib.so.0 -> libfsplib.so.0.0.0-rw-r--r-- 1 root root 22808 Jul 9 2011 libfsplib.so.0.0.0lrwxrwxrwx 1 root root 19 Dec 21 2015 libgdiplus.so -> libgdiplus.so.0.0.0lrwxrwxrwx 1 root root 19 Dec 21 2015 libgdiplus.so.0 -> libgdiplus.so.0.0.0-rw-r--r-- 1 root root 423928 Dec 21 2015 libgdiplus.so.0.0.0drwxr-xr-x 2 root root 4096 Dec 1 2018 libmbim-rw-r--r-- 1 root root 241488 Apr 12 2016 libMonoPosixHelper.so-rw-r--r-- 1 root root 133880 Apr 12 2016 libMonoSupportW.solrwxrwxrwx 1 root root 17 Nov 26 2017 libnetpbm.so.10 -> libnetpbm.so.10.0...