pavement

Ln

From FreeBSDwiki
(Difference between revisions)
Jump to: navigation, search
(Major re-work of the ln command reference with a view to 'n00b' users)
Line 1: Line 1:
Ln is the system command used to create both hard and soft filesystem [[links]].
+
{{System-Commands}}
  
To create a hard link:
+
The '''ln''' command is used to create links between files and directories on UNIX and Unix-like file systems.  It can be used to give the illusion of copying files without the requirement to duplicate the space of really doing so.  This means that a file that has been linked to exists only once, but can be listed under several different directories.
  
ph34r# '''ln /usr/bin/originalfile /usr/home/jimbo/myhardlinktooriginalfile'''
+
Before discussing the types of links that the ln command can create, these being 'hard' links and 'symbolic' links, the following example may help explain the concept.
  
To create a soft link:
+
== Example Use ==
  
ph34r# '''ln -s /usr/bin/originalfile /usr/home/jimbo/mypointertooriginalfile'''
+
A corporate web server exists hosting several domains; www.example.com, downloads.example.com and support.example.com.  The underlying content directories for these sites are seperate from each other as follows:
 +
 
 +
/server/www/
 +
/server/downloads/
 +
/server/support/
 +
 
 +
The marketing team want a consistent look and feel to the three sites but they also want the ability to make changes without having to copy them to the three directories every time.
 +
 
 +
Fortunately the web server is running [[FreeBSD]] Unix with Apache!
 +
 
 +
As the system administrator you tell marketing to update there design changes in the file www/looknfeel.css and put images in www/images/.  You then set-up links to those files in the remaining directories downloads/ and support/ as follows:
 +
 
 +
%ln www/looknfeel.css downloads
 +
%ln www/looknfeel.css support
 +
%
 +
%mkdir downloads/images
 +
%mkdir support/images
 +
%
 +
%ln www/images/* downloads/images
 +
%ln www/images/* support/images
 +
 
 +
Running the above sequence of ln commands establishes links from the www/looknfeel.css file into both the downloads/ and support/ directories.  The file system then gives the illusion of having the same file called 'looknfeel.css' in each directory:
 +
 
 +
%ls -l www/
 +
drwxr-xr-x  2 www  www  512 Apr 21 10:47 images
 +
-rw-r--r--  2 www  www  29 Apr 21 10:40 '''looknfeel.css'''
 +
%
 +
%ls -l downloads/
 +
drwxr-xr-x  2 www  www  512 Apr 21 10:49 images
 +
-rw-r--r--  2 www  www  29 Apr 21 10:40 '''looknfeel.css'''
 +
%
 +
%ls -l support/
 +
total 4
 +
drwxr-xr-x  2 www  www  512 Apr 21 10:49 images
 +
-rw-r--r--  3 www  www  29 Apr 21 10:40 '''looknfeel.css'''
 +
 
 +
Note that the size and date-stamp of the 'looknfeel.css' file is the same for each directory in is listed in.  This is because the links created all point back to the original within the www/ directory.  Therefore any changes make to www/looknfeel.css will be available to the downloads/ and support/ directories imediately.  The same applies to the images directory:
 +
 
 +
%ls -l www/images/
 +
-rw-r--r--  3 www  www  40 Apr 21 10:47 contact.png
 +
-rw-r--r--  3 www  www  86 Apr 21 10:47 corporate_large.jpg
 +
-rw-r--r--  3 www  www  40 Apr 21 10:46 corporate_logo.png
 +
%
 +
%ls -l downloads/images/
 +
-rw-r--r--  3 www  www  40 Apr 21 10:47 contact.png
 +
-rw-r--r--  3 www  www  86 Apr 21 10:47 corporate_large.jpg
 +
-rw-r--r--  3 www  www  40 Apr 21 10:46 corporate_logo.png
 +
%
 +
%ls -l support/images/
 +
-rw-r--r--  3 www  www  40 Apr 21 10:47 contact.png
 +
-rw-r--r--  3 www  www  86 Apr 21 10:47 corporate_large.jpg
 +
-rw-r--r--  3 www  www  40 Apr 21 10:46 corporate_logo.png
 +
 
 +
Once again the size and date-stamps of the image files are the same as those from the original www/images/ directory.
 +
 
 +
While changes made to the files linked from the www/ directory affect those in the other directories, as noted above, it is not limited to editing the 'master' files in the www/ directory.  Someone in marketing could be working on the support website when they make a change to the support/looknfeel.css linked file.  Because this is a link to the original (or 'master') file all they are in fact doing is editing www/looknfeel.css.  Therefore any changes from any directory where there are linked files will always edit the master files in www/.
 +
 
 +
Links such as these are referred to as 'hard links'.
 +
 
 +
== Hard Links ==
 +
 
 +
A ''hard link'' is created when using the '''ln''' without the '-s' option (see 'Symbolic Links' below).  This type of link, as implied by the name, is a physical link at the file system level.
 +
 
 +
When a hard link is established on a file it is the file's unique identity number, known as an '[[inode]]', that is used on the destination's link name.  The inode can be thought of as a serial number assigned to a file or directory as it is created.  It is a pointer to the actual file as it is stored on the physical disk.  This is why it is possible to edit both the original and the link file names and change the same originating file - the operating system asks the file system to update a file and the file system uses the inode to do so.
 +
 
 +
To show this using the file 'looknfeel.css' created in the above example the '[[ls]]' command is used with the '-i' option:
 +
 
 +
%ls -i www
 +
2378798 images          '''2378794''' looknfeel.css
 +
%
 +
%ls -i downloads/
 +
2378801 images          '''2378794''' looknfeel.css
 +
%
 +
%ls -i support
 +
2378802 images          '''2378794''' looknfeel.css
 +
 
 +
It can be seen that the looknfeel.css file has the same inode within each directory where-as the sub-directory images/ is different because they are each unique directories however the image files within these directories share the same inodes also:
 +
 
 +
%ls -i www/images/
 +
'''2378799''' contact.png  '''2378800''' corporate_large.jpg  '''2378797''' corporate_logo.png
 +
%
 +
%ls -i downloads/images/
 +
'''2378799''' contact.png  '''2378800''' corporate_large.jpg  '''2378797''' corporate_logo.png
 +
%
 +
%ls -i support/images/
 +
'''2378799''' contact.png  '''2378800''' corporate_large.jpg  '''2378797''' corporate_logo.png
 +
 
 +
=== Moving Files ===
 +
 
 +
Moving files using the '[[mv]]' command on hard-linked files is possible because the inode remains the same, regardless of wether the original file name was used or one of it's linked file names.  Therefore editing a moved file will affect the contents of the original file.
 +
 
 +
=== Copying Filse ===
 +
 
 +
Copying files using the '[[cp]]' command will duplicate the original file and give the copied file a new inode, regardless of wether the original file name was used or one of it's linked file names.  Therefore editing the new file will not affect the contents of the original file.
 +
 
 +
=== Removing Files ===
 +
 
 +
Remoiving files using the '[[rm]]' command will remove the file specified, regardless of wether the original file name was used or one of it's linked file names.  While it is possible to remove the original file this will not remove the actual contents of it.  This is due to each link being counted against the inode of the file.  Therefore removing the original reduces the link count by one and as such the file exists and is editable using the remaining linked filenames.  Until the final link is removed the file will remain.
 +
 
 +
This property of hard links can be used to great effect with creating a simple backup (though in itself not a true reliable backup).
 +
 
 +
=== Limitations ===
 +
 
 +
Since hard links rely on the file's inode they are limited to the physical [[slice]] (often called a 'partition' in other operating systems) of the file system.  You could not, for example, link a file from the /tmp/ directory to the /var/ directory if they reside on seperate slices.
 +
 
 +
== Symbolic Links ==
 +
 
 +
A ''symbolic link'', sometimes called a ''soft link'' in oposition to ''hard links' or simply ''symlinks'', is created when using the '''ln''' with the '-s' option.  This type of link is similar in functionality to a 'shortcut' in the Microsoft Windows operating system.
 +
 
 +
When a symbolic link to a file is made the ln command creates a small file containomg either the physical or relative path to the file being linked to.  This is where the similatiry with Microsoft's 'shortcuts' is derived.  A symbolic link can be create in one of two ways:
 +
 
 +
=== Relative Path ===
 +
 
 +
%cd /server
 +
%ln -s www/looknfeel.css relative_path
 +
 
 +
This establishes a symbolic link using the relative path, that is, the sub-directory within the current directory.  Therefore the file 'relative_path' simply contains 'www/looknfeel.css'.
 +
 
 +
=== Physical Path ===
 +
 
 +
%cd /server
 +
%ln -s /server/www/looknfeel.css physical_path
 +
 
 +
This establishes a symbolic link using the physical path, that is, the entire directory path from the [[root]] to the file itself.  Therefore the file 'physuical_path' contains '/server/www/liiknfeel.css'.
 +
 
 +
== Symbolic Links (continued) ==
 +
 
 +
There are advantages and disadvantages of using relative and physical paths in the creation of symlinks.  One particular advantage, in contrast to the limitation of hard links, is that they can span slices.  Other issues are determined when using the following commands:
 +
 
 +
=== Moving Files ===
 +
 
 +
Moving files using the '[[mv]]' command on symbolic-linked files is only possible for those with the physical path because the path from the root directory is noted.  Therefore moving a symbolic link with a relative path will result in the loss of the relativity between its original directory and its destination directory, unless the sub-directory it links to is also moved.
 +
 
 +
=== Copying Filse ===
 +
 
 +
Copying files using the '[[cp]]' command will duplicate the symbolic link and not the original file and is only possible for those with the physical path because the path from the root directory is noted.  Therefore copying a symbolic link with a relative path will result in the loss of relativity between its original directory and its destination directory, unless the sub-directory it links to is also copied.
 +
 
 +
=== Removing Files ===
 +
 
 +
Removing files using the '[[rm]]' command will remove the file specified, regardless of wether the original file name was used or one of it's linked file names.  In contrast to hard links if the original file is removed the file no longer exists.  This is due to each link being a reference of the original file's location as a physical or relative path and not its file system inode.  Therefore removing the original will cause loss of the data stored within it but leave the remaining linked filenames.  The linked filenames will no longer be of any use.
 +
 
 +
This property of symbolic links can often be seen of Microsoft Windows where an application is uninstalled but a shortcut to it remains on the desktop.  The physical path is still contained within the link (or shortcut as on Microsoft Windows) despite the original file being removed.
  
 
[[Category:System Commands]]
 
[[Category:System Commands]]

Revision as of 07:31, 21 April 2008

The Mediawiki system (the 'software' that runs this site) capitalises all articles. Please note that commands on most UNIX and Unix-like systems are entered in lower case. As an example the article documenting the Ln command would be issued from the command line as 'ln'.

The ln command is used to create links between files and directories on UNIX and Unix-like file systems. It can be used to give the illusion of copying files without the requirement to duplicate the space of really doing so. This means that a file that has been linked to exists only once, but can be listed under several different directories.

Before discussing the types of links that the ln command can create, these being 'hard' links and 'symbolic' links, the following example may help explain the concept.

Contents

Example Use

A corporate web server exists hosting several domains; www.example.com, downloads.example.com and support.example.com. The underlying content directories for these sites are seperate from each other as follows:

/server/www/
/server/downloads/
/server/support/

The marketing team want a consistent look and feel to the three sites but they also want the ability to make changes without having to copy them to the three directories every time.

Fortunately the web server is running FreeBSD Unix with Apache!

As the system administrator you tell marketing to update there design changes in the file www/looknfeel.css and put images in www/images/. You then set-up links to those files in the remaining directories downloads/ and support/ as follows:

%ln www/looknfeel.css downloads
%ln www/looknfeel.css support
%
%mkdir downloads/images
%mkdir support/images
%
%ln www/images/* downloads/images
%ln www/images/* support/images

Running the above sequence of ln commands establishes links from the www/looknfeel.css file into both the downloads/ and support/ directories. The file system then gives the illusion of having the same file called 'looknfeel.css' in each directory:

%ls -l www/
drwxr-xr-x  2 www  www  512 Apr 21 10:47 images
-rw-r--r--  2 www  www   29 Apr 21 10:40 looknfeel.css
%
%ls -l downloads/
drwxr-xr-x  2 www  www  512 Apr 21 10:49 images
-rw-r--r--  2 www  www   29 Apr 21 10:40 looknfeel.css
%
%ls -l support/
total 4
drwxr-xr-x  2 www  www  512 Apr 21 10:49 images
-rw-r--r--  3 www  www   29 Apr 21 10:40 looknfeel.css

Note that the size and date-stamp of the 'looknfeel.css' file is the same for each directory in is listed in. This is because the links created all point back to the original within the www/ directory. Therefore any changes make to www/looknfeel.css will be available to the downloads/ and support/ directories imediately. The same applies to the images directory:

%ls -l www/images/
-rw-r--r--  3 www  www  40 Apr 21 10:47 contact.png
-rw-r--r--  3 www  www  86 Apr 21 10:47 corporate_large.jpg
-rw-r--r--  3 www  www  40 Apr 21 10:46 corporate_logo.png
%
%ls -l downloads/images/
-rw-r--r--  3 www  www  40 Apr 21 10:47 contact.png
-rw-r--r--  3 www  www  86 Apr 21 10:47 corporate_large.jpg
-rw-r--r--  3 www  www  40 Apr 21 10:46 corporate_logo.png
%
%ls -l support/images/
-rw-r--r--  3 www  www  40 Apr 21 10:47 contact.png
-rw-r--r--  3 www  www  86 Apr 21 10:47 corporate_large.jpg
-rw-r--r--  3 www  www  40 Apr 21 10:46 corporate_logo.png

Once again the size and date-stamps of the image files are the same as those from the original www/images/ directory.

While changes made to the files linked from the www/ directory affect those in the other directories, as noted above, it is not limited to editing the 'master' files in the www/ directory. Someone in marketing could be working on the support website when they make a change to the support/looknfeel.css linked file. Because this is a link to the original (or 'master') file all they are in fact doing is editing www/looknfeel.css. Therefore any changes from any directory where there are linked files will always edit the master files in www/.

Links such as these are referred to as 'hard links'.

Hard Links

A hard link is created when using the ln without the '-s' option (see 'Symbolic Links' below). This type of link, as implied by the name, is a physical link at the file system level.

When a hard link is established on a file it is the file's unique identity number, known as an 'inode', that is used on the destination's link name. The inode can be thought of as a serial number assigned to a file or directory as it is created. It is a pointer to the actual file as it is stored on the physical disk. This is why it is possible to edit both the original and the link file names and change the same originating file - the operating system asks the file system to update a file and the file system uses the inode to do so.

To show this using the file 'looknfeel.css' created in the above example the 'ls' command is used with the '-i' option:

%ls -i www
2378798 images          2378794 looknfeel.css
%
%ls -i downloads/
2378801 images          2378794 looknfeel.css
%
%ls -i support
2378802 images          2378794 looknfeel.css

It can be seen that the looknfeel.css file has the same inode within each directory where-as the sub-directory images/ is different because they are each unique directories however the image files within these directories share the same inodes also:

%ls -i www/images/
2378799 contact.png  2378800 corporate_large.jpg  2378797 corporate_logo.png
%
%ls -i downloads/images/
2378799 contact.png  2378800 corporate_large.jpg  2378797 corporate_logo.png
%
%ls -i support/images/
2378799 contact.png  2378800 corporate_large.jpg  2378797 corporate_logo.png

Moving Files

Moving files using the 'mv' command on hard-linked files is possible because the inode remains the same, regardless of wether the original file name was used or one of it's linked file names. Therefore editing a moved file will affect the contents of the original file.

Copying Filse

Copying files using the 'cp' command will duplicate the original file and give the copied file a new inode, regardless of wether the original file name was used or one of it's linked file names. Therefore editing the new file will not affect the contents of the original file.

Removing Files

Remoiving files using the 'rm' command will remove the file specified, regardless of wether the original file name was used or one of it's linked file names. While it is possible to remove the original file this will not remove the actual contents of it. This is due to each link being counted against the inode of the file. Therefore removing the original reduces the link count by one and as such the file exists and is editable using the remaining linked filenames. Until the final link is removed the file will remain.

This property of hard links can be used to great effect with creating a simple backup (though in itself not a true reliable backup).

Limitations

Since hard links rely on the file's inode they are limited to the physical slice (often called a 'partition' in other operating systems) of the file system. You could not, for example, link a file from the /tmp/ directory to the /var/ directory if they reside on seperate slices.

Symbolic Links

A symbolic link, sometimes called a soft link in oposition to hard links' or simply symlinks, is created when using the ln with the '-s' option. This type of link is similar in functionality to a 'shortcut' in the Microsoft Windows operating system.

When a symbolic link to a file is made the ln command creates a small file containomg either the physical or relative path to the file being linked to. This is where the similatiry with Microsoft's 'shortcuts' is derived. A symbolic link can be create in one of two ways:

Relative Path

%cd /server
%ln -s www/looknfeel.css relative_path

This establishes a symbolic link using the relative path, that is, the sub-directory within the current directory. Therefore the file 'relative_path' simply contains 'www/looknfeel.css'.

Physical Path

%cd /server
%ln -s /server/www/looknfeel.css physical_path

This establishes a symbolic link using the physical path, that is, the entire directory path from the root to the file itself. Therefore the file 'physuical_path' contains '/server/www/liiknfeel.css'.

Symbolic Links (continued)

There are advantages and disadvantages of using relative and physical paths in the creation of symlinks. One particular advantage, in contrast to the limitation of hard links, is that they can span slices. Other issues are determined when using the following commands:

Moving Files

Moving files using the 'mv' command on symbolic-linked files is only possible for those with the physical path because the path from the root directory is noted. Therefore moving a symbolic link with a relative path will result in the loss of the relativity between its original directory and its destination directory, unless the sub-directory it links to is also moved.

Copying Filse

Copying files using the 'cp' command will duplicate the symbolic link and not the original file and is only possible for those with the physical path because the path from the root directory is noted. Therefore copying a symbolic link with a relative path will result in the loss of relativity between its original directory and its destination directory, unless the sub-directory it links to is also copied.

Removing Files

Removing files using the 'rm' command will remove the file specified, regardless of wether the original file name was used or one of it's linked file names. In contrast to hard links if the original file is removed the file no longer exists. This is due to each link being a reference of the original file's location as a physical or relative path and not its file system inode. Therefore removing the original will cause loss of the data stored within it but leave the remaining linked filenames. The linked filenames will no longer be of any use.

This property of symbolic links can often be seen of Microsoft Windows where an application is uninstalled but a shortcut to it remains on the desktop. The physical path is still contained within the link (or shortcut as on Microsoft Windows) despite the original file being removed.

Personal tools