Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I understand the link concept in Linux, hard or soft.

Can someone please tell me the real use case?

Why do we need to keep multiple references to the same file? Isn't their management a problem?

If someone by mistake copies a reference instead of making a new reference of the original, I think it can be a mess with manageability aspect.

share|improve this question

migrated from stackoverflow.com Dec 5 '11 at 23:04

This question came from our site for professional and enthusiast programmers.

1  
To address your last statement, managing multiple instances of the same file would be a lot harder than managing a single file/dir with multiple references to it. –  Shawn Chin Dec 5 '11 at 22:53
    
what is the purpose of moving this question from stackoverflow to superuser? i don't know anything about this group. –  P K Dec 5 '11 at 23:30
    
@Praveen This is a computer usage SE - computer usage does not fall under SO's target audience. –  new123456 Dec 6 '11 at 0:06

5 Answers 5

up vote 8 down vote accepted

Any time that you want multiple names for the same thing, you need a link.

Shared libraries in Linux are named with their actual name, like in this example:

lrwxrwxrwx. 1 root root     16 Dec  2 15:24 /usr/lib64/libcurl.so -> libcurl.so.4.2.0
lrwxrwxrwx. 1 root root     16 Dec  2 15:04 /usr/lib64/libcurl.so.4 -> libcurl.so.4.2.0
-rwxr-xr-x. 1 root root 380864 Sep 19 07:04 /usr/lib64/libcurl.so.4.2.0

This is so that programs that want just any version of libcurl open libcurl.so. If they want version 4 they open libcurl.so.4. If they need the very specific version 4.2.0 they can open libcurl.so.4.2.0.

This is also used for programs like /usr/bin/java. Java can be provided by many different sources. It might be the IcedTea OpenJDK. It might be the Sun JRE. Or it might be the IBM version. Using a symlink lets the system point to a default version, while keeping each version in its own directory.

share|improve this answer
    
Great use case. Makes version control binary files a snap. –  Evan Plaice Feb 1 '12 at 18:08

Links provide layer of redirection for a file name and its content. Regard this situation illustrated by Michael Jakl, in an article regarding use of rsync to mimic the behavior of Apple's Time Machine feature.

Hard-Links

share|improve this answer
2  
Side question: what was the tool you used to generate the chart? –  Frank Dec 5 '11 at 23:19
    
@Frank I am not the creator of said image and I may be a bad person for not making explicit attribution to its source from the referenced article by Michael Jakl. As such I don't know how it was created, but will ask. –  David J. Liszewski Dec 6 '11 at 0:11
1  
re: Drawing Program. I contacted Michael. He said: "I used OmniGraffle (sadly, only available on OS X) for that diagram. Since then I've started to use Google Draw for simple diagrams (on Linux, the available options are less than optimal)." -- hope that helps. –  David J. Liszewski Dec 6 '11 at 14:19
    
Thanks for the update! –  Frank Dec 8 '11 at 12:50

There are several uses of symbolic links in Linux, or *nix systems in general. For example, when you want to move the physical file location without breaking the existing or common "well-known" references, you may want to create a symbolic link at the old location. This is useful when you try to upgrade a commonly used tool with a private version, for whatever reason. Some utilities, such as busybox, uses a common binary to serve several needs. In certain Linux distributions, such as OpenWRT, the /bin/bash, /bin/ls, /bin/mv, etc. are all links to /bin/busybox. This binary is capable to behave differently based on the argv[0] used.

share|improve this answer

Real world examples of hard link usage can be found in /bin and other bin directories. Run the command ls -l /bin | grep -v ' 1 ' | sort. This will give you a list of commands with multiple links. Some commands will act differently depending on the name you call them with. (Busybox is an extreme case.) Hard links must be on the same device and don't require additional disk space.

Symbolic links are used heavily in /etc. It enables files located in other places in the hierarchy to be used without duplication. Symbolic links can cross device boundaries, but require additional disk space for the pointer.

share|improve this answer

To simplify backups

Use Case 1: Relocating documents to a separate local or NFS partition

Lets say you like to update your Operating System with each new release. A typical backup approach would be to copy your home folder to a separate partition so you can copy the contents back after the install.

What I typically do is symlink the folders I want to keep backed up to the separate partition. To restore the symlinks, I just run a quick bash script to restore the links after the OS install.

I would just call it backing up your data but there's really no additional 'backup' step. The nice part is if you accidentally pull a noob move like 'rm -rf' your filesystem, rm doesn't follow symlinks so you won't lose your files.

Note: I understand that you can change the location of where /home is mounted using /etc/fstab. The reason I don't use that technique is because I only want to selectively link parts of my user files and leave any accumulated junk behind.

Use Case 2: Using a cross-platform partition (the easy way)

The added benefit to using symlinks (soft links) is that they work across different filesystems.

Some of us don't have the luxury (due to business needs) of being able to use Linux as our only OS so we jump back and fourth a lot. To extend Case 1, what I personally do is use a NTFS partition for my user files and symlink out the various config and user files that I want to pull into Linux. That keeps all of my stuff in one place.

Note: If you didn't already know, you can also create symlinks in Windows using the MKLINK command.

Imagine this. You have a Dropbox account that you use in both Linux and Windows. To keep from having to download/sync two copies of the same files it's as easy to use everything across both OSes as symlinking to the Dropbox folder on the NTFS partition from your /home folder.

Note: To auto-mount a NTFS drive (in Debian based distros, not sure about others) you'll have to add an mount point in /etc/fstab with umask=000 for write privileges.

Use Case 3: To support default and custom folder structures simultaneously

Lets say you are a webdeveloper that is in charge of working on multiple websites.

You got your testing server setup properly in:

/opt/lampp

You could try to create one massive configuration that works across all sites but that will become cumbersome to maintain over time as new sites are added. The more sites you add the more out-of-sync the permission settings will be with the production server.

What happens when you want to drop in the production server config to test for security vulnerabilities or to verify that no server-side scripting errors are being displayed to the users. Do you just add another set of exceptions for that? How confident are you that your testing configuration is the same as the one the other developers on the team are using.

Apache specifically is extremely flexible when it comes to configurations but with flexibility comes complexity; and complexity introduces risk when you're writing code for the web.

Fortunately, there's a much simpler approach. Create a bash script for each website with --testing and --production arguments. Then just configure the arguments to drop the proper symlinks into the htdocs and config folders for the specific configuration. The site could be under version control, on a separate partition, or even on a remote share. It doesn't matter, as long as the symlinks point to the right place.

share|improve this answer
    
very useful information. thanks.. –  P K Feb 2 '12 at 5:03
    
@Praveen No problem. I was also a bit mystified by the real-world applicability of symlinks. Now, I couldn't see myself working without them. For me, understanding symlinks made me more aware of what the standard filesystem hierarchy abstraction really represents. –  Evan Plaice Feb 2 '12 at 22:07

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.