104.6. Create and change hard and symbolic links
Last updated
Last updated
Weight: 2
Description: Candidates should be able to create and manage hard and symbolic links to a file.
Key Knowledge Areas:
Create links
Identify hard and/or soft links
Copying versus linking files
Use links to support system administration tasks
Terms and Utilities:
ln
ls
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.
-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 ..)
hard link
soft link
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
Use the 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).
ls -l command shows all the links with the link column showing the number of links:
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.
ls -l command shows all links with second column value 1 and the link points to the original file.
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.
Identifying links via find command
To find which files are hard links to a particular inode, use the find
command and the -samefile
option with a file name or the -inum
option with an inode number:
We can also use the find
command to search for symbolic links using the -type l
find expression:
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.
symbolic links can be broken but that subsequent write operations may create a new file. Use links with care.
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.
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.
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.
.
.
.
remove symbolic links
We can remove (delete) symbolic links using the rm
, unlink
, and find
commands.
To remove a symlink, you need to have writing permissions on the directory that contains the symlink. Otherwise, you will get “Operation not permitted” error.
Remove Symbolic Links with rm
To delete a symlink, invoke the rm
command followed by the symbolic link name as an argument: rm symlink_name
With rm
you can delete more than one symbolic links at once. To do that pass the names of the symlinks as arguments, separated by space: rm symlink1 symlink2
Try -i to get confirmed.
If the name of the argument ends with /
, the rm
command assumes that the file is a directory. The error happens because, when used without the -d
or -r
option, rm
cannot delete directories.
unlink
The unlink command deletes a given file!
Unlike rm, unlink accepts only a single argument.
To delete a symbolic link, run the unlink command followed by the symlink name as an argument: unlink symlink_name
Do not append the / trailing slash at the end of the symlink name because unlink cannot remove directories.
To find all broken symbolic links under a given directory, run the following command: find /path/to/directory -xtype l
Once you find the broken symlinks, you can either manually remove them with rm
or unlink
or use the -delete
option of the find
command: find /path/to/directory -xtype l -delete
To remove a symbolic link, use either the rm
or unlink
command followed by the name of the symlink as an argument. When removing a symbolic link that points to a directory do not append a trailing slash to the symlink name.
.
https://developer.ibm.com/tutorials/l-lpic1-104-6/
http://bambamdeo-linux.blogspot.com/2014/11/soft-link-and-hard-link.html
https://www.ostechnix.com/explaining-soft-link-and-hard-link-in-linux-with-examples/
https://linoxide.com/linux-how-to/difference-soft-link-hard-link/
https://www.geeksforgeeks.org/soft-hard-links-unixlinux/
https://linuxize.com/post/how-to-remove-symbolic-links-in-linux/
.