Episode 018 - ln command

The ln command is used to create a link between an existing file and a destination, typically newley created, file. Some operating systems may all this creating a shortcut. Recall that Linux treats everything like a file, thus you can create links to files, directories, or even devices.

There are two types of links:

  • Hard Links: A hard like is a connection where two files share the same inode.
  • Symbolic Links: A symblic link is a special file that refers to a different file.

So what is an inode? An inode is a data structure on a file system that “stores information about a file system object (file, device node, socket, pipe, etc), except data content, file name, and location in the file system” (http://en.wikipedia.org/wiki/Inode). A hard link shares the same inode information with the file it is linked to. So in a sense the Hard Link has the same inode, points to the same data but may have a different name. A symbolic link does not share the same inode with the file it is linked to, it is merely a special file that refers to the file it links to and has a different inode.

Hard links behave differently than symbolic links. Because a hard link shares the same innode with the file it is linked to if you delete the original file the hard link will still exist. This behavior is controlled by the count in the innode that holds how many filenames point to the file. Deleting the master file or any hard linked files decrements this count by each file deleted. The data is deleted when this count reaches zero. This is not true with a symbolic link. If you delete the file a symbolic link is linked to then the symbolic link becomes orphaned.

Hard links cannot exist across different file systems or partitions and in most cases you cannot create a hard link to a directory. You can create symbolic links across file systems, partitions, and to directories.

Permission on a symbolic link are always 777 or Read, Write, Execute for owner, owning group, and everyone else. That does not grant those permissions to file the symbolic link references. Any action peformed on a symbolic link is governed by the permission of the file linked to. Therefore you cannot override permissions on the master file by attempting to alter permissions on the symbolic link. Whatever permissions you are trying to set would be passed on to the master file and if you have the permission to alter the master file then they will take effect, otherwise you will be denied this change.

Changes to a hard link also effect the target of the hard link, since they share the same inode. Furthermore, the permissions on the hard link will be the same as the permissions of the target.

Armed with the basic understanding of the differences between hard and symbolic links is critical to using the ln command properly. The basic syntax for the ln command is:

ln target link

By default ln creates hard links:

ln /home/dann/mypoem /home/dann/copies/mypoem

This will create the hard link /home/dann/copies/mypoem to the target file /home/dann/mypoem. Both will share the same inode and point to the same data but they technically have different names as the paths are different.

ln /home/dann/mypoem /home/dann/mypoem

This will fail because the link “mypoem” would be created in the same directory where “mypoem” already exists and would not be a unique file name.

ln /home/dann/mypoem /home/dann/mypoem-link

This example would work because it is creating a new file name “mypoem-link” that is unique in the directory.

To create a symbolic link use the -s or --symbolic switch:

ln -s /home/dann/mypoem /var/www/mypoem

A new file, a symbolic link, will be created in the /var/www directory called mypoem and the target would be /home/dann/mypoem (so long as you have the permission to execute this command). Any reference to the /var/www/mypoem file would be passed to the /home/dann/mypoem so long file permissions on /home/dann/mypoem permit.

If you omit the link name ln will create a hard or symbolic link with the same name of the target in the current directory:

ln -s /home/dann/mypoem

This will create a symbolic link in the current directory called “mypoem” to the target /home/dann/mypoem. Simalarly:

ln -s /home/dann/mypoem /var/www/

Will create a symbolic link called mypoem in /var/www pointing to the target file /home/dann/mypoem.

ln test1 test2 test3 test4 /home/dann/test

In this case four hard links would be created in /home/dann/test called: test1, test2, test3, test4 respectively. When you set the link to a directory it will create a link for each target specified.

You can overwrite an existing link or file with a new link by using the -f or --force option. Thus if there existed a file or link called “mypoem” in /var/www:

ln -sf /home/dann/mypoem2 /var/www/mypoem

Before removing /var/www/mypoem and creating a new symbolic link /var/www/mypoem to /home/dann/mypoem you will be prompted to confirm this change.

This would remove the /var/www/mypoem that existed and create a new symbolic link called /var/www/mypoem with a target of /home/dann/mypoem2. Adding -i or --interactive will prompt before any deletions:

ln -si /home/dann/mypoem2 /var/www/mypoem

The --backup=CONTROL option will backup an existing file before creating a link in a format specified by CONTROL. The value of CONTROL can be:

  • none, off - never make a backup
  • numbered, t - make numbered backups - format is filename~
  • simple, never - make a simple backup - format is filename.~#~
  • existing, nil - numbered backup if numbered backup already exists, otherwise create simple

The -b option is backup=existing.

You can add a specific suffic to the backup files with the -S SUFFIX, or --suffix-SUFFIX, option:

ln -S .bkup mypoem test

Would backup test to test.bkup if it existed before creating the link test to mypoem.

You can attempt to create a symbolic link to a symbolic link but this will merely create a symbolic link to the target of the symbolic link. If a symbolic link “mypoem-link” existed with a target called “mypoem” and you attempted to create as symbolic link from “newlink” to “mypoem-link”:

ln -s mypoem-link newlink

The symbolic link newlink would be created with the target mypoem, the same target of mypoem-link. The same holds true if you try to create a hard link to a symbolic link, it will create a hard link to the target of the symbolic link. But if you pass the -P or --physical switch then this will create a hard link to the symbolic link. Thus if we executed:

ln -p newlink hardlink

a new hard link would be created to the symbolic link “newlink” and would have the same inode as the symbolic link. Both would point to the same target file mypoem.

You can create a relative symbolic link with the -r, or --relative, switch. A relative symbolic link contains a relative path to the target as opposed to a direct path:

ln -rs /home/dann/mypoem /home/dann/copies/mypoem


ls -l /home/dann/copies/mypoem

would show:

mypoem -> ../mypoem

The relative path from the link to the target. In most cases the -r must be using in conjunction with the -s switch.

Finally there is a verbose option to the ln command, -v, which will echo out any files it creates:

ln -v mypoem /var/www/mypoem

Output is:


There are some rather advanced options to the ln command the cover specific conditionals. The -n, or --no-dereference, option treats the target operand specially when it is a symbolic link to a directory. In the case where the target is a symbolic link to a directory it treats this link as a normal file. Therefore if you had a symbolic link to a directory “test”=>”directory”:

ln -s mypoem test

Would create a symbolic link directory/mypoem to mypoem. But:

ln -n mypoem test

Would report an error that test already existed:

ln -nf mypoem test

Would create a new symbolic link test to the target mypoem removing the old symbolic link test to the directory: directory. In this last case the -n swith would treat the target: test, which is a symbolic link, as a regular file and the -f would force the deletion of the file. This is different than:

ln -sf mypoem test

Which would have forced the creation of the symbolic link directory/test if there already was a file called directory/test.

The -T, or --no-target-directory is a bit stronger than the -n switch when the last operand is a directory or a symbolic link to a directory. What this switch does it so force the ln command to treat any directory or symbolic link to a directory as a regular file, and not apply any special conditions as in the case of a directory. In most cases if you try to peform some kind of linking with the operand being a directory it will fail regardless reporting that it cannot overwrite the directory. So in the case sited where “test” is a symbolic link to directory “directory”:

ln -Tsf mypoem test

Would delete test and create a new symbolic link to mypoem called test. It would not create a symbolic link in directory to mypoem called test.

The -t DIRECTORY, or --t-directory=DIRECTORY, option tells ln to use DIRECTORY as the specified directory option. You would use this option in conjunction with a command like xargs:

find . -type f -print0 | xargs --null --no-run-if-empty ln -t /home/dann/what --

In this example the find command would pass all values found to the xargs command which would take each value one at a time and pass it to the ln command, but would not pass null or empty values. The ln command would create links in the “/home/dann/what” directory identified by the -t switch. Without the -t option you would get an error that may look like this:

ln: target ‘./three’ is not a directory


  • man ln
  • info ln
  • http://www.gnu.org/software/coreutils/manual/html_node/Target-directory.html#Target-directory
  • http://en.wikipedia.org/wiki/Inode

If the video is not clear enough view it off the YouTube website and select size 2 or full screen.  Or download the video in Ogg Theora format:

Thank you very much!

This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>