- How to: Linux / UNIX create soft link with ln command
- Two types of links
- How do I create soft link / symbolic link under Unix and Linux?
- How to use the ln command
- Creating Symlink to a directory
- How to overwrite symlinks/Soft link
- How to delete or remove symlinks/soft links
- Getting help about the ln command
- Conclusion
- Explaining Soft Link And Hard Link In Linux With Examples
- What is Soft Link And Hard Link In Linux?
- Creating Soft Link or Symbolic Link
- Creating Hard Link
- So, what is the difference between Hard link and the normal copied file?
- Hard links and soft links in Linux explained
- More Linux resources
- Hard links
- Hard limits
- Soft links
- Hard or soft?
How to: Linux / UNIX create soft link with ln command
Symbolic links can be made to directories as well as to files on different filesystems or different partitions.
Two types of links
- symbolic links (also known as “soft links” or “symlinks”): Refer to a symbolic path indicating the abstract location of another file.
- hard links : Refer to the specific location of physical data.
How do I create soft link / symbolic link under Unix and Linux?
Soft links are created with the ln command. For example, the following would create a soft link named link1 to a file named file1, both in the current directory
$ ln -s file1 link1
To verify new soft link run:
$ ls -l file1 link1
Sample outputs:
- No ads and tracking
- In-depth guides for developers and sysadmins at Opensourceflare✨
- Join my Patreon to support independent content creators and start reading latest guides:
- How to set up Redis sentinel cluster on Ubuntu or Debian Linux
- How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
- How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
- A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
- How to protect Linux against rogue USB devices using USBGuard
Join Patreon ➔
From the above outputs it is clear that a symbolic link named ‘link1’ contains the name of the file named ‘file1’ to which it is linked.
How to use the ln command
So the syntax is as follows to create a symbolic link in Unix or Linux, at the shell prompt:
$ ln -s < source-filename >< symbolic-filename >
For example create a softlink for /webroot/home/httpd/test.com/index.php as /home/vivek/index.php, enter the following command:
$ ln -s /webroot/home/httpd/test.com/index.php /home/vivek/index.php
$ ls -l
Sample outputs:
You can now edit the soft link named /home/vivek/index.php and /webroot/home/httpd/test.com/index.php will get updated:
$ vi /home/vivek/index.php
Your actual file /webroot/home/httpd/test.com/index.php remains on disk even if you deleted the soft link /home/vivek/index.php using the rm command:
$ rm /home/vivek/index.php ## ##
## But original/actual file remains as it is ##
$ ls -l /webroot/home/httpd/test.com/index.php
Creating Symlink to a directory
The syntax remains same:
$ ln -s
For example, create a symbolic link from the /home/lighttpd/http/users/vivek/php/app/ directory to the /app/ directory you would run:
$ ln -s /home/lighttpd/http/users/vivek/php/app/ /app/
Now I can edit files using /app/
$ cd /app/
$ ls -l
$ vi config.php
How to overwrite symlinks/Soft link
Pass the -f to the ln command to overwrite links:
ln -f -s /path/to/my-cool-file.txt link.txt
How to delete or remove symlinks/soft links
Use the rm command to delete a file including symlinks:
rm my-link-name
unlink /app/
rm /home/vivek/index.php
Getting help about the ln command
Type the following ln command:
$ man ln
$ ln —help
ln command option | Description |
---|---|
—backup | make a backup of each existing destination file |
-b | like —backup but does not accept an argument |
-d | allow the superuser to attempt to hard link directories (note: will probably fail due to system restrictions, even for the superuser) |
-f | remove existing destination files |
-i | prompt whether to remove destinations |
-L | dereference TARGETs that are symbolic links |
-n | treat LINK_NAME as a normal file if it is a symbolic link to a directory |
-P | make hard links directly to symbolic links |
-r | create symbolic links relative to link location |
-s | make symbolic links instead of hard links |
-S | override the usual backup suffix |
-t | specify the DIRECTORY in which to create the links |
-T | treat LINK_NAME as a normal file always |
-v | print name of each linked file |
—help | display this help and exit |
—version | output version information and exit |
Conclusion
You learned how to create a symbolic link in Linux using the ln command by passing the -s option. See ln command man page here for more information.
🐧 Get the latest tutorials on Linux, Open Source & DevOps via
Источник
Explaining Soft Link And Hard Link In Linux With Examples
What is Soft Link And Hard Link In Linux?
A symbolic or soft link is an actual link to the original file, whereas a hard link is a mirror copy of the original file. If you delete the original file, the soft link has no value, because it points to a non-existent file.
But in the case of hard link, it is entirely opposite. Even if you delete the original file, the hard link will still has the data of the original file. Because hard link acts as a mirror copy of the original file.
In a nutshell, a soft link
- can cross the file system,
- allows you to link between directories,
- has different inode number and file permissions than original file,
- permissions will not be updated,
- has only the path of the original file, not the contents.
- can’t cross the file system boundaries (i.e. A hardlink can only work on the same filesystem),
- can’t link directories,
- has the same inode number and permissions of original file,
- permissions will be updated if we change the permissions of source file,
- has the actual contents of original file, so that you still can view the contents, even if the original file moved or removed.
Still don’t get it? Well, allow me to show you some practical examples.
Creating Soft Link or Symbolic Link
Let us create an empty directory called «test» .
Change to the «test» directory:
Now, create a new file called source.file with some data as shown below.
Let us view the data of the source.file.
Well, the source.file has been created.
Now, create the a symbolic or soft link to the source.file .
Let us compare the data of both source.file and softlink.file .
As you see in the above output, softlink.file displays the same data as source.file .
Let us check the inodes and permissions of softlink.file and source.file .
Sample output:
As we see in the above screenshot, the inode number (11665731 vs 11665692) and file permissions (lrwxrwxrwx vs -rw-r—r—) are different, even though the softlink.file has same contents as source.file . Hence, it is proved that soft link don’t share the same inode number and permissions of original file.
Now, remove the original file (i.e source.file ) and see what happens.
Check contents of the softlink.file using command:
Sample output:
As you see above, there is no such file or directory called softlink.file after we removed the original file (i.e source.file ).
So, now we understand that soft link is just a link that points to the original file. The softlink is like a shortcut to a file. If you remove the file, the shortcut is useless.
As you already know, if you remove the soft link, the original file will still be available.
Suggested read:
Creating Hard Link
Create a file called source.file with some contents as shown below.
Let us verify the contents of the file.
The source.file has been created now.
Now, let us create the hard link to the source.file as shown below.
Check the contents of hardlink.file :
You see the hardlink.file displays the same data as source.file.
Let us check the inode and permissions of hardlink.file and source.file .
Sample output:
Now, we see that both hardlink.file and source.file have the same the inodes number (11665692) and file permissions (-rw-r—r—). Hence, it is proved that hard link file shares the same inodes number and permissions of original file.
Note: If we change the permissions on source.file, the same permission will be applied to the hardlink.file as well.
Now, remove the original file (i.e source.file ) and see what happens.
Check contents of hardlink.file using command:
Sample output:
As you see above, even if I deleted the source file, I can view contents of the hardlink.file . Hence, it is proved that Hard link shares the same inode number, the permissions and data of the original file.
So, what is the difference between Hard link and the normal copied file?
You might be wondering why would we create a hard link while we can easily copy/paste the original file? Creating a hard link to a file is different than copying it.
If you copy a file, it will just duplicate the content. So if you modify the content of a one file (either original or hard link), it has no effect on the other one.
However if you create a hard link to a file and change the content of either of the files, the change will be be seen on both.
Let us have a look at the source.file.
The source file has a single line that says — Welcome to OSTechNix.
Append a new line, for example «Welcome to Linux» in source.file or hardlink.file .
Now check contents of both files.
See? The changes we just made on source.file are updated in both files. Meaning — both files (source and hard link) synchronizes.
Whatever changes you do in any file will be reflected on other one. If you normally copy/paste the file, you will not see any new changes in other file.
For more details, check the man pages.
In this guide, we have discussed what is soft link and hard link in Linux, how to create softlink and hardlink with example commands and finally we explained the difference between hardlink and normal copied file.
Hope you got a basic idea of how to use symbolic or soft link and hard link in Linux.
Источник
Hard links and soft links in Linux explained
More Linux resources
Have you ever been familiar with something, worked around it, but not fully understood its concepts? I feel like that happens to me more than most people. This is a frustrating feeling, but it is also one that often is easily remedied. Sometimes, it only takes someone explaining the concept in «plain English,» aka layman’s terms. That is the goal of this article. I want to talk about hard links and soft (symbolic) links in the most basic terms possible. You may realize that this concept, which is often a struggle for sysadmins, is quite simple. If nothing else, I take you through the syntax to create these links (which many people find difficult to remember). So let’s get down to it.
Hard links
The concept of a hard link is the most basic we will discuss today. Every file on the Linux filesystem starts with a single hard link. The link is between the filename and the actual data stored on the filesystem. Creating an additional hard link to a file means a few different things. Let’s discuss these.
First, you create a new filename pointing to the exact same data as the old filename. This means that the two filenames, though 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.
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:
The syntax is ln (original file path) (new file path) .
Now when I look at my filesystem, I see both hard links.
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.
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.
The shared inode number is 2730074, meaning these files are identical data.
If you want more information on inodes, read my full article here.
Hard limits
While useful, there are some limitations to what hard links can do. For starters, they can only be created for regular files (not directories or special files). Also, a hard link cannot span multiple filesystems. They only work when the new hard link exists on the same filesystem as the original.
Soft links
Commonly referred to as symbolic links, soft 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 :
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.
All of this sounds great, but there are some drawbacks to using a soft link. The biggest concern is data loss and data confusion. If the original file is deleted, the soft link is broken. This situation is referred to as a dangling soft link. If you were to create a new file with the same name as the original, your dangling soft link is no longer dangling at all. It points to the new file created, whether this was your intention or not, so be sure to keep this in mind.
Hard or soft?
There is no clear answer here. The best link is the type that fits your particular situation. While these concepts can be tricky to remember, the syntax is pretty straightforward, so that is a plus! To keep the two easily separated in your mind, I leave you with this:
- A hard link always points a filename to data on a storage device.
- A soft link always points a filename to another filename, which then points to information on a storage device.
Hopefully, this helps you keep them separated as you work your way through the link types needed to accomplish your daily goals!
Источник