In this tutorial, you will learn how to use hard and symbolic links (also called “soft” links) to make multiple file names reference the same file. In Linux, two common ways to create multiple names that point to the same file are:
- Create a soft link to the file (symbolic link)
- Create a hard link to the file.
Use soft links (symbolic links)
A soft link is a special type of file that points to an existing file or directory. It can be used to link two files on different file systems. Soft links can also point to special files.
of ln -s The command is used to create soft links. Let us consider an example:
In the following example, ln -s The command is used to create a new soft link for the existing file /tmp/file1.txt, which will be named /tmp/file2.txt:
$ echo "Hello from file1" > /tmp/file1.txt $ ln -s /tmp/file1.txt /tmp/file2.txt
Confirm by listing the files:
$ ls -l /tmp/file1.txt /tmp/file2.txt -rw-------. 1 jkmutai jkmutai 17 Feb 4 22:37 /tmp/file1.txt lrwxrwxrwx. 1 jkmutai jkmutai 14 Feb 4 22:38 /tmp/file2.txt -> /tmp/file1.txt
You can see that the first character of the long list of /tmp/file2.txt is Rise Instead of-. This indicates that the file is a soft link, not a regular file. (A kind of d Indicates that the file is a directory. )
Check the contents of the symbolic link file.
$ cat /tmp/file2.txt Hello from file1
For directories, use:
ln -s /dir /dir2
If you delete the original regular file, the soft link will still point to the missing file-“Dangling Soft Links. “
Use hard links
Every file in Linux starts with a single hard link. By creating a new hard link to a file, you can create another name that points to the same data.
The new hard link behaves exactly the same as the original file name. It’s hard to tell the difference between the new hard link and the original name of the file. You use ln Create hard link command-points to another name for an existing file.
$ echo "Hello World from Hard Link" >/tmp/hello1.txt $ ln /tmp/hello1.txt /tmp/hello2.txt
- /tmp/hello1.txt is the path to the existing file
- /tmp/hello2.txt is the hard link you want to create.
Use ls -i ls list file options Inode number. If the inode numbers are the same, the files are hard links to the same data.
$ ls -li /tmp/hello1.txt /tmp/hello2.txt 2591191 -rw-------. 2 jkmutai jkmutai 27 Feb 5 08:16 /tmp/hello1.txt 2591191 -rw-------. 2 jkmutai jkmutai 27 Feb 5 08:16 /tmp/hello2.txt --- Let's add third file and recheck --- $ ln /tmp/hello1.txt /tmp/hello3.txt $ ls -li /tmp/hello1.txt /tmp/hello2.txt /tmp/hello3.txt 2591191 -rw-------. 3 jkmutai jkmutai 27 Feb 5 08:16 /tmp/hello1.txt 2591191 -rw-------. 3 jkmutai jkmutai 27 Feb 5 08:16 /tmp/hello2.txt 2591191 -rw-------. 3 jkmutai jkmutai 27 Feb 5 08:16 /tmp/hello3.txt
All hard links referencing the same file have the same content:
- Number of links
- access permission
- User and group ownership
- Time scale
- document content
Compared with soft links:
$ ls -li /tmp/file1.txt /tmp/file2.txt /tmp/file3.txt 2442008 -rw-------. 1 jkmutai jkmutai 17 Feb 4 22:37 /tmp/file1.txt 2442949 lrwxrwxrwx. 1 jkmutai jkmutai 14 Feb 4 22:38 /tmp/file2.txt -> /tmp/file1.txt 2601927 lrwxrwxrwx. 1 jkmutai jkmutai 14 Feb 5 08:24 /tmp/file3.txt -> /tmp/file1.txt
The main differences between soft and hard links
- Hard links point names to data on the storage device
- Soft links point a name to another name that points to data on the storage device
Enjoy your Linux time and check out our special offers for more information.
Learning Materials Now Available
To purchase Linux books, check:
Best Linux Books for Beginners and Experts
LPIC-1 and LPIC-2 reference books
Read RHCSA and RHCE books