Signup/Sign In

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 -s with ln command.

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.

Options Description
--backup[=CONTROL] make a backup of each existing destination file
-b like --backup but does not accept an argument
-f, --force remove existing destination files
-i, --interactive prompt whether to remove destinations
-L, --logical dereference TARGETs that are symbolic links
-s, --symbolic make symbolic links instead of hard links
-t, -target-directory=Directory specify the DIRECTORY in which to create the links
-T, --no-target-directory treat LINK_NAME as a normal file always
-v, --verbose print name of each linked file
-d, -F, --directory allow the superuser to attempt to hard link directories (note: will probably fail due to system restrictions, even for the superuser
-S, --suffix=SUFFIX override the usual backup suffix
--help display help and exit
--version 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.

linux terminal: create links

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.

screenshot of ln command in linux terminal

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.

create symbolic links with ln commands in linux

Conclusion

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.



About the author:
Pradeep has expertise in Linux, Go, Nginx, Apache, CyberSecurity, AppSec and various other technical areas. He has contributed to numerous publications and websites, providing his readers with insightful and informative content.