Written by Chris Gregg, with modifications by Nick Troccoli
Click here for a walkthrough video.
(Note: this topic is something that you might find useful, but you may not have to use yourself). Symbolic links let you create a link to a file in another directory. This is useful when you want to have references to a file in multiple places, but all of these references refer to the same actual file. For example, we use symbolic links in assignment starter code to provide you access to the
samples directory, the
sanitycheck tool, and the
submit tool. While it looks like those files and folders are in your assignment folder, in fact they are symbolic links to where those files actually live: in our CS107 directory. This saves space, and also means that if the tools or samples change, we can change them in the CS107 directory and every student will be able to access the changes.
To see symbolic links, use
ls. You can use the
-l flag to get additional information about files. If a file is a symbolic link, it will have an arrow to the actual path where the original file is located:
$ ls -l total 11 drwx------ 3 cgregg operator 2048 Sep 6 12:06 assign0 drwx------ 4 cgregg operator 2048 Sep 6 08:42 assign1 drwx------ 3 cgregg operator 2048 Sep 6 08:43 assign2 drwx------ 2 cgregg operator 2048 Sep 6 09:44 assign3 -rw------- 1 cgregg operator 18 Sep 6 11:54 file1.txt -rw------- 1 cgregg operator 46 Sep 6 11:55 file2.txt lrwxr-xr-x 1 cgregg operator 29 Sep 6 14:49 samples -> /afs/.ir/class/cs107/samples/ $
In this example, the
samples directory is actually located in the cs107 directory, but linked to you so you have direct access to it.
To create a symlink:
ln -s [ACTUAL_FILE] [LINK_LOCATION]
For example, if I wanted to make a symbolic link within my
cs107 folder to all of the class samples, I could do the following:
ln -s /afs/.ir/class/cs107/samples cs107/
ln -s indicates that we are creating a symbolic link.
/afs/.ir/class/cs107/samples is the location of the actual file or directory.
cs107/ says to put the symbolic link (named
samples) in the