my cheat sheet on linux symbolic links

Home

1 Creating a symoblic link

1.1 Simple form:

ln -s original new-symlink-to-original

Will create a link so that link -> original To delete just the link: rm link

Note that the original file is NOT deleted, only the symbolic link to it is deleted.

If you are in /x/y/z directory:

  • ln -s junk1 junk2

will create a link in /x/y/z/junk2 -> /x/y/z/junk1

pnemonic to help remember the order: it is REVERSE ORDER to how it looks in a directory listing. or that the original comes first so:

  • ln -s junk1 junk2 creates junk2 -> junk1
  • ln -s original NewSymlink

1.2 2nd simplest form (keeping the same name in a different directory)

Assume you have a file called original1 in a directory /x/y/z, so /x/y/z/original1

cd to a target directory that does not contain a file called original1, say, /usr/local/bin Then, the command ln -s /x/y/z//original1 will create a link in /usr/local/bin, namely /usr/local/bin/original1 -> /x/y/z/original1

In the manual pages, i.e. man ln , target means the new symbolic link which in my mind is opposite. Just be ware of that.

ln -s existing-file target-directory
# or
ln -s existing-file new-pointer-to-existing-file
ln -s existing-file symb-link-to-existing-file

so, for me this creates: new-pointer-to-existing-file -> existing file (kind of the reverse order that these were entered into the ln -s command

example:

  • Zintiss-MacBook-Pro-5:~] zintis% ln -s CAN-pricelist.txt pricelist
       [Zintiss-MacBook-Pro-5:~] zintis% lst
    total 207296
    -rw-r--r--@   1 zintis  staff  45580288  4 Jan 10:44 CAN-pricelist.txt
    lrwxr-xr-x    1 zintis  staff        17  4 Jan 10:44 pricelist -> CAN-pricelist.txt
    drwx------+ 133 zintis  staff      4522  4 Jan 10:36 Documents
    drwx------+  22 zintis  staff       748  4 Jan 10:28 Downloads
    
  • ln -s source-file target-directory

Given one or two arguments, ln creates a link to an existing file sourcefile. If targetfile is given, the link has that name; targetfile may also be a directory in which to place the link; otherwise it is placed in the current directory. If only the directory is speci- fied, the link will be made to the last component of sourcefile.

So to get ~/Movies to point to an external drive do the following:

  cd to ~/
# enter the command:
ln -s /Volumes/WD-3TB-perkons/Movies/ Movies

# then still in ~/ directory run:
ls -l and you'll see:
[Zintiss-MacBook-Pro-2:~] zintis% pwd
/Users/zintis
[Zintiss-MacBook-Pro-2:~] zintis% ls -l
.
.
.
drwx------+ 143 zintis  staff      4862 25 Jun 13:31 Documents
drwx------+  41 zintis  staff      1394 28 Jun 13:04 Downloads
drwx------@  58 zintis  staff      1972 13 May 10:38 Library
lrwxr-xr-x    1 zintis  staff        31 30 Jun 13:40 Movies -> /Volumes/WD-3TB-perkons/Movies/
drwx------+   7 zintis  staff       238 31 Mar 22:27 Music
drwxr-xr-x    4 zintis  staff       136 30 Aug  2011 NetApp
drwx------+ 103 zintis  staff      3502  3 May 13:57 Pictures
drwxr-xr-x+   6 zintis  staff       204 22 Aug  2011 Public
.
.
.
mkdir /Users/Shared/Pictures/iPhoto\ Library

sudo ln -s /Users/Shared/Pictures/iPhoto\ Library/ /Users/user1/Pictures/iPhoto\ Library

sudo ln -s /Users/Shared/Pictures/iPhoto\ Library/ /Users/user2/Pictures/iPhoto\ Library

cd /Volumes
[rtp-zperkons-8717:/Volumes] zintis% pwd
/Volumes
    [rtp-zperkons-8717:/Volumes] zintis% ls -la
total 24
drwxrwxrwt@  6 root    admin   204  9 Dec 00:19 .
drwxr-xr-x  33 root    wheel  1190  9 Dec 01:36 ..
-rw-r--r--@  1 zintis  admin  6148 25 Sep 21:07 .DS_Store
lrwxr-xr-x   1 root    admin     1  7 Dec 09:27 320GB-HItachi -> /
drwxrwxrwx   0 root    wheel     0  9 Dec 11:43 MobileBackups
drwxrwxr-x  25 zintis  staff  1326  9 Dec 11:23 WD-3TB-perkons
[rtp-zperkons-8717:/Volumes] zintis% ln -s WD-3TB-perkons/ WD-1TB-partition
[rtp-zperkons-8717:/Volumes] zintis% ls -la
total 32
drwxrwxrwt@  7 root    admin   238  9 Dec 12:04 .
drwxr-xr-x  33 root    wheel  1190  9 Dec 01:36 ..
-rw-r--r--@  1 zintis  admin  6148  9 Dec 12:04 .DS_Store
lrwxr-xr-x   1 root    admin     1  7 Dec 09:27 320GB-HItachi -> /
drwxrwxrwx   0 root    wheel     0  9 Dec 11:43 MobileBackups
lrwxr-xr-x   1 zintis  admin    15  9 Dec 12:04 WD-1TB-partition -> WD-3TB-perkons/
drwxrwxr-x  25 zintis  staff  1326  9 Dec 11:23 WD-3TB-perkons
[rtp-zperkons-8717:/Volumes] zintis% 

From man page:

SYNOPSIS
   ln [-Ffhinsv] source_file [target_file]
   ln [-Ffhinsv] source_file ... target_dir
   link source_file target_file

1.3 Home