martes, 15 de junio de 2021

HARD LINKS AND SYMBOLIC LINKS IN LINUX




HARD LINKS

Each file in the Linux file system begins with a single hard link.

The link is between the file name and the actual data stored in the file system.

Creates a new filename that points to the exact same data as the old filename. This means that the two file names, although different, point to identical data.

For example, if I create file /home/tcarrigan/demo/link_test and write hello world in the file, I have a single hard link between the file name link_test and the file content hello world.

[tcarrigan@server demo]$ ls -l
total 4
-rw-rw-r--. 1 tcarrigan tcarrigan 12 Aug 29 14:27 link_test

Take note of the link count here (1).

Next, I create a new hard link in /tmp to the exact same file using the following command:

[tcarrigan@server demo]$ ln link_test /tmp/link_new

The syntax is ln (original file path) (new file path).

Now when I look at my filesystem, I see both hard links.

[tcarrigan@server demo]$ ls -l link_test /tmp/link_new 
-rw-rw-r--. 2 tcarrigan tcarrigan 12 Aug 29 14:27 link_test
-rw-rw-r--. 2 tcarrigan tcarrigan 12 Aug 29 14:27 /tmp/link_new

The primary difference here is the filename. The link count has also been changed (2). Most notably, if I cat the new file's contents, it displays the original data.

[tcarrigan@server demo]$ cat /tmp/link_new 
hello world

When changes are made to one filename, the other reflects those changes. The permissions, link count, ownership, timestamps, and file content are the exact same. If the original file is deleted, the data still exists under the secondary hard link. The data is only removed from your drive when all links to the data have been removed. If you find two files with identical properties but are unsure if they are hard-linked, use the ls -i command to view the inode number. Files that are hard-linked together share the same inode number.

[tcarrigan@server demo]$ ls -li link_test /tmp/link_new 
2730074 -rw-rw-r--. 2 tcarrigan tcarrigan 12 Aug 29 14:27 link_test
2730074 -rw-rw-r--. 2 tcarrigan tcarrigan 12 Aug 29 14:27 /tmp/link_new

The shared inode number is 2730074, meaning these files are identical data.


Symbolic links

link together non-regular and regular files.

They can also span multiple filesystems. By definition, a soft link is not a standard file, but a special file that points to an existing file. Let's look at how to create a soft link. I use the ln -s command and the following syntax:

ln -s (file path you want to point to) (new file path)

In the example below, I create a new file at /home/tcarrigan/demo/soft_link_test with the file content soft Hello world. I then create a soft link to that file at /tmp/soft_link_new:

[tcarrigan@server demo]$ ln -s /home/tcarrigan/demo/soft_link_test /tmp/soft_link_new
[tcarrigan@server demo]$ ls -l soft_link_test /tmp/soft_link_new 
-rw-rw-r--. 1 tcarrigan tcarrigan 17 Aug 30 11:59 soft_link_test
lrwxrwxrwx. 1 tcarrigan tcarrigan 35 Aug 30 12:09 /tmp/soft_link_new -> /home/tcarrigan/demo/soft_link_test

Notice that /tmp/soft_link_new is just a symbolic link, pointing to the original /home/tcarrigan/demo/soft_link_test. If I cat the content of /tmp/soft_link_new, I should see the soft Hello world text.

[tcarrigan@server demo]$ cat /tmp/soft_link_new 
soft Hello world

DIFFERENCE - A hard link always points a file name to data on a storage device. - A symbol link always points one filename to another filename,      which then points to information on a storage device.

No hay comentarios:

Publicar un comentario