WebMasterCampus
WEB DEVELOPER Resources

Linux ln Command

Learn Linux ln Command with examples


Linux ln Command

In Linux we use “ln” to make links between files.

By default, the ln command creates hard links. To create a symbolic link, use the -s (–symbolic) option.

ln Command Syntax

~$   ln [OPTION]... [-T] TARGET LINK_NAME
~$   ln [OPTION]... TARGET
~$   ln [OPTION]... TARGET... DIRECTORY
~$   ln [OPTION]... -t DIRECTORY TARGET...

# By default, the ln command creates hard links.

A soft or symbolic link is something like a shortcut in Windows. It is an indirect pointer to a file or directory. Unlike a hard link, a symbolic link can point to a file or a directory on a different filesystem or partition.

The ln command syntax for creating symbolic links is as follows

~$   ln -s [OPTIONS] FILE LINK

# ln command doesn’t produce any output and returns zero on successful completion.

If both the FILE and LINK are given, ln will create a link from the file specified as the first argument (FILE) to the file specified as the second argument (LINK).

If only one file is given as an argument or the second argument is a dot (.), ln will create a link to that file in the current working directory . The name of the symlink will be the same as the name of the file it points to.

~$  touch email_list

~$  echo "Email List" > email_list

~$  head email_list

~$  ln -s email_list email_list_soft_link

~$  ls -l

~$  mv email_list

~$  head email_list_hard_link
cat: email_list_soft_link: No such file or directory

# if we rename email_list to email_list_1, the soft or symbolic link between email_list and email_list_soft_link will be broken.

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.

Example: if you want to create a symbolic link from the /home/Desktop/pdf directory to the ~/my_pdf directory, run:

~$  ln -s /home/Desktop/pdf ~/my_pdf

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

ln -sf my_test_file.txt my_overwrite_link_file.txt

Hard link is a kind of additional name for an existing file. Hard links are associating two or more file names with the same inode . You can create one or more hard links for a single file. Hard links cannot be created for directories and files on a different filesystem or partition.

Linux ln Command Hard link Diagram

~$  touch email_list

~$  echo "Email List" > email_list

~$  head email_list

~$  ln email_list email_list_hard_link

~$  ls -l

~$  rm email_list 

~$  head email_list_hard_link

# Look what will now happen if email_list is deleted, email_list_hard_link will still points to the same contents, and is thus unaffected.

There are some issues with hard links that can sometimes make them unsuitable.

  • First, The link is identical to the file or directory it points to, it becomes difficult to give a command such as “list all the contents of this directory recursively but ignore any links”. Most modern operating systems don’t allow hard links on directories to prevent endless recursion.
  • Second, drawback of hard links is that they have to be located within the same file system, and most large systems today consist of multiple file systems.

To delete/remove symbolic links use either the unlink.


unlink symlink_file_to_remove

ln Command in Linux (Documentation)

NAME
       ln - make links between files

SYNOPSIS
       ln [OPTION]... [-T] TARGET LINK_NAME
       ln [OPTION]... TARGET
       ln [OPTION]... TARGET... DIRECTORY
       ln [OPTION]... -t DIRECTORY TARGET...

DESCRIPTION
       In  the 1st form, create a link to TARGET with the name LINK_NAME.
       In the 2nd form, create a link to TARGET in the current directory.
       In  the  3rd  and 4th forms, create links to each TARGET in DIREC‐
       TORY.  Create hard links by default, symbolic  links  with  --sym‐
       bolic.  By default, each destination (name of new link) should not
       already exist.  When creating hard links, each TARGET must  exist.
       Symbolic links can hold arbitrary text; if later resolved, a rela‐
       tive link is interpreted in relation to its parent directory.

       Mandatory arguments to long options are mandatory  for  short  op‐
       tions too.

       --backup[=CONTROL]
              make a backup of each existing destination file

       -b     like --backup but does not accept an argument
      
       -d, -F, --directory
              allow  the  superuser  to  attempt to hard link directories
              (note: will probably fail due to system restrictions,  even
              for the superuser)

       -f, --force
              remove existing destination files

       -i, --interactive
              prompt whether to remove destinations

       -L, --logical
              dereference TARGETs that are symbolic links

       -n, --no-dereference
              treat  LINK_NAME  as a normal file if it is a symbolic link
              to a directory

       -P, --physical
              make hard links directly to symbolic links

       -r, --relative
              create symbolic links relative to link location

       -s, --symbolic
              make symbolic links instead of hard links

       -S, --suffix=SUFFIX
             override the usual backup suffix

       -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

       --help display this help and exit

       --version
              output version information and exit

       The backup suffix  is  '~',  unless  set  with  --suffix  or  SIM‐
       PLE_BACKUP_SUFFIX.  The version control method may be selected via
       the --backup option or  through  the  VERSION_CONTROL  environment
       variable.  Here are the values:

       none, off
              never make backups (even if --backup is given)

       numbered, t
              make numbered backups

       existing, nil
              numbered if numbered backups exist, simple otherwise

       simple, never
              always make simple backups

       Using  -s ignores -L and -P.  Otherwise, the last option specified
       controls behavior when a TARGET is a symbolic link, defaulting  to
       -P.
Created with love and passion.