Little Girl's Mostly Linux Blog

HardAndSoftLinks

Create hard and soft links

 

This page was last updated on May 31, 2012.

 
Inodes are unique numbers used by UNIX-style file systems to reference file system objects (files, directories, and file system links). Hard links are file names that reference inodes. Soft links are file names that reference hard links.

About hard links

  • A hard link is a file name that links to an inode.
  • You can only create hard links to files.
  • Hard links must be on the same file system as their inodes.
  • An inode can have more than one hard link.
  • If a hard link’s data is changed, all its other hard links will have immediate access to the change.
  • If an inode has more than one hard link, deleting one of the hard links will not delete the inode or affect the other hard links.
  • You can rename a hard link without interfering with its reference to an inode.

Create a hard link to a file

Replace /source in this example with the (full path and) name of the file you wish to use as the source and replace /destination with the (full path and) name of the file you wish to use as a hard link:

ln /source /destination

This example created the /destination file, which links to the same inode as the /source file.

About soft links

  • Soft links are also known as symbolic links.
  • A soft link is a file name that links to a hard link.
  • You can create soft links to files, directories, and file system links.
  • Soft links do not have to be on the same file system as their hard links.
  • A hard link can have more than one soft link.
  • If a hard link’s data is changed, any or all of its soft links will have immediate access to the change.
  • If a hard link has more than one soft link, deleting one of the soft links will not delete the hard link or affect the other soft links.
  • If a had link has one soft link, deleting the soft link will not delete the hard link.
  • If a hard link is deleted, any or all of its soft links will break.
  • You can rename a soft link without interfering with its reference to a hard link.

Create a soft link to a file

To create a soft link to a file, type this command in a terminal window, replacing source with the (full path and) name of the file you wish to link to, and destination with the (full path and) name of the file you wish to use as a soft link:

ln -s /source /destination

Create a soft link to a directory

To create a soft link to a directory, type this command in a terminal window, replacing source with the (full path and) name of the directory you wish to link to, and destination with the (full path and) name of the file you wish use as a soft link:

ln -s /source /destination

Example of a soft link to a directory

Let’s say you created a folder named common in your /var/www directory for a page named common to be served up by your web server. You would need root access to edit the contents of the /var/www/common folder.

To get around this, you can create a new folder called common in your /home/username directory. Since this folder is in your home directory, you don’t need root access to edit its contents. At this point you could do all your editing in the /home/username/common folder, but you’d still need to copy the changes to /var/www/common every time, which would require root access and be a hassle.

Let’s say you want to be able to do all your page editing in the /home/username/common folder and have any changes you make show up immediately and automatically on the web server. Since your web server software looks in the /var/www/common folder, you would create a symbolic link in var/www/common that points to /home/username/common:

sudo ln -s /home/username/common /var/www/common

Note: Since /var/www is a system directory, it requires root access, so sudo was used in front of the command.

Now all changes in /home/username/common will immediately be reflected in /var/www/common and will show up when the page is loaded in a browser. You also don’t have to worry about root access or copying files over to /var/www/common.

To access the page, load up http://IP/common/, replacing IP with the IP or address of your web server.

Obligatory Happy Ending

And they all lived happily ever after. The end.

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Comment:

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: