Links in Linux
Introduction: file types in Linux
In Linux everything is files and links are no exception.
Hard links
Run
ls -ld /etc
drwxr-xr-x. 85 root root 8192 Nov 22 00:18 /etc
85 in this example this value is hard link count - the number of hard links with the same metadata value.
-d means show the directories themselves and not their contents.
-l means show in long listing
If you add the -i (--inode) flag, which shows the number of each file, the result will be approximately as follows.
ls -ldi /etc
16777281 drwxr-xr-x. 85 root root 8192 Nov 22 00:183 /etc
The command will give a similar result
ls -ldi /etc/.
16777281 drwxr-xr-x. 85 root root 8192 Nov 22 00:18 /etc/.
ls -ldi /etc/. and ls -ldi /etc are linked to the same metadata
Thus the minimum number of hard links to a directory is two.
By the inode number you can find the path to the directory
sudo find / -inum 16777281
/etc
If you create an empty directory and study the links
mkdir d1
ls -ld d1
drwxr-xr-x 2 andrei users 4096 Nov 22 00:18 d1
ls -l d1
total 0
ls -la d1
total 8 drwxr-xr-x 2 andrei users 4096 Nov 22 00:18 . drwx------ 60 andrei users 4096 Nov 22 00:18 ..
ls -ldi d1 d1/.
582294 drwxr-xr-x 2 andrei users 4096 Nov 22 00:18 d1 582294 drwxr-xr-x 2 andrei users 4096 Nov 22 00:18 d1/.
If you create in d1 a subdirectory, the number of hard links to d1 will increase from 2 to 3, since a link from the subdirectory is added.
mkdir d1/subd1
ls -la d1
total 12 drwxr-xr-x 3 andrei users 4096 Nov 22 00:18 . drwx------ 60 andrei users 4096 Nov 22 00:18 .. drwxr-xr-x 2 andrei users 4096 Nov 22 00:18 subd1
You can make a hard link to a regular file
echo hi > file1
cat file1
hi
ls -l file1
-rw-rw-r--. 1 andrei andrei 3 Nov 22 00:18 file1
ln file1 file2
ls -l file1
-rw-rw-r--. 2 andrei andrei 3 Nov 22 00:18 file1
ls -li file1 file2
51314587 -rw-rw-r--. 2 andrei andrei 3 Nov 22 00:18 file1 51314587 -rw-rw-r--. 2 andrei andrei 3 Nov 22 00:18 file2
As we have already seen file1 and file2 are the same file.
Hard links can point to files only within their own file system
ln /boot/vmlinuz-3.10.0-1160.49.1.el7.x86_64 ./link1
ln: failed to create hard link './link1' => '/boot/vmlinuz-3.10.0-1160.49.1.el7.x86_64': Invalid cross-device link
Symbolic links
Besides hard links you can create symbolic links
ln -s file1 file3
ls -li file1 file2 file3
51314587 -rw-rw-r--. 2 andrei andrei 3 Nov 22 00:18 file1 51314587 -rw-rw-r--. 2 andrei andrei 3 Nov 22 00:18 file2 51314588 lrwxrwxrwx. 1 andrei andrei 5 Nov 22 00:18 file3 -> file1
If you delete the file
file1
your terminal should start signalling that the symbolic link leads nowhere. It may turn red, and the non-existent target file will blink.
A hard link, when its target is deleted, will lose 1 in the links column.
rm file1
ls -l
-rw-rw-r--. 1 andrei andrei 0 Nov 22 00:18 file2
lrwxrwxrwx. 1 andrei andrei 5 Nov 22 00:18 file3 -> file1
Note that a symbolic link has before the access permissions a l
Symbolic links can point to files of another file system
ln -s /boot/vmlinuz-3.10.0-1160.49.1.el7.x86_64 ./link1
ls -l
… lrwxrwxrwx. 1 andrei andrei 41 Nov 22 00:18 link1 -> /boot/vmlinuz-3.10.0-1160.49.1.el7.x86_64
Practical examples
If the system is installed on a small disk, for example
an SSD, it makes sense to connect a cheaper HDD for storing media.
Suppose you first used the directory
/uploads
on the SSD; to avoid changing the code in the project, you can move the contents of the old
/uploads
to the new HDD and make a symbolic link like
ln -s /mnt/hdd /uploads
An example from
Nginx
In the Nginx configuration there are directories such as sites-available and sites-enabled;
to «enable» a site you need to add a link to the site from sites-available into sites-enabled
ll /etc/nginx | grep sites
drwxr-xr-x 2 root root 4096 tammi 20 18:12 sites-available/ drwxr-xr-x 2 root root 4096 tammi 20 18:12 sites-enabled/
Article author: Andrei Olegovich