This section provides basic information on the ln
command and its usage.
ln
(link) is one of the commands you can write in the Linux shell.
The most basic usage of the ln
command allows you to create a link file (link-name
) to your current file (file-name
):
$ ln file-name link-name
There are a few additional syntax options you can use:
Syntax | Explanation |
---|---|
ln file-name link-name |
Creates a hard link to the file with the name provided by the user. |
ln file-name |
Creates a hard link in the current directory. The link name will be the same as the file name. |
ln file-name directory |
Creates a hard link in the directory given by the user. The link name will be the same as the file name. |
Note: By default, the
ln
command creates hard links. To create a symbolic link, use the-s
argument.
Typically, ln
does not replace existing files. If you would try to create a link with a link-name
of an existing file, you will get an error:
ln: failed to create hard link 'link.txt': File exists
Use the -f
(force) argument to replace an existing file.
This section provides basic information on hard links.
Links are a method to refer to a file stored on the hard drive. They are a part of a file system that organizes directories and files.
A hard link is an exact copy of an existing file. It shares the same inode, so the information on the file and its attributes (such as type of a file, size, physical address, or ownership).
Note: Hard links do not use extra memory as they do not create a physical copy of the file. They serve as an additional way to access the original file stored in the inode.
Fig.1: Both files point to the same inode
The hard link points directly to the file's inode, not the file itself. Because of that, you can change the original file's contents or its location, and your hard link will still work. You can even remove the original file, and it will be accessible through the hard link. It will not become invalid.
This section provides basic information on symbolic links.
A symbolic link is a special file version that points to another file. It can be compared to the shortcut, popular in Microsoft Windows systems. Unlike a hard link, a symbolic link does not contain the data in the target file. It points to another entry somewhere in the file system. If the file or directory that the symbolic link points to gets deleted, the link becomes invalid.
Fig.2: Symlink file points to the original file, which points to the inode
This procedure provides exercises to practice hard and symbolic links creation.
Prerequisites:
- Access to a Linux shell
Procedure:
- Set up example directories and files.
- Create a hard link to an existing file.
- Create a symbolic link to an existing file.
Result: Hard and symbolic links to the example files are created.
This procedure describes example directories and files creation.
Prerequisites:
- Access to a Linux shell
Procedure:
-
Create a new
practice
directory using themkdir
command.$ mkdir -p /tmp/practice/
Tip: Create your practice files in the temporary (
/tmp/
) directory. The next time your PC boots up, all files and directories will be deleted. -
Create the two example files using the
touch
command.$ touch file1.txt file2.txt
Result: A new directory and two example files are created.
This procedure describes a hard link creation.
Prerequisites:
- Example directory and files are created
Procedure:
- Go to the
practice
directory using thecd
command.$ cd /tmp/practice
- Create a hard link, within the current directory, to an
example1.txt
file using theln
command.$ ln example1.txt link1.txt
- Optional: Check if the hard link was created correctly by checking the inode number, using the
ln -i
command.Expected output: Files have the same inode number.$ ls -i file1.txt link1.txt
522316 file1.txt 522316 link1.txt
Result: A new file with the same inode number is created.
This procedure describes a symbolic link creation.
Prerequisites:
- Example directory and files are created
Procedure:
- Go to the
practice
directory using thecd
command.$ cd /tmp/practice
- Create a symbolic link, within the current directory, to an
example2.txt
file using the `ln -s' command.$ ln -s example2.txt link2.txt
- Optional: Check if the symbolic link was created correctly by checking the inode number using the
readlink
command.Expected output:$ readlink link2.txt
file2.txt
Result: A new symlink file is created.
The inode is a database that uniquely describes a file with its attributes. Every newly created file receives a new inode number.
Command options (arguments) are a list of parameters that can control the command's behavior. Options can add additional actions to a command or change its behavior.
Shell is a command-line interface that directly exposes you to the operating system internals. It interprets your commands and passes the information on what to do with them to the operating system.