LN Command - Create Links

https://linuxize.com/post/how-to-create-symbolic-links-in-linux-using-the-ln-command/

There are two types of links in Linux/UNIX systems:

How to Use the ln Command

ln is a command-line utility for creating links between files. By default, the ln command creates hard links. To create a symbolic link, use the -s (--symbolic) option.

The ln command syntax for creating symbolic links is as follows:

ln -s [OPTIONS] FILE LINK

By default, on success, ln doesn’t produce any output and returns zero.

To create a symbolic link to a given file, open your terminal and type:

ln -s source_file symbolic_link

Replace source_file with the name of the existing file for which you want to create the symbolic link and symbolic_link with the name of the symbolic link.

The symbolic_link parameter is optional. If you do not specify the symbolic link, the ln command will create a new link in your current directory:

In the following example, we are creating a symbolic link named my_link.txt to a file named my_file.txt:

ln -s my_file.txt my_link.txt

To verify that the symlink was successfully created, use the ls command:

ls -l my_link.txt

The output will look something like this:

lrwxrwxrwx 1 linuxize users  4 Nov  2 23:03  my_link.txt -> my_file.txt

The l character is a file type flag that represents a symbolic link. The -> symbol shows the file the symlink points to.

The command for creating a symbolic link to a directory is the same as when creating a symbolic link to a file. Specify the directory name as the first parameter and the symlink as the second parameter.

For example, if you want to create a symbolic link from the /mnt/my_drive/movies directory to the ~/my_movies directory you would run:

ln -s /mnt/my_drive/movies ~/my_movies

If you try to create a symbolic link that already exists , the ln command will print an error message.

ln -s my_file.txt my_link.txt
ln: failed to create symbolic link 'my_link.txt': File exists

To overwrite the destination path of the symlink, use the -f (--force) option.

ln -sf my_file.txt my_link.txt

To delete/remove symbolic links use either the unlink or rm command.

The syntax of the unlink is very simple:

Removing a symbolic link using the rm command is the same as when removing a file:

No matter which command you use, when removing a symbolic link not append the / trailing slash at the end of its name.

If you delete or move the source file to a different location, the symbolic file will be left dangling (broken) and should be removed.


Revision #1
Created 23 December 2023 15:12:55 by ColtM
Updated 7 August 2024 23:24:39 by ColtM