File shortcuts in Linux
By seapegasus on Sep 07, 2005
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/myfilein 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/myfilein the current directory, but set the shortcut's name to
ln -s /long/path/to/dir/myfile myShortcut
All together now: To create a shortcut to
/long/path/to/dir/myfilein the directory
/path, and set the shortcut's name to
ln -s /long/path/to/dir/myfile /path/myShortcut
For directories, it works the same way.
Useful tips for using the Terminal command
- What's the difference between a symlink and a hard link? When specifying the
ln -soption, 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
unlinkcommand 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
rmcommand. 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
mvuse: 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,
lncan 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