Controlling Disk Space w/ Symbolic Links
I’ve covered this in other articles here, but when I went searching for something to point a customer at I had a little trouble finding it, so we’ll do it here:
Let’s say you have a system with a few filesystems. One of those systems is getting tight on space, but the other has plenty of room. You want to add a large amount of data, but it has to be in the file system that’s low on space. For example, it’s /home that is low on space, and it’s /home/fred/drwaings that needs more room.
There are several ways to handle that. First you could just move that “drawings” directory to /bigdrive/drawings and leave it at that. Any scripts that use /home/fred/drawings would need to be updated and any users who need access would have to get used to looking on /bigdrive. You could also just move all of fred: ~fred could now be /bigdrive/users/fred or whatever. If only Fred used these, that might be an easy solution: move him over, edit /etc/passwd to change his home directory (or use “usermod” to do it for you). Or..
You could use a symbolic link. This method starts by moving “drawings” to /bigdrive as before:
mv /home/fred/drawings /bigdrive/drawings
ln -s /bigdrive/drawings /home/fred/drawings
For almost all uses, this is entirely transparent: no scripts need to be modified, no users need to be notified or retrained. Any action that accesses /home/fred/drawings will end up accessing /bigdrive/drawings instead. Well, almost any action – “ls -l” does work slightly differently after the move:
# before the link
$ ls -l drawings
-rw-r--r-- 1 apl wheel 17086 Nov 12 09:46 01-04-06_0537.jpg
-rw-r--r-- 1 apl wheel 42590 Nov 12 09:46 12-28-05_0521.jpg
-rw-r--r-- 1 apl wheel 48270 Nov 12 09:46 12-28-05_1038.jpg
-rw-r--r-- 1 apl wheel 39134 Nov 12 09:46 12-28-05_1042.jpg
-rw-r--r-- 1 apl wheel 39138 Nov 12 09:46 12-28-05_1043.jpg
-rw-r--r-- 1 apl wheel 41874 Nov 12 09:46 12-28-05_1044.jpg
-rw-r--r-- 1 apl wheel 41578 Nov 12 09:45 12-28-05_1045.jpg
# after the link
$ ls -l drawings
lrwxr-xr-x 1 apl apl 9 Nov 22 08:10 tfoo -> /bigdrive/drawings
Also, “rm” won’t complain if you just “rm /home/fred/drawings”: it won’t warn about this being a directory – but it doesn’t remove /bigdrive/drawings, just the symbolic link in /home/fred. But watch out: some Unixes have slightly different treatments – see Different symbolic link behaviour.
But there’s nothing else that would confuse a script or program ordinarily. A program can determine that it has crossed a symbolic link if it wants to, but there’s seldom any reason to, so you are unlikely to have any issues.
Symbolic links can be confusing if you are running around with “cd”. The issue is this: if you were sitting in /home/fred and then did “cd drawings”, and you then “cd ..”, where are you? Will “pwd” show /home/fred or /bigdrive?
Well, it depends on your shell and perhaps some environment variables. For example, with Bash, you could “set -P” to make “cd” follow the physical path for symbolic links. You can also just say “cd -P” or “cd -L” to have specific control if necessary.
Mac OS X has symbolic links, but it also has something similar called aliases that have some features symbolic links do not.
Way back when, Microsoft MSDOS had something that was a little bit like a Unix symbolic link. Actually, it was a way to “mount” drives, but they called it “JOIN”. For example, you could “JOIN D: C:\NEWDRIVE”, and then if you “CD \NEWDRIVE” you’d be sitting on D:. Hardly anybody ever used this, but it has been there just the same (there’s a Unix “join” also, but that’s a database like command that merges files based on a common key- see “man join”).
*Originally published at APLawrence.com
A.P. Lawrence provides SCO Unix and Linux consulting services http://www.pcunix.com