- linux ln command
- Two types of linking files and directories
- What is a soft link?
- What is a hard link?
- Creating soft links (symlinks) with ln
- i-nodes
- See also
- Ln Command: How to Create Symbolic Links in Linux
- Ln Command to Create Symbolic Links
- Create a Symbolic Link to Linux Directory
- Force Overwrite Symbolic Links
- Deleting or Removing Links
- Soft Links vs Hard Links
- Soft (Symbolic) Links
- Hard Links
linux ln command
ln command is a Unix command for linking files or directories to each other. Essentially, it creates new files with the names you specify, and refer them to already existing files or directories. When you run any Unix command against a symlink, it is first resolved (the original file it points to is confirmed) and the Unix command works with that file to produce desired outcome.
Two types of linking files and directories
There are two common approaches to link a file or directory in Unix: soft linking and hard linking. Soft links are also called symlinks (symbolic links).
What is a soft link?
Soft link (also referred to as symlink – short for symbolic link) is a special type of file in Unix, which references another file or directory. Symlink contains the name for another file and contains no actual data. To most commands, symlinks look like a regular file, but all the operations (like reading from a file) are referred to the file the symlink points to. When you remove a soft link, you simply remove one of the pointers to the real file. When you remove the original file a soft link points to, your data is lost. Even though your soft link will still exist, it will be pointing to the non-existent file and will therefore be useless (it will probably have to be removed as well).
What is a hard link?
Hard link is a pointer to physical data. Effectively, all standard files are hard links, because they ultimately create an association between a file name and a physical data which corresponds to each file. In Unix, you can create as many hard links to a file as you like, and there is even a special counter for such references. When you’re using the long format of an ls command, you can see this counter. When you remove a hard link, you decrease this link counter for a data on your storage. If you remove the original file, the data will not be lost as long as there’s at least one hard link pointing to it.
Creating soft links (symlinks) with ln
Let’s start with a really simple example. We create a text file, and then use soft link to reference it. This shows how the file is created. It’s called file1, and has a line of text data in it which we confirm using cat command:
ubuntu$ echo "Text file #1" > file1 ubuntu$ cat file1 Text file #1
Now let’s use ln command to create a soft link. The newly created symlink will be a file called file2, and ls command will show you that it points to file1:
ubuntu$ ls -l file2 lrwxrwxrwx 1 root root 5 Mar 31 03:54 file2 -> file1
If you try accessing the file2, you will ultimately access file1, that’s why the following example shows you the contents of file1:
ubuntu$ cat file2 Text file #1
Now, if you remove file1, this will make file2 symlink invalid, and any attempts to use it will return a “file not found” type of error:
ubuntu$ rm file1 ubuntu$ cat file2 cat: file2: No such file or directory
Creating hard links with ln Now, let’s look at creation of hard links in Unix. For this example, we’ll recreate the file1:
ubuntu$ echo "Text file #1" > file1 ubuntu$ cat file1 Text file #1
If you use ls to look at file1, you can see that the link counter (second field from the left) is set to 1 – which means that there is only one file name pointing to the data with our “Text file #1” text:
ubuntu$ ls -l file1 -rw-r--r-- 1 root root 13 Mar 31 06:18 file1
And now we use ln command to create a hard link called file3, which points to the same data as file1:
ubuntu$ ls -l file1 file3 -rw-r--r-- 2 root root 13 Mar 31 06:18 file1 -rw-r--r-- 2 root root 13 Mar 31 06:18 file3
Notice, how the file1 and file3 files look like absolutely normal files, and there’s nothing showing a logical link between them. To confirm that both filenames are actually referring to the same area on the disk, you can use -i option for the ls command, which will show you an i-node value.
i-nodes
i-nodes are data structures of a filesystem used to store all the important properties of each file: size, owner’s user id and group id, access permission and more. The important thing is that each named data area on your disk must have an inode, and when you create a new data file this means creating an i-node. But when you’re using hard links, you’re effectively creating filesystem directory entry, which references an already existing data, so the hard link gets the same i-node number pointing to the same data.
ubuntu$ ls -il file1 file3 655566 -rw-r--r-- 2 root root 13 Mar 31 06:18 file1 655566 -rw-r--r-- 2 root root 13 Mar 31 06:18 file3
The first number in each line of the output is the i-node number, and since we’re referencing the same data, the i-node numbers are also the same.
See also
- what is ln
- chmod vs chown
- basic Unix commands
- lrwxrwxrwx
- Unix symlink example
- Unix Commands
- hard-link in unix
- ls command
- Show what symlink points to
Ln Command: How to Create Symbolic Links in Linux
A link creates a reference to a file or a folder. Symbolic links are used in Linux for managing and collating files.
In this guide, learn how to use the ln command to create symbolic links in Linux.
- A system running Linux
- Access to a terminal window / command line (Activities >Search> type Terminal)
- (optional) A user account with sudo or root privileges (needed to access certain protected files and directories)
Ln Command to Create Symbolic Links
To use the ln command, open a terminal window and enter the command with the following format:
- By default, the ln command creates a hard link.
- Use the -s option to create a soft (symbolic) link.
- The -f option will force the command to overwrite a file that already exists.
- Source is the file or directory being linked to.
- Destination is the location to save the link – if this is left blank, the symlink is stored in the current working directory.
For example, create a symbolic link with:
ln -s test_file.txt link_file.txt
This creates a symbolic link (link_file.txt) that points to the test_file.txt.
To verify whether the symlink has been created, use the ls command:
Create a Symbolic Link to Linux Directory
A symbolic link can refer to a directory. To create a symbolic link to a directory in Linux:
ln -s /mnt/external_drive/stock_photos ~/stock_photos
This example creates a symbolic link named stock_photos in the home (~/) directory. The link refers to the stock_photos directory on an external_drive.
Note: If the system has a connection to another computer, such as a corporate network or a remote server, symlinks can be linked to resources on those remote systems.
Force Overwrite Symbolic Links
You might receive an error message as displayed in the image below:
The error message means that there’s already a file in the destination named link_file.txt. Use the -f option to force the system to overwrite the destination link:
ln -sf test_file.txt link_file.txt
Note: Using the -f option will permanently delete the existing file.
Deleting or Removing Links
If the original file is moved, deleted, or becomes unavailable (such as a server going offline), the link will be unusable. To remove a symbolic link, use either the rm (remove) or unlink command:
rm link_file.txt unlink link_file.txt
Soft Links vs Hard Links
The ln command can be used to create two different kinds of links:
Soft (Symbolic) Links
A soft link, sometimes called a symbolic link or symlink, points to the location or path of the original file. It works like a hyperlink on the internet.
Here are a few important aspects of a soft link:
- If the symbolic link file is deleted, the original data remains.
- If the original file is moved or deleted, the symbolic link won’t work.
- A soft link can refer to a file on a different file system.
- Soft links are often used to quickly access a frequently-used file without typing the whole location.
Hard Links
When a file is stored on a hard drive, several things happen:
- The data is physically written to the disk.
- A reference file, calledinode, is created to point to the location of the data.
- A filename is created to refer to the inode data.
A hard link works by creating another filename that refers to the inode data of the original file. In practice, this is similar to creating a copy of the file.
Here are a few important aspects of hard links:
- If the original file is deleted, the file data can still be accessed through other hard links.
- If the original file is moved, hard links still work.
- A hard link can only refer to a file on the same file system.
- The inode and file data are permanently deleted when the number of hard links is zero.
You should now have a solid understanding of hard and symbolic (soft) links, and how to work with them. Use the ln command to create links and verify using the ls command.