File shortcuts in Linux

In Windows and MacOS, you can create handy desktop shortcuts to access files and directories whose paths you never are able to memorize. In MacOS, such a shortcut is called alias. In Linux, shortcuts are called symbolic links (symlinks); they are created using the terminal command ln. Some use cases:

Simplest case: You want to create a symlink to /long/path/to/dir/myfile in the current directory and give the shortcut the same name as the original (myfile), then you type
ln -s /long/path/to/dir/myfile
To create a shortcut to /long/path/to/dir/myfile in the current directory, but set the shortcut's name to myShortcut
ln -s /long/path/to/dir/myfile myShortcut
All together now: To create a shortcut to /long/path/to/dir/myfile in the directory /path, and set the shortcut's name to myShortcut
ln -s /long/path/to/dir/myfile /path/myShortcut

For directories, it works the same way.
Useful tips for using the Terminal command ln:

  • What's the difference between a symlink and a hard link? When specifying the ln -s option, you get a symbolic link, without it, you get a hard link to the file or directory. If you want a Windows-style shortcut it's a symlink, so you must use the -s option. Symlinks are clearly marked and can be deleted without the original being touched. But beware, if someone deletes The One Original(tm), all of the symlinks will irrecoverably break!
    Hard links on the other hand, never break, as long as one of the instances still exists — no matter which. In other words, hard links allow you to have one (original) file in many places under different names. Note, that you will not be able to tell a hard link from its original! They are the same. You edit one, you edit all. You need to keep track of hard links yourself.
    You see, the reason why beginners use symlinks is, symlinks are clearly marked and less confusing. ;-)
  • How to remove a hard link? Use the unlink command on the one of the hard links that you do no want to keep. Be careful when unlinking the last instance of the file, or when unlinking a normal file: Unlinking will quietly remove these files without any warning! (Actually, they are still on you harddrive, but you won't be able to find them anymore...)
  • How to remove a symlink? Symbolic links can be savely deleted with the rm command. If you should get the error message "rm cannot remove directory", do not out of a habit use rm -rf mySymlink/\* on a symlink to a directory, because that will delete the original's content! Instead make sure not to end the symbolic link's name in a slash when removing it. (You may get a slash in the end if you let the shell complete a link to a directory by pressing the tab key).
  • Many people have trouble remembering the order of the arguments to ln. :-) It's actually the same order that cp and mv use: First the source, that's the existing file or directory where you want the link to point to, then the target, that's the shortcut's name. If you mix up source and target, you create a useless link pointing nowhere inside the target directory (or you get an error message). Confused? Just keep one thing in mind: The first argument must be the source — because specifying the file or directory to link to is obviously mandatory. The target, however, is second, because it's optional — if you don't supply it, ln can just use the original file's name and place the shortcut in the current working directory by default.
  • How to tell what's a file and what's a link? The Linux shell usually marks symlinks, either by a color or by adding a character like @ to the file name. (You never type the @ when working with the symlink, though!) If you are not sure, you can get more information about your files by listing them with ls -l. This will clearly mark symlinks with a small letter L at the beginning of the line and a little arrow after the name plus the path where they point to.
    There is no way to tell a hard link from a 'normal' file! Like I said, they are the same thing, one file appears in two different places at the same time.

Here is an example of what symbolic and hard links look like in the wild. Note the L and the arrows identifying myfile and myShortcut as symlinks. Symlinks are usualy suspiciously small (compared to what you'd expect from a real file) and their change/creation dates are the dates of the symlink's change/creation, not the original's!
The files split_personality_one.txt and split_personality_too.txt don't look any different than just_some_normal_file.txt, but they are actually hard links! Psst, tell you a secret: All hard links of a flock have the same size and date... (that's the only hint you'll get from the shell about their identity.)
[jane:~]> ls -l
lrwxrwxrwx 1 jane users 65 Aug 15 15:19 myfile -> /long/path/to/dir/containing/the/original/myfile
lrwxrwxrwx 1 jane users 65 Aug 19 11:59 myShortcut -> /long/path/to/dir/containing/the/other/myfile
-rw-r--r-- 1 jane users 3471 Sep 1 11:22 just_some_normal_file.txt
-rw-r--r-- 2 jane users 763 Aug 24 17:14 split_personality_one.txt
-rw-r--r-- 2 jane users 763 Aug 24 17:14 split_personality_too.txt


And this has existed long before the days of 'linux'. Your write up sounds like its specific to linux.

Posted by another on September 07, 2005 at 09:31 AM CEST #

Hello Ruth. There is a way how to know if file is hard-linked. The number between permissions array and user name/uid tells you count of the links to the file.

$ : > original
$ ls -l original
-rw------- 1 pavuk other 0 Sep 7 18:14 original
$ ln original copy
$ ls -l copy
-rw------- 2 pavuk other 0 Sep 7 18:14 copy
$ ls -l original
-rw------- 2 pavuk other 0 Sep 7 18:14 original
$ rm original
$ ls -l copy
-rw------- 1 pavuk other 6 Sep 7 18:15 copy



Posted by PAVUK on September 07, 2005 at 11:22 AM CEST #

Pavuk, thanks for the tip, I always wondered what that useless "1" was about. :-) To the other poster, sorry, I don't see your point. Every OS has links, it's just that not every beginner knows how to create them in Linux, that's why I write about it.

Posted by Seapegasus on September 09, 2005 at 09:43 AM CEST #

Huh? Did somebody not close his bold tag here? Tsk.

Posted by Seapegasus on September 09, 2005 at 09:45 AM CEST #


Posted by guest on July 26, 2006 at 02:27 PM CEST #

If you do:
> ls -li
   3549651 -rw-r--r--   2 user     www            3 Oct  4 10:31 test
   3549651 -rw-r--r--   2 user     www            3 Oct  4 10:31 test.hlnk

You see another number called the inode. The inode is unique per file, so hard links to the same file will have the same inode.

Posted by guest on October 11, 2006 at 10:54 AM CEST #

Did you know that Windows 2000 and XP (and newer versions that use NTFS) allow hard links? The only way I know how to create them is via cygwin ( which provides a Unix-like environment within Windows. The Windows API probably has a way to do this also, but I'm not familiar with it.

NTFS also has a way to do its own symlinks (not the same as shortcuts): junctions. See the "junction" commandline app from Sysinternals.

Posted by guest on October 11, 2006 at 11:11 AM CEST #

Post a Comment:
  • HTML Syntax: NOT allowed

NetBeans IDE, Java SE and ME, 3D Games, Linux, Mac, Cocoa, Prague, Linguistics.


« July 2016

No bookmarks in folder