Chat Zalo Chat Messenger Phone Number Đăng nhập
Linux ln command - Unix Tutorial

Linux ln command – Unix Tutorial

ln command

Make links and symbolic links between

files or directories. The ln command is a Unix command

for linking files or directories together. Essentially, it creates new files with the names you specify and forwards them to existing files or directories. When you run any Unix command against a symbolic link, it is first resolved (the original file it points to is confirmed) and the Unix command works with that file to produce the desired result.

There are

two common approaches to linking a file or directory in Unix: soft link and hard link. Soft links are also called symbolic links (symbolic links).

What is a soft link?

Soft link (also known as symbolic link) is a special type of file in Unix, which refers to another file or directory. Symlink contains the name of another file and contains no actual data. To most commands, symbolic links look like a normal file, but all operations (such as reading a file) refer to the file that the symbolic link points to.

When you delete a soft link, you simply remove one of the pointers to the actual file. When you remove the original file that a soft link points to, its data is lost. Even though your soft link will still exist, it will point to the non-existent file and will therefore be useless (it will probably have to be deleted as well).

What is a hard link?

The hard link is a pointer to physical data. Indeed, all standard files are hard links, because they ultimately create an association between a file name and a physical piece of data that corresponds to each file.

On Unix, you can create as many hard links to a file as you want, and there is even a special counter for such references. When you use the long format of an ls command, you may see this counter.

When you remove a hard link, you decrease this link counter for a piece of data in your storage. If you delete the original file, the data will not be lost as long as there is at least one hard link pointing to it.

Creating Symlinks with ln

Let’s start with a really simple example. We create a text file and then use a soft link to reference it.

This shows how the file is created. It’s called file1, and it has a line of text data that we confirm using the cat command:

Now let’s use the ln command to create a smooth link. The newly created symbolic link will be

a file called file2, and the ls command will show you that it points to file1: If you try to access file2, you will eventually access file1,

that’s why the following example shows you the contents of

file1: Now, if you delete file1, this will make file2 symlink invalid, and any attempt to use it will return a “file not found” type of error:

Creating hard links

with ln Now, let’s look at creating hard links on Unix. For this example, we’ll re-create

file1: If you use

ls to look at file1, you can see that the link counter (second field from left) is set to 1, which means that there is only one filename pointing to the data with our text “Text

file #1″:

And now we use the ln command to create a hard link called file3, which points to the same data as File1

:

If we use the ls command once again, you can see that the link counter has been increased and is now 2:

Notice how file1 and file3 files look like absolutely normal files, and there is nothing to show a logical link between them.

To confirm that both file names actually refer to the same area of the disk, You can use the -i option for the ls command, which will show you a value of i-node.

I-nodes are data structures of a file system that are used to store all the important properties of each file: size, owner user ID and group ID, access permission, and more. The important thing is that every 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 use hard links, you are effectively creating a file system directory entry, which references already existing data, so the hard link gets the same i-node node number pointing to the same data.

The first number on each line of the output is the i-node number,

and since we are 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
  • symbolic link example

  • Unix commands
  • hard link in unix
  • ls command
  • Show what the symbolic
  • link points to

Contact US