How to create Links Between Files (ln command)
In Linux, If we want to create shortcuts of files/programs, we use
ln command. The
ln (links) command is used to create links between files in the Linux/Unix operating system.
There are two types of links-
- Soft Link or Symbolic Links
- Hard Links
If we use the
ln command without any option then the link is a hard link for the soft links we have to use the option
The general syntax of the ln command
ln [OPTION]... [-T] TARGET LINK_NAME
ln [OPTION]... TARGET
ln [OPTION]... TARGET... DIRECTORY
ln [OPTION]... -t DIRECTORY TARGET...
Brief description of options available with the 'ln' command.
|make a backup of each existing destination file
|like --backup but does not accept an argument
|remove existing destination files
|prompt whether to remove destinations
|dereference TARGETs that are symbolic links
|make symbolic links instead of hard links
|specify the DIRECTORY in which to create the links
|treat LINK_NAME as a normal file always
|print name of each linked file
|allow the superuser to attempt to hard link directories (note: will probably fail due to system restrictions, even for the superuser
|override the usual backup suffix
|display help and exit
|output version information and exit
Example: Creating a link file in the same directory.
After executing this command a link file is created that is a hard link because by default a link is a hard link.
Example: Creating a soft link using
-s option ln command.
In this example, using -s option with ln command soft link is created. If we delete 'textfile.txt' then 'l2.txt' won't open.
Example: Creating a link to a backup file using
-b option with ln command.
In this example,
textfile.txt~ backup file available in 'shadow' folder here
~ sign denotes that this file is the backup file.
In this tutorial, we covered creating links between files using the ln (links) command with available options in the ln command. Also using examples it is explained how to create soft links. By default, the link is a hard link for the soft links we use the option -s.