Create links between files
Manage links between files
hard/soft link
Create multiple names pointing to the same file.
Create hard link
Every file starts with a hard link, from its initial name to the file system's data. When a new hard link to a file is created, another name pointing to the same data is also created. The new hard link has the same effect as the original file name. Once created, the new hard link is identical to the file's original name.
[root@servera ~]# pwd
/root
[root@servera ~]# touch newfile.txt
[root@servera ~]# ls -l newfile.txt
-rw-r--r--. 1 root root 0 Jul 16 04:53 newfile.txt
newfile.txt
The number of links is 1, absolute address/root/newfile.txt
Use the ln command to create a new hard link to an existing file. At least two parameters are required, namely the path to the existing file and the path to the hard link to be created.
Create a hard link to the existing file newfile.txt in the /tmp directory
[root@servera ~]# ls -l newfile.txt /tmp/newfile-link2.txt
-rw-r--r--. 2 root root 0 Jul 16 04:53 newfile.txt
-rw-r--r--. 2 root root 0 Jul 16 04:53 /tmp/newfile-link2.txt
If you want to know whether two files are hard links to each other, use ls -i to list the inode numbers of the files. If the files are on the same system and have an index numbering system, then the two files are hard links pointing to the same data.
[root@servera ~]# ls -il newfile.txt /tmp/newfile-link2.txt
33579695 -rw-r--r--. 2 root root 0 Jul 16 04:53 newfile.txt
33579695 -rw-r--r--. 2 root root 0 Jul 16 04:53 /tmp/newfile-link2.txt
Even if the original file is deleted, the file content will still be available as long as at least one hard link exists. Only deleting the last hard link will remove the data from storage.
[root@servera ~]# rm -f newfile.txt
[root@servera ~]# ls -l /tmp/newfile-link2.txt
-rw-r--r--. 1 root root 6 Jul 16 05:03 /tmp/newfile-link2.txt
[root@servera ~]# cat /tmp/newfile-link2.txt
Hello
Hard link limitations
- Only for regular files, you cannot use ln to create hard links to directories or special files
- Hard links can only be used when both files are on the same file system.
List directories located on different file systems
[root@servera ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
devtmpfs 874504 0 874504 0% /dev
tmpfs 894592 0 894592 0% /dev/shm
tmpfs 894592 8900 885692 1% /run
tmpfs 894592 0 894592 0% /sys/fs/cgroup
/dev/mapper/rhel-root 17811456 1884916 15926540 11% /
/dev/nvme0n1p1 1038336 225396 812940 22% /boot
tmpfs 178916 0 178916 0% /run/user/0
The files of two different Mounted On directories and their subdirectories are on different file systems.
Create soft link
Created by ln -s, soft links are not regular files, but special files that point to existing files or directories.
- Can link two files on different file systems
- Can point to a directory or special file, not limited to regular files
To /tmp/newfile-link2.txt
create a new soft link
[root@servera ~]# ln -s /tmp/newfile-link2.txt /tmp/newfile-symlink.txt
[root@servera ~]# ls -l /tmp/newfile-link2.txt /tmp/newfile-symlink.txt
-rw-r--r--. 1 root root 6 Jul 16 05:03 /tmp/newfile-link2.txt
lrwxrwxrwx. 1 root root 22 Jul 16 05:17 /tmp/newfile-symlink.txt -> /tmp/newfile-link2.txt
[root@servera ~]# cat /tmp/newfile-symlink.txt
Hello
When the original regular file is deleted, the soft link still points to the file, but the target disappears. Soft links pointing to missing files become "dangling soft links"
[root@servera ~]# rm -rf /tmp/newfile-link2.txt
[root@servera ~]# ls -l /tmp/newfile-symlink.txt
lrwxrwxrwx. 1 root root 22 Jul 16 05:17 /tmp/newfile-symlink.txt -> /tmp/newfile-link2.txt
[root@servera ~]# cat /tmp/newfile-symlink.txt
cat: /tmp/newfile-symlink.txt: No such file or directory
Soft links can point to directories. Plays the same role as a directory. Changing to a soft link via cd will change the current working directory into the linked directory. Some tools can track the fact that the current working directory was reached using soft links. For example, cd will update the current working directory with the name of the soft link. -P updates to actual directory name.
Create a soft link pointing to the /etc directory
[root@servera ~]# ln -s /etc /root/configfiles
[root@servera ~]# cd /root/configfiles/
[root@servera configfiles]# cd -P /root/configfiles/
[root@servera etc]# pwd
/etc
[root@servera etc]# cd /root/configfiles/
[root@servera configfiles]# pwd
/root/configfiles