Udemy

Linking Files - Soft Links vs. Hard Links

A free video tutorial from Ted LeRoy
Enterprise Security Architect - Online Instructor
Rating: 4.3 out of 5Instructor rating
4 courses
11,662 students
Linking Files - Soft Links vs. Hard Links

Lecture description

Ubuntu Server - Groups - Hard Links and Soft Links

In this lesson, we’ll discuss something we haven’t touched on yet, linking files.

Instead of making copies of files all over your system, it is sometimes desirable to just link to one version of the file.

This is done with links.

A link is similar in concept to a shortcut in Windows.

Links are pointers to the actual file.

There are two types. Soft links, often referred to as symbolic links or sym links, and hard links. We’ll discuss both in this lesson.

Before discussing that, though, we’ll take a look at inode numbers, and what they are. You’ll understand why shortly.

inodes

Along with a name, every file is assigned an index or inode number. The inode number is how the system actually keeps track of the file.

Typing ls -li (the i is to print inode numbers) will yield a line similar to the following:

22514 -rw-rw-r-- 1 theo theo 1167 Nov 10 20:39 file-permissions.txt

The leading number, 22514, is the inode number for the file file-permissions.txt.

The number of inodes available on a system is limited, but is very large, depending on the file system in use.

Typing df -i will give you the status of inode use on your file system.

theo@ubuntu-server:~$ df -i

Filesystem                          Inodes  IUsed  IFree IUse% Mounted on

udev                                121959    456 121503    1% /dev

tmpfs                               127009    589 126420    1% /run

/dev/mapper/ubuntu--server--vg-root 557056 130883 426173   24% /

tmpfs                               127009      1 127008    1% /dev/shm

tmpfs                               127009      3 127006    1% /run/lock

tmpfs                               127009     16 126993    1% /sys/fs/cgroup

/dev/sda1                           124928    313 124615    1% /boot

tmpfs                               127009      4 127005    1% /run/user/1000

You can see that inode use is very low on my vm except for /dev/mapper/ubuntu—server—vg-root.

You can get more information about a particular mount point with tune2fs: 

sudo tune2fs -l /dev/sda1 | grep -i inode

Filesystem features:      ext_attr resize_inode dir_index filetype sparse_super large_file

Inode count:              124928

Free inodes:              124615

Inodes per group:         2048

Inode blocks per group:   256

First inode:              11

Inode size:           128

Now, lets see how inodes relate to linking to files.

Hard Links

To create a hard link to a file, you just type 

ln <file-name> <link-name>

If you have a file called locs.txt and you want to make a hard link to it called hard-link-to-locs.txt, you would type

ln locs.txt hard-link-to-locs.txt

Running ls -li on the directory those files are in shows they point to the same inode number.

It is essentially two names pointing to the same underlying file.

Soft Links

To create a soft link, the command is the same, but you add the -s option.

If you have a file called locs2.txt and you want to make a hard link to it called soft-link-to-locs.txt, you would type

ln locs2.txt soft-link-to-locs.txt

Running ls -li on the directory those files are in shows they point to different inode numbers.

A soft link points a new file name to the name of the original file.

ls -li will show an arrow pointing from the soft link to the file name it is linked to:

soft-link-to-locs.txt -> locs2.txt

When listing files, working soft links will show up light blue, and broken ones will show up red by default on a color capable system.

Behavior

Changing the name of a file with a hard link will not break the link, as it is pointing to the inode number, not the file name.

Deleting the file a hard link points to will not break the link either. The hard linked file will just be the only instance of the file left on the system.

Changing the name of a file with a soft link to it will break the link. The soft link will no longer be able to find the file.

Deleting a file with a soft link to it will also break the link. 

Practical Use

I most often see links used for system files in web servers like Apache and Nginx. When you want links, potentially in several locations, to point to one “master” file. You make the changes in the original file, and all links are automatically up to date.

Learn more from the full course

Ubuntu Linux Fundamentals Linux Server Administration Basics

Updated for Ubuntu 20.04 - The Latest! Gain essential skills with Linux Server in this 11 hour Beginner's course.

11:20:56 of on-demand video • Updated October 2022

You will learn what Linux is
Installing Linux
Working at the command line and why the Command Line Interface is so simple yet powerful
Configuring and securing remote access with SSH
Securing your server, ufw, apt update and upgrade
Stopping bad guys with Fail2ban
Installing and securing nginx web server
Managing users and groups
How to use the Linux file system
English [Auto]
In this lesson we'll discuss something we haven't touched on yet linking files instead of making copies of files all over your system. It's sometimes desirable just to have links to one file. This is done with soft links or SIM links or with hard links. The link is similar in concept to a shortcut in Windows. Links are pointers to the actual file. There are two types soft links often referred to as symbolic links or SIM links and hard links. We'll discuss both in this lesson before discussing that though we'll take a look at. I know the numbers before discussing that though we'll take a look at. I know numbers you'll understand why shortly along with the name every file is assigned an index or I note. No no no. It's how the system actually tracks the file. If we type L S minus l i i is to show the I node number. We'll see a leading number before the rest of the information on the file. That number is the AI node number or index number every file on the system has its own unique AI node number. The number of AI nodes on a system is limited but it's very large depending on the file system in use we type D F minus AI. We can see the status of our AI node usage on the system we can see here the only file it's using an appreciable number of AI nodes is this Dev mapper ubuntu server v g root which is part of the logical volume manager or LV M for this system. But it still has plenty of space it's used only 24 percent of the available nodes. Most directories have only used less than 1 percent. You could get more information about a particular mount point with a tuned to FSM command if we want to have a closer look at the dev SDK one mount we would type pseudo tune to F S minus l slash Dev slash SD a one in the world and then we would grep for AI node because that'll give us a lot of additional information. So it tells us file system features ie node count how many are free. I know it's per group blocks per group. First I node and I node size now let's see how I nodes relate to links we have two files here. Low CSO or LOC stat text and locks to dot text to create a hard link. We would type l n file name link name. Now if we do alas minus l I again we can see that locks and the hard link have the same AI node number it's essentially two names pointing to the same underlying file creating a soft link is very similar but we add the minus s options will be l n minus s file name and link name. Now if we do. Minus ally we can see there's a new soft link here. You can see it's light blue. That means it's working. It's not broken and you can see an arrow pointing to the file it's linked to the file name it's linked to and you can see it has a completely new I know no so a soft link essentially links to the name of the file not the eye node number. So how do these behave. Changing the name of the file with a hard link will not break the link as it's pointing to the I know no not the file name we move locks to locks too we can see both files are there both are still referring to the same I know no. And if we cat locks new if we can't locks new DOT text it works if we can't hard link two locks to that also works however the same is not the case for soft links if we cat to file now it works if we change the file name we can see the link now shows as red meaning it's broken because locks to is no longer there we try and cat that file doesn't work the link is broken. Similarly if you delete a file that has a hard link the hard link still works and the contents are still there if you deal if you delete a file that has a soft link it also won't work. How are these used in practical terms. If you have a web server chances are the files that tell the server what pages are active will be some links to another file also system files often use symbols links. That way you can change just an original file and all the links pointing to it are also seeing the new information. I don't have a web server on here but you can see that RC 0 dot D also uses links all these are links to files in it. D So that's it for links hard links and soft links onto the next lesson.