Controlling disk space with symbolic links


2006/11/22

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

And then:

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
total 53776
-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
etc.
# 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 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").



Got something to add? Send me email.





(OLDER) <- More Stuff -> (NEWER)    (NEWEST)   

Printer Friendly Version

-> -> Controlling disk space with symbolic links

6 comments



Increase ad revenue 50-250% with Ezoic


More Articles by

Find me on Google+

© Anthony Lawrence







Wed Nov 22 17:06:56 2006: 2636   BigDumbDinosaur


But there's nothing else that would confuse a script or program ordinarily.

Samba is a notable exception. Normally, Samba will not follow symbolic links that point to anywhere outside of the filespace already mapped into shares. If, for example, a SYMLINK to the new filespace was placed into a subdirectory already mapped to Windows as a share, the SYMLINK would be invalid -- Samba would not allow you to follow it. In this sort of situation you may have insert WIDE LINKS = YES in smb.conf in order to get to that storage. Experient carefully.



Thu Nov 23 02:13:27 2006: 2642   drag


It's probably worth mentioning the 'bind' mount option. Since you covered it before...
(link)


I don't have a OS X machine aviable to me right now, but I remember 'Finder' being very finicky about these sort of things. For instance using the commnand line to mount SMB shares would cause Finder to flip out.

If you go to your desktop and make some symbolic links from the bash shell will Finder be happy with that? Also I am curious about other types of special files like named pipes and such.

Also it's worth noting what Windows supports;
Windows supports 3 types of 'linking'; hardLinks, junctions, and symbolic Links

Hardlinks work the same way they do in Unix-land and is supported by NTFS. This allows you to have multiple file names for a single file.

Junctions are like mount points. Unlike unix-land were you can use any directory you have to create specific junctions and this allows you to do things like work around the 26 drive letter limitation or do things similar to 'mount --bind'.
(link) It's worth noting the limitations, some commands like to delete files recursively and if you use a junction to mount a volume and then try to delete that junction you may end up with a suprisingly lot of extra disk space.


Symbolic links are new to NTFS version 5.0 and SMB 2.0. NTFSv5 is aviable on XP-64 (I suppose the AMD64 version and maybe not the IA64) and SMB 2.0 and NTFSv5 for Windows Vista. (SMB2.0 will only be used between vista machines otherwise you fallback to regular smb)

These links are controlled via the mklink command..







Thu May 23 00:33:45 2013: 12080   anonymous

gravatar


Good article. What if you have a situation to want to still use both directories without moving any object to the bigdrive? How would you achieve that using Soft link?



Thu May 23 00:45:03 2013: 12081   TonyLawrence

gravatar


Not sure what you mean. Can you be more specific?



Thu May 23 12:01:23 2013: 12082   anonymous

gravatar


Ok. Let me try again.
/directory_A is the original directory but say 500GB but it's using 95% of the disk capacity.
/directory_B is mounted as a new drive with say 1TB disk capacity.

Now, without moving the content of /directory_A to /directory_B, how easy to use symbolic link to add /directory_B as additonal disk space. That way, it will be 500GB + 1TB disk space in total. Note, that the applications will only see directory_A with no extra effort to point or move objects to /directory_B.






Thu May 23 18:22:49 2013: 12083   TonyLawrence

gravatar


Yes, with union mounts.

See
(link) and (link)

------------------------
Kerio Samepage


Have you tried Searching this site?

Unix/Linux/Mac OS X support by phone, email or on-site: Support Rates

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more.

Contact us





One of the main causes of the fall of the Roman Empire was that, lacking zero, they had no way to indicate successful termination of their C programs. (Robert Firth)





This post tagged: