Softpanorama

May the source be with you, but remember the KISS principle ;-)
Home Switchboard Unix Administration Red Hat TCP/IP Networks Neoliberalism Toxic Managers
(slightly skeptical) Educational society promoting "Back to basics" movement against IT overcomplexity and  bastardization of classic Unix

ln command

News Recommended Links Recommended articles Reference Solaris UFS File System
Usage of links for simplifying navigation in Unix Advanced Unix filesystem navigation pushd/popd/dirs NCD clones cp command
Filesystems Horror Stories Unix History Humor Etc

Hard and soft links are a great features of Unix. It is a reference (link) in a directory to a file stored in another directory. Links can be of two types: hard and soft.  In case of soft links you can reference not only a file but a directory essentially providing alternative path to the file and subdirectories in it.  The latter is extremely important for simplifying complex filesystem navigation. About usage of symbolic links for filesystem navigation see:

There might be multiple links to a file. Links eliminate redundancy because you do not need to store multiple copies of a file.

Links are of two types: hard and soft (also known as symbolic).

To create a symbolic link, you must use the -s  option with the ln  command. Files that are soft linked contain an l  symbol at the first bit of the access permission bits displayed by the ls -l  command, whereas those that are hard linked do not contain the l  symbol. A directory is symbolically linked to a file. However, it cannot be hard linked.

It is obvious that no file exists with a link count less than one.  Relative pathnames .  or ..  are nothing but links for the current directory and its parent directory.  These are present in every directory: any directory stores the two links ., ..  and the Inode  numbers of the files. They can be listed by the ls -lia  option. A directory must have a minimum of two links. The number of links increases as the number of sub-directories increase. Whenever you issue a command to list the file attributes, it refers to the Inode  block with the Inode  number and the corresponding data is retrieved.

ln command is used to make a link between files. It's format resembles the format of cp command. As we already know a link allows a filename in one directory to point to a file in another directory. Once a link has been made the linkname (alias for a file) can be used, in most circumstances, as if it were a normal file. As a link takes up much less disc space than the file which it is pointing to this it can be used to save space if you need to store duplicate files in many dfferent directories.  It is also useful for simplifying navigation especially in obscure software packages with crazy directory structures like Tivoli. For example you can create a link to the specified target directory from some specialized directory (for example specially created root directory /q -- qucklinks)

 

To create a symbolic link in Unix, at the Unix prompt, enter the following command:

  ln -s source_file myfile

Replace source_file with the name of the existing file for which you want to create the symbolic link (this file can be any existing file or directory across the file systems). Replace myfile with the name of the symbolic link. The ln command then creates the symbolic link. After you've made the symbolic link, you can perform an operation on or execute myfile, just as you could with the source_file. You can use normal file management commands (e.g., cp, rm) on the symbolic link.

Note: If you delete the source file or move it to a different location, your symbolic file will not function properly. You should either delete or move it. If you try to use it for other purposes (e.g., if you try to edit or execute it), the system will send a "file nonexistent" message.

To find out more about symbolic links, you can view the man pages for the ln command. To do this, at the Unix prompt, enter the following command:

  man ln

Mandatory arguments to long options are mandatory for short options too.

This example shows copying three files from a directory into the current working directory.

    [2]%cp ~team/IntroProgs/MoreUltimateAnswer/more*
    [3]%ls -l more*
    -rw-rw-r--   1 mrblobby  mrblobby    632 Sep 21 18:12 moreultimateanswer.adb
    -rw-rw-r--   1 mrblobby  mrblobby   1218 Sep 21 18:19 moreultimatepack.adb
    -rw-rw-r--   1 mrblobby  mrblobby    784 Sep 21 18:16 moreultimatepack.ads

The three files take a total of 2634 bytes. The equivalent ln commands would be:

    [2]%ln -s ~team/IntroProgs/MoreUltimateAnswer/moreultimateanswer.adb .
    [3]%ln -s ~team/IntroProgs/MoreUltimateAnswer/moreultimatepack.adb .
    [4]%ln -s ~team/IntroProgs/MoreUltimateAnswer/moreultimatepack.adb .
    [5]%ls -l
    lrwxrwxrwx   1  mrblobby  mrblobby     35 Sep 22 08:50 moreultimateanswer.adb ->
                     /users/team/IntroProgs/MorUltimateAnswer/moreultimateanswer.adb
    lrwxrwxrwx   1  mrblobby  mrblobby     37 Sep 22 08:49 moreultimatepack.adb ->                       /users/team/IntroProgs/MorUltimateAnswer/moreultimatepack.adb
    lrwxrwxrwx   1   mrblobby  mrblobby    37 Sep 22 08:50 moreultimatepack.ads ->
                     /users/team/IntroProgs/MorUltimateAnswer/moreultimatepack.ads

The ln command now indicates in the first character on the line that this entry is a link. The filename is followed by an arrow ( -> ) and the full path name of the file which it is pointing to. The total size of the three links is 109 bytes. Although mrblooby is shown as owning these files, he only owns the links, and these permissions do not relate to the file itself.

The -s option allows a symbolic link to be made, rather than a phsical link. This allows a link to be made between two file systems. As a symbolic link can be made whenever a non-symbolic link can be made, but a non-symbolic link link cannot always be made when a symbolic link can be made, a symbolic link should always be specified.

The usage of the link command is.

   ln -s ActualFilename LinkFileName

Where -s indicates a symbolic link. ActualFilename is the name of the file which is to be linked to and LinkFileName is the name by which the file should be known.


ln will complain if:

Usage of links for simplifying navigation in Unix

In many flavours of Unix, espesially in HP-UX and AIX system files are located in strange places, which create a lot discomfort for sysadmins who are used to Linux or Solaris. You can minimize those creating symbolic links to target files in location that you used to.

As for directories lining to directores is not a good idea, but still you can create special root directory, called, say, hub and put symbolic links to directories into it. It is important never run recursive operation via this directory as you can affect files that you never intended to.


Top Visited
Switchboard
Latest
Past week
Past month

NEWS CONTENTS

Old News ;-)

[Nov 03, 2018] David Both

/n/n
/n
Jun 22, 2017 | opensource.com
/n .../n/n

The long listing of the /lib64 directory above shows that the first character in the/n filemode is the letter "l," which means that each is a soft or symbolic link.

Hard/n links/n/n

In An introduction to Linux's/n EXT4 filesystem , I discussed the fact that each file has one inode that contains/n information about that file, including the location of the data belonging to that file./n Figure 2 in that/n article shows a single directory entry that points to the inode. Every file must have at least/n one directory entry that points to the inode that describes the file. The directory entry is a/n hard link, thus every file has at least one hard link.

/n/n

In Figure 1 below, multiple directory entries point to a single inode. These are all hard/n links. I have abbreviated the locations of three of the directory entries using the tilde ( ~ )/n convention for the home directory, so that ~ is equivalent to /home/user in this example. Note/n that the fourth directory entry is in a completely different directory, /home/shared , which/n might be a location for sharing files between users of the computer.

/n/n

fig1directory_entries.png/n Figure 1

/n/n

Hard links are limited to files contained within a single filesystem. "Filesystem" is used/n here in the sense of a partition or logical volume (LV) that is mounted on a specified mount/n point, in this case /home . This is because inode numbers are unique only within each/n filesystem, and a different filesystem, for example, /var or /opt , will have inodes with the/n same number as the inode for our file.

/n/n

Because all the hard links point to the single inode that contains the metadata about the/n file, all of these attributes are part of the file, such as ownerships, permissions, and the/n total number of hard links to the inode, and cannot be different for each hard link. It is one/n file with one set of attributes. The only attribute that can be different is the file name,/n which is not contained in the inode. Hard links to a single file/inode located in the same/n directory must have different names, due to the fact that there can be no duplicate file names/n within a single directory.

/n/n

The number of hard links for a file is displayed with the ls -l command. If you want to/n display the actual inode numbers, the command ls -li does that.

Symbolic (soft) links/n/n

The difference between a hard link and a soft link, also known as a symbolic link (or/n symlink), is that, while hard links point directly to the inode belonging to the file, soft/n links point to a directory entry, i.e., one of the hard links. Because soft links point to a/n hard link for the file and not the inode, they are not dependent upon the inode number and can/n work across filesystems, spanning partitions and LVs.

/n/n

The downside to this is: If the hard link to which the symlink points is deleted or renamed,/n the symlink is broken. The symlink is still there, but it points to a hard link that no longer/n exists. Fortunately, the ls command highlights broken links with flashing white text on a red/n background in a long listing.

Lab project: experimenting with links/n/n

I think the easiest way to understand the use of and differences between hard and soft links/n is with a lab project that you can do. This project should be done in an empty directory as a/n non-root user . I created the ~/temp directory for this project, and you should, too./n It creates a safe place to do the project and provides a new, empty directory to work in so/n that only files associated with this project will be located there.

Initial setup/n/n

First, create the temporary directory in which you will perform the tasks needed for this/n project. Ensure that the present working directory (PWD) is your home directory, then enter the/n following command.

/n
/nmkdir temp/n
/n/n

Change into ~/temp to make it the PWD with this command.

/n
/ncd temp/n
/n/n

To get started, we need to create a file we can link to. The following command does that and/n provides some content as well.

/n
/ndu -h > main.file.txt/n
/n/n

Use the ls -l long list to verify that the file was created correctly. It should look/n similar to my results. Note that the file size is only 7 bytes, but yours may vary by a byte or/n two.

[ dboth @ david temp ] $ ls -l
/n total 4
/n -rw-rw-r-- 1 dboth dboth 7 Jun 13 07: 34 main.file.txt/n/n

Notice the number "1" following the file mode in the listing. That number represents the/n number of hard links that exist for the file. For now, it should be 1 because we have not/n created any additional links to our test file.

Experimenting with hard links/n/n

Hard links create a new directory entry pointing to the same inode, so when hard links are/n added to a file, you will see the number of links increase. Ensure that the PWD is still ~/temp/n . Create a hard link to the file main.file.txt , then do another long list of the/n directory.

[ dboth @ david temp ] $ ln main.file.txt link1.file.txt
/n [ dboth @ david temp ] $ ls -l
/n total 8
/n -rw-rw-r-- 2 dboth dboth 7 Jun 13 07: 34 link1.file.txt
/n -rw-rw-r-- 2 dboth dboth 7 Jun 13 07: 34 main.file.txt/n/n

Notice that both files have two links and are exactly the same size. The date stamp is also/n the same. This is really one file with one inode and two links, i.e., directory entries to it./n Create a second hard link to this file and list the directory contents. You can create the link/n to either of the existing ones: link1.file.txt or main.file.txt .

[ dboth @ david temp ] $/n ln link1.file.txt link2.file.txt ; ls -l
/n total 16
/n -rw-rw-r-- 3 dboth dboth 7 Jun 13 07: 34 link1.file.txt
/n -rw-rw-r-- 3 dboth dboth 7 Jun 13 07: 34 link2.file.txt
/n -rw-rw-r-- 3 dboth dboth 7 Jun 13 07: 34 main.file.txt/n/n

Notice that each new hard link in this directory must have a different name because two/n files -- really directory entries -- cannot have the same name within the same directory. Try/n to create another link with a target name the same as one of the existing ones.

[ dboth @/n david temp ] $ ln main.file.txt link2.file.txt
/n ln: failed to create hard link 'link2.file.txt' : File exists/n/n

Clearly that does not work, because link2.file.txt already exists. So far, we have created/n only hard links in the same directory. So, create a link in your home directory, the parent of/n the temp directory in which we have been working so far.

[ dboth @ david temp ] $ ln/n main.file.txt .. / main.file.txt ; ls -l .. / main *
/n -rw-rw-r-- 4 dboth dboth 7 Jun 13 07: 34 main.file.txt/n/n

The ls command in the above listing shows that the main.file.txt file does exist in the home/n directory with the same name as the file in the temp directory. Of course, these are not/n different files; they are the same file with multiple links -- directory entries -- to the same/n inode. To help illustrate the next point, add a file that is not a link.

[ dboth @ david/n temp ] $ touch unlinked.file ; ls -l
/n total 12
/n -rw-rw-r-- 4 dboth dboth 7 Jun 13 07: 34 link1.file.txt
/n -rw-rw-r-- 4 dboth dboth 7 Jun 13 07: 34 link2.file.txt
/n -rw-rw-r-- 4 dboth dboth 7 Jun 13 07: 34 main.file.txt
/n -rw-rw-r-- 1 dboth dboth 0 Jun 14 08: 18 unlinked.file/n/n

Look at the inode number of the hard links and that of the new file using the -i option to/n the ls command.

[ dboth @ david temp ] $ ls -li
/n total 12
/n 657024 -rw-rw-r-- 4 dboth dboth 7 Jun 13 07: 34 link1.file.txt
/n 657024 -rw-rw-r-- 4 dboth dboth 7 Jun 13 07: 34 link2.file.txt
/n 657024 -rw-rw-r-- 4 dboth dboth 7 Jun 13 07: 34 main.file.txt
/n 657863 -rw-rw-r-- 1 dboth dboth 0 Jun 14 08: 18 unlinked.file/n/n

Notice the number 657024 to the left of the file mode in the example above. That is the/n inode number, and all three file links point to the same inode. You can use the -i option to/n view the inode number for the link we created in the home directory as well, and that will also/n show the same value. The inode number of the file that has only one link is different from the/n others. Note that the inode numbers will be different on your system.

/n/n

Let's change the size of one of the hard-linked files.

[ dboth @ david temp ] $ df -h/n > link2.file.txt ; ls -li
/n total 12
/n 657024 -rw-rw-r-- 4 dboth dboth 1157 Jun 14 14 : 14 link1.file.txt
/n 657024 -rw-rw-r-- 4 dboth dboth 1157 Jun 14 14 : 14 link2.file.txt
/n 657024 -rw-rw-r-- 4 dboth dboth 1157 Jun 14 14 : 14 main.file.txt
/n 657863 -rw-rw-r-- 1 dboth dboth 0 Jun 14 08: 18 unlinked.file/n/n

The file size of all the hard-linked files is now larger than before. That is because there/n is really only one file that is linked to by multiple directory entries.

/n/n

I know this next experiment will work on my computer because my /tmp directory is on a/n separate LV. If you have a separate LV or a filesystem on a different partition (if you're not/n using LVs), determine whether or not you have access to that LV or partition. If you don't, you/n can try to insert a USB memory stick and mount it. If one of those options works for you, you/n can do this experiment.

/n/n

Try to create a link to one of the files in your ~/temp directory in /tmp (or wherever your/n different filesystem directory is located).

[ dboth @ david temp ] $ ln link2.file.txt / tmp/n / link3.file.txt
/n ln: failed to create hard link '/tmp/link3.file.txt' = > 'link2.file.txt' :
/n Invalid cross-device link/n/n

Why does this error occur? The reason is each separate mountable filesystem has its own set/n of inode numbers. Simply referring to a file by an inode number across the entire Linux/n directory structure can result in confusion because the same inode number can exist in each/n mounted filesystem.

/n/n

There may be a time when you will want to locate all the hard links that belong to a single/n inode. You can find the inode number using the ls -li command. Then you can use the find/n command to locate all links with that inode number.

[ dboth @ david temp ] $ find . -inum/n 657024
/n . / main.file.txt
/n . / link1.file.txt
/n . / link2.file.txt/n/n

Note that the find command did not find all four of the hard links to this inode because we/n started at the current directory of ~/temp . The find command only finds files in the PWD and/n its subdirectories. To find all the links, we can use the following command, which specifies/n your home directory as the starting place for the search.

[ dboth @ david temp ] $ find ~/n -samefile main.file.txt
/n / home / dboth / temp / main.file.txt
/n / home / dboth / temp / link1.file.txt
/n / home / dboth / temp / link2.file.txt
/n / home / dboth / main.file.txt/n/n

You may see error messages if you do not have permissions as a non-root user. This command/n also uses the -samefile option instead of specifying the inode number. This works the same as/n using the inode number and can be easier if you know the name of one of the hard/n links.

Experimenting with soft links/n/n

As you have just seen, creating hard links is not possible across filesystem boundaries;/n that is, from a filesystem on one LV or partition to a filesystem on another. Soft links are a/n means to answer that problem with hard links. Although they can accomplish the same end, they/n are very different, and knowing these differences is important.

/n/n

Let's start by creating a symlink in our ~/temp directory to start our exploration.

[/n dboth @ david temp ] $ ln -s link2.file.txt link3.file.txt ; ls -li
/n total 12
/n 657024 -rw-rw-r-- 4 dboth dboth 1157 Jun 14 14 : 14 link1.file.txt
/n 657024 -rw-rw-r-- 4 dboth dboth 1157 Jun 14 14 : 14 link2.file.txt
/n 658270 lrwxrwxrwx 1 dboth dboth 14 Jun 14 15 : 21 link3.file.txt - >
/n link2.file.txt
/n 657024 -rw-rw-r-- 4 dboth dboth 1157 Jun 14 14 : 14 main.file.txt
/n 657863 -rw-rw-r-- 1 dboth dboth 0 Jun 14 08: 18 unlinked.file/n/n

The hard links, those that have the inode number 657024 , are unchanged, and the number of/n hard links shown for each has not changed. The newly created symlink has a different inode,/n number 658270 . The soft link named link3.file.txt points to link2.file.txt . Use the cat/n command to display the contents of link3.file.txt . The file mode information for the symlink/n starts with the letter " l " which indicates that this file is actually a symbolic link.

/n/n

The size of the symlink link3.file.txt is only 14 bytes in the example above. That is the/n size of the text link3.file.txt -> link2.file.txt , which is the actual content of the/n directory entry. The directory entry link3.file.txt does not point to an inode; it points to/n another directory entry, which makes it useful for creating links that span file system/n boundaries. So, let's create that link we tried before from the /tmp directory.

[ dboth @/n david temp ] $ ln -s / home / dboth / temp / link2.file.txt
/n / tmp / link3.file.txt ; ls -l / tmp / link *
/n lrwxrwxrwx 1 dboth dboth 31 Jun 14 21 : 53 / tmp / link3.file.txt - >
/n / home / dboth / temp / link2.file.txt Deleting links/n/n

There are some other things that you should consider when you need to delete links or the/n files to which they point.

/n/n

First, let's delete the link main.file.txt . Remember that every directory entry that points/n to an inode is simply a hard link.

[ dboth @ david temp ] $ rm main.file.txt ; ls -li
/n total 8
/n 657024 -rw-rw-r-- 3 dboth dboth 1157 Jun 14 14 : 14 link1.file.txt
/n 657024 -rw-rw-r-- 3 dboth dboth 1157 Jun 14 14 : 14 link2.file.txt
/n 658270 lrwxrwxrwx 1 dboth dboth 14 Jun 14 15 : 21 link3.file.txt - >
/n link2.file.txt
/n 657863 -rw-rw-r-- 1 dboth dboth 0 Jun 14 08: 18 unlinked.file/n/n

The link main.file.txt was the first link created when the file was created. Deleting it now/n still leaves the original file and its data on the hard drive along with all the remaining hard/n links. To delete the file and its data, you would have to delete all the remaining hard/n links.

/n/n

Now delete the link2.file.txt hard link.

[ dboth @ david temp ] $ rm link2.file.txt ; ls/n -li
/n total 8
/n 657024 -rw-rw-r-- 3 dboth dboth 1157 Jun 14 14 : 14 link1.file.txt
/n 658270 lrwxrwxrwx 1 dboth dboth 14 Jun 14 15 : 21 link3.file.txt - >
/n link2.file.txt
/n 657024 -rw-rw-r-- 3 dboth dboth 1157 Jun 14 14 : 14 main.file.txt
/n 657863 -rw-rw-r-- 1 dboth dboth 0 Jun 14 08: 18 unlinked.file/n/n

Notice what happens to the soft link. Deleting the hard link to which the soft link points/n leaves a broken link. On my system, the broken link is highlighted in colors and the target/n hard link is flashing. If the broken link needs to be fixed, you can create another hard link/n in the same directory with the same name as the old one, so long as not all the hard links have/n been deleted. You could also recreate the link itself, with the link maintaining the same name/n but pointing to one of the remaining hard links. Of course, if the soft link is no longer/n needed, it can be deleted with the rm command.

/n/n

The unlink command can also be used to delete files and links. It is very simple and has no/n options, as the rm command does. It does, however, more accurately reflect the underlying/n process of deletion, in that it removes the link -- the directory entry -- to the file being/n deleted.

Final thoughts/n/n

I worked with both types of links for a long time before I began to understand their/n capabilities and idiosyncrasies. It took writing a lab project for a Linux class I taught to/n fully appreciate how links work. This article is a simplification of what I taught in that/n class, and I hope it speeds your learning curve. David Both - David Both is a Linux and/n Open Source advocate who resides in Raleigh, North Carolina. He has been in the IT industry for/n over forty years and taught OS/2 for IBM where he worked for over 20 years. While at IBM, he/n wrote the first training course for the original IBM PC in 1981. He has taught RHCE classes for/n Red Hat and has worked at MCI Worldcom, Cisco, and the State of North Carolina. He has been/n working with Linux and Open Source Software for almost 20 years. dgrb on 23 Jun 2017/n Permalink/n There is a hard link "gotcha" which IMHO is worth mentioning.

/n/n

If you use an editor which makes automatic backups - emacs certainly is one such - then you/n may end up with a new version of the edited file, while the backup is the linked copy, because/n the editor simply renames the file to the backup name (with emacs, test.c would be renamed/n test.c~) and the new version when saved under the old name is no longer linked.

/n/n

Symbolic links avoid this problem, so I tend to use them for source code where required.

/n /n /n
/n/n

[Nov 03, 2018] David Both

/n/n
/n
Nov 03, 2018 | opensource.com
/n /n/n

Feed 161 /n up 4 comments/n Links Image by : Paul Lewin . Modified by Opensource.com./n CC BY-SA 2.0 x/n Get the newsletter

/n/n

Join the 85,000 open source advocates who receive our giveaway alerts and article/n roundups.

/n/n

https://opensource.com/eloqua-embedded-email-capture-block.html?offer_id=70160000000QzXNAA0

An introduction to/n Linux's EXT4 filesystem ; Managing devices in Linux ;/n An/n introduction to Linux filesystems ; and A Linux user's guide to Logical/n Volume Management , I have briefly mentioned an interesting feature of Linux filesystems/n that can make some tasks easier by providing access to files from multiple locations in the/n filesystem directory tree./n/n

There are two types of Linux filesystem links: hard and soft. The difference between the two/n types of links is significant, but both types are used to solve similar problems. They both/n provide multiple directory entries (or references) to a single file, but they do it quite/n differently. Links are powerful and add flexibility to Linux filesystems because everything is a file/n .

/n/n

More Linux resources

/n/n /n/n

I have found, for instance, that some programs required a particular version of a library./n When a library upgrade replaced the old version, the program would crash with an error/n specifying the name of the old, now-missing library. Usually, the only change in the library/n name was the version number. Acting on a hunch, I simply added a link to the new library but/n named the link after the old library name. I tried the program again and it worked perfectly./n And, okay, the program was a game, and everyone knows the lengths that gamers will go to in/n order to keep their games running.

/n/n

In fact, almost all applications are linked to libraries using a generic name with only a/n major version number in the link name, while the link points to the actual library file that/n also has a minor version number. In other instances, required files have been moved from one/n directory to another to comply with the Linux file specification, and there are links in the/n old directories for backwards compatibility with those programs that have not yet caught up/n with the new locations. If you do a long listing of the /lib64 directory, you can find many/n examples of both.

lrwxrwxrwx. 1 root root 36 Dec 8 2016 cracklib_dict.hwm ->/n ../../usr/share/cracklib/pw_dict.hwm
/n lrwxrwxrwx. 1 root root 36 Dec 8 2016 cracklib_dict.pwd ->/n ../../usr/share/cracklib/pw_dict.pwd
/n lrwxrwxrwx. 1 root root 36 Dec 8 2016 cracklib_dict.pwi ->/n ../../usr/share/cracklib/pw_dict.pwi
/n lrwxrwxrwx. 1 root root 27 Jun 9 2016 libaccountsservice.so.0 ->/n libaccountsservice.so.0.0.0
/n -rwxr-xr-x. 1 root root 288456 Jun 9 2016 libaccountsservice.so.0.0.0
/n lrwxrwxrwx 1 root root 15 May 17 11:47 libacl.so.1 -> libacl.so.1.1.0
/n -rwxr-xr-x 1 root root 36472 May 17 11:47 libacl.so.1.1.0
/n lrwxrwxrwx. 1 root root 15 Feb 4 2016 libaio.so.1 -> libaio.so.1.0.1
/n -rwxr-xr-x. 1 root root 6224 Feb 4 2016 libaio.so.1.0.0
/n -rwxr-xr-x. 1 root root 6224 Feb 4 2016 libaio.so.1.0.1
/n lrwxrwxrwx. 1 root root 30 Jan 16 16:39 libakonadi-calendar.so.4 ->/n libakonadi-calendar.so.4.14.26
/n -rwxr-xr-x. 1 root root 816160 Jan 16 16:39 libakonadi-calendar.so.4.14.26
/n lrwxrwxrwx. 1 root root 29 Jan 16 16:39 libakonadi-contact.so.4 ->/n libakonadi-contact.so.4.14.26/n/n

A few of the links in the /lib64 directory

/n/n

The long listing of the /lib64 directory above shows that the first character in the/n filemode is the letter "l," which means that each is a soft or symbolic link.

Hard/n links/n/n

In An introduction to Linux's/n EXT4 filesystem , I discussed the fact that each file has one inode that contains/n information about that file, including the location of the data belonging to that file./n Figure 2 in that/n article shows a single directory entry that points to the inode. Every file must have at least/n one directory entry that points to the inode that describes the file. The directory entry is a/n hard link, thus every file has at least one hard link.

/n/n

In Figure 1 below, multiple directory entries point to a single inode. These are all hard/n links. I have abbreviated the locations of three of the directory entries using the tilde ( ~ )/n convention for the home directory, so that ~ is equivalent to /home/user in this example. Note/n that the fourth directory entry is in a completely different directory, /home/shared , which/n might be a location for sharing files between users of the computer.

/n/n

fig1directory_entries.png/n Figure 1

/n/n

Hard links are limited to files contained within a single filesystem. "Filesystem" is used/n here in the sense of a partition or logical volume (LV) that is mounted on a specified mount/n point, in this case /home . This is because inode numbers are unique only within each/n filesystem, and a different filesystem, for example, /var or /opt , will have inodes with the/n same number as the inode for our file.

/n/n

Because all the hard links point to the single inode that contains the metadata about the/n file, all of these attributes are part of the file, such as ownerships, permissions, and the/n total number of hard links to the inode, and cannot be different for each hard link. It is one/n file with one set of attributes. The only attribute that can be different is the file name,/n which is not contained in the inode. Hard links to a single file/inode located in the same/n directory must have different names, due to the fact that there can be no duplicate file names/n within a single directory.

/n/n

The number of hard links for a file is displayed with the ls -l command. If you want to/n display the actual inode numbers, the command ls -li does that.

Symbolic (soft) links/n/n

The difference between a hard link and a soft link, also known as a symbolic link (or/n symlink), is that, while hard links point directly to the inode belonging to the file, soft/n links point to a directory entry, i.e., one of the hard links. Because soft links point to a/n hard link for the file and not the inode, they are not dependent upon the inode number and can/n work across filesystems, spanning partitions and LVs.

/n/n

The downside to this is: If the hard link to which the symlink points is deleted or renamed,/n the symlink is broken. The symlink is still there, but it points to a hard link that no longer/n exists. Fortunately, the ls command highlights broken links with flashing white text on a red/n background in a long listing.

Lab project: experimenting with links/n/n

I think the easiest way to understand the use of and differences between hard and soft links/n is with a lab project that you can do. This project should be done in an empty directory as a/n non-root user . I created the ~/temp directory for this project, and you should, too./n It creates a safe place to do the project and provides a new, empty directory to work in so/n that only files associated with this project will be located there.

Initial setup/n/n

First, create the temporary directory in which you will perform the tasks needed for this/n project. Ensure that the present working directory (PWD) is your home directory, then enter the/n following command.

/n
/nmkdir temp/n
/n/n

Change into ~/temp to make it the PWD with this command.

/n
/ncd temp/n
/n/n

To get started, we need to create a file we can link to. The following command does that and/n provides some content as well.

/n
/ndu -h > main.file.txt/n
/n/n

Use the ls -l long list to verify that the file was created correctly. It should look/n similar to my results. Note that the file size is only 7 bytes, but yours may vary by a byte or/n two.

[ dboth @ david temp ] $ ls -l
/n total 4
/n -rw-rw-r-- 1 dboth dboth 7 Jun 13 07: 34 main.file.txt/n/n

Notice the number "1" following the file mode in the listing. That number represents the/n number of hard links that exist for the file. For now, it should be 1 because we have not/n created any additional links to our test file.

Experimenting with hard links/n/n

Hard links create a new directory entry pointing to the same inode, so when hard links are/n added to a file, you will see the number of links increase. Ensure that the PWD is still ~/temp/n . Create a hard link to the file main.file.txt , then do another long list of the/n directory.

[ dboth @ david temp ] $ ln main.file.txt link1.file.txt
/n [ dboth @ david temp ] $ ls -l
/n total 8
/n -rw-rw-r-- 2 dboth dboth 7 Jun 13 07: 34 link1.file.txt
/n -rw-rw-r-- 2 dboth dboth 7 Jun 13 07: 34 main.file.txt/n/n

Notice that both files have two links and are exactly the same size. The date stamp is also/n the same. This is really one file with one inode and two links, i.e., directory entries to it./n Create a second hard link to this file and list the directory contents. You can create the link/n to either of the existing ones: link1.file.txt or main.file.txt .

[ dboth @ david temp ] $/n ln link1.file.txt link2.file.txt ; ls -l
/n total 16
/n -rw-rw-r-- 3 dboth dboth 7 Jun 13 07: 34 link1.file.txt
/n -rw-rw-r-- 3 dboth dboth 7 Jun 13 07: 34 link2.file.txt
/n -rw-rw-r-- 3 dboth dboth 7 Jun 13 07: 34 main.file.txt/n/n

Notice that each new hard link in this directory must have a different name because two/n files -- really directory entries -- cannot have the same name within the same directory. Try/n to create another link with a target name the same as one of the existing ones.

[ dboth @/n david temp ] $ ln main.file.txt link2.file.txt
/n ln: failed to create hard link 'link2.file.txt' : File exists/n/n

Clearly that does not work, because link2.file.txt already exists. So far, we have created/n only hard links in the same directory. So, create a link in your home directory, the parent of/n the temp directory in which we have been working so far.

[ dboth @ david temp ] $ ln/n main.file.txt .. / main.file.txt ; ls -l .. / main *
/n -rw-rw-r-- 4 dboth dboth 7 Jun 13 07: 34 main.file.txt/n/n

The ls command in the above listing shows that the main.file.txt file does exist in the home/n directory with the same name as the file in the temp directory. Of course, these are not/n different files; they are the same file with multiple links -- directory entries -- to the same/n inode. To help illustrate the next point, add a file that is not a link.

[ dboth @ david/n temp ] $ touch unlinked.file ; ls -l
/n total 12
/n -rw-rw-r-- 4 dboth dboth 7 Jun 13 07: 34 link1.file.txt
/n -rw-rw-r-- 4 dboth dboth 7 Jun 13 07: 34 link2.file.txt
/n -rw-rw-r-- 4 dboth dboth 7 Jun 13 07: 34 main.file.txt
/n -rw-rw-r-- 1 dboth dboth 0 Jun 14 08: 18 unlinked.file/n/n

Look at the inode number of the hard links and that of the new file using the -i option to/n the ls command.

[ dboth @ david temp ] $ ls -li
/n total 12
/n 657024 -rw-rw-r-- 4 dboth dboth 7 Jun 13 07: 34 link1.file.txt
/n 657024 -rw-rw-r-- 4 dboth dboth 7 Jun 13 07: 34 link2.file.txt
/n 657024 -rw-rw-r-- 4 dboth dboth 7 Jun 13 07: 34 main.file.txt
/n 657863 -rw-rw-r-- 1 dboth dboth 0 Jun 14 08: 18 unlinked.file/n/n

Notice the number 657024 to the left of the file mode in the example above. That is the/n inode number, and all three file links point to the same inode. You can use the -i option to/n view the inode number for the link we created in the home directory as well, and that will also/n show the same value. The inode number of the file that has only one link is different from the/n others. Note that the inode numbers will be different on your system.

/n/n

Let's change the size of one of the hard-linked files.

[ dboth @ david temp ] $ df -h/n > link2.file.txt ; ls -li
/n total 12
/n 657024 -rw-rw-r-- 4 dboth dboth 1157 Jun 14 14 : 14 link1.file.txt
/n 657024 -rw-rw-r-- 4 dboth dboth 1157 Jun 14 14 : 14 link2.file.txt
/n 657024 -rw-rw-r-- 4 dboth dboth 1157 Jun 14 14 : 14 main.file.txt
/n 657863 -rw-rw-r-- 1 dboth dboth 0 Jun 14 08: 18 unlinked.file/n/n

The file size of all the hard-linked files is now larger than before. That is because there/n is really only one file that is linked to by multiple directory entries.

/n/n

I know this next experiment will work on my computer because my /tmp directory is on a/n separate LV. If you have a separate LV or a filesystem on a different partition (if you're not/n using LVs), determine whether or not you have access to that LV or partition. If you don't, you/n can try to insert a USB memory stick and mount it. If one of those options works for you, you/n can do this experiment.

/n/n

Try to create a link to one of the files in your ~/temp directory in /tmp (or wherever your/n different filesystem directory is located).

[ dboth @ david temp ] $ ln link2.file.txt / tmp/n / link3.file.txt
/n ln: failed to create hard link '/tmp/link3.file.txt' = > 'link2.file.txt' :
/n Invalid cross-device link/n/n

Why does this error occur? The reason is each separate mountable filesystem has its own set/n of inode numbers. Simply referring to a file by an inode number across the entire Linux/n directory structure can result in confusion because the same inode number can exist in each/n mounted filesystem.

/n/n

There may be a time when you will want to locate all the hard links that belong to a single/n inode. You can find the inode number using the ls -li command. Then you can use the find/n command to locate all links with that inode number.

[ dboth @ david temp ] $ find . -inum/n 657024
/n . / main.file.txt
/n . / link1.file.txt
/n . / link2.file.txt/n/n

Note that the find command did not find all four of the hard links to this inode because we/n started at the current directory of ~/temp . The find command only finds files in the PWD and/n its subdirectories. To find all the links, we can use the following command, which specifies/n your home directory as the starting place for the search.

[ dboth @ david temp ] $ find ~/n -samefile main.file.txt
/n / home / dboth / temp / main.file.txt
/n / home / dboth / temp / link1.file.txt
/n / home / dboth / temp / link2.file.txt
/n / home / dboth / main.file.txt/n/n

You may see error messages if you do not have permissions as a non-root user. This command/n also uses the -samefile option instead of specifying the inode number. This works the same as/n using the inode number and can be easier if you know the name of one of the hard/n links.

Experimenting with soft links/n/n

As you have just seen, creating hard links is not possible across filesystem boundaries;/n that is, from a filesystem on one LV or partition to a filesystem on another. Soft links are a/n means to answer that problem with hard links. Although they can accomplish the same end, they/n are very different, and knowing these differences is important.

/n/n

Let's start by creating a symlink in our ~/temp directory to start our exploration.

[/n dboth @ david temp ] $ ln -s link2.file.txt link3.file.txt ; ls -li
/n total 12
/n 657024 -rw-rw-r-- 4 dboth dboth 1157 Jun 14 14 : 14 link1.file.txt
/n 657024 -rw-rw-r-- 4 dboth dboth 1157 Jun 14 14 : 14 link2.file.txt
/n 658270 lrwxrwxrwx 1 dboth dboth 14 Jun 14 15 : 21 link3.file.txt - >
/n link2.file.txt
/n 657024 -rw-rw-r-- 4 dboth dboth 1157 Jun 14 14 : 14 main.file.txt
/n 657863 -rw-rw-r-- 1 dboth dboth 0 Jun 14 08: 18 unlinked.file/n/n

The hard links, those that have the inode number 657024 , are unchanged, and the number of/n hard links shown for each has not changed. The newly created symlink has a different inode,/n number 658270 . The soft link named link3.file.txt points to link2.file.txt . Use the cat/n command to display the contents of link3.file.txt . The file mode information for the symlink/n starts with the letter " l " which indicates that this file is actually a symbolic link.

/n/n

The size of the symlink link3.file.txt is only 14 bytes in the example above. That is the/n size of the text link3.file.txt -> link2.file.txt , which is the actual content of the/n directory entry. The directory entry link3.file.txt does not point to an inode; it points to/n another directory entry, which makes it useful for creating links that span file system/n boundaries. So, let's create that link we tried before from the /tmp directory.

[ dboth @/n david temp ] $ ln -s / home / dboth / temp / link2.file.txt
/n / tmp / link3.file.txt ; ls -l / tmp / link *
/n lrwxrwxrwx 1 dboth dboth 31 Jun 14 21 : 53 / tmp / link3.file.txt - >
/n / home / dboth / temp / link2.file.txt Deleting links/n/n

There are some other things that you should consider when you need to delete links or the/n files to which they point.

/n/n

First, let's delete the link main.file.txt . Remember that every directory entry that points/n to an inode is simply a hard link.

[ dboth @ david temp ] $ rm main.file.txt ; ls -li
/n total 8
/n 657024 -rw-rw-r-- 3 dboth dboth 1157 Jun 14 14 : 14 link1.file.txt
/n 657024 -rw-rw-r-- 3 dboth dboth 1157 Jun 14 14 : 14 link2.file.txt
/n 658270 lrwxrwxrwx 1 dboth dboth 14 Jun 14 15 : 21 link3.file.txt - >
/n link2.file.txt
/n 657863 -rw-rw-r-- 1 dboth dboth 0 Jun 14 08: 18 unlinked.file/n/n

The link main.file.txt was the first link created when the file was created. Deleting it now/n still leaves the original file and its data on the hard drive along with all the remaining hard/n links. To delete the file and its data, you would have to delete all the remaining hard/n links.

/n/n

Now delete the link2.file.txt hard link.

[ dboth @ david temp ] $ rm link2.file.txt ; ls/n -li
/n total 8
/n 657024 -rw-rw-r-- 3 dboth dboth 1157 Jun 14 14 : 14 link1.file.txt
/n 658270 lrwxrwxrwx 1 dboth dboth 14 Jun 14 15 : 21 link3.file.txt - >
/n link2.file.txt
/n 657024 -rw-rw-r-- 3 dboth dboth 1157 Jun 14 14 : 14 main.file.txt
/n 657863 -rw-rw-r-- 1 dboth dboth 0 Jun 14 08: 18 unlinked.file/n/n

Notice what happens to the soft link. Deleting the hard link to which the soft link points/n leaves a broken link. On my system, the broken link is highlighted in colors and the target/n hard link is flashing. If the broken link needs to be fixed, you can create another hard link/n in the same directory with the same name as the old one, so long as not all the hard links have/n been deleted. You could also recreate the link itself, with the link maintaining the same name/n but pointing to one of the remaining hard links. Of course, if the soft link is no longer/n needed, it can be deleted with the rm command.

/n/n

The unlink command can also be used to delete files and links. It is very simple and has no/n options, as the rm command does. It does, however, more accurately reflect the underlying/n process of deletion, in that it removes the link -- the directory entry -- to the file being/n deleted.

Final thoughts/n/n

I worked with both types of links for a long time before I began to understand their/n capabilities and idiosyncrasies. It took writing a lab project for a Linux class I taught to/n fully appreciate how links work. This article is a simplification of what I taught in that/n class, and I hope it speeds your learning curve. Topics Linux About the author David Both - David Both is a Linux and Open Source advocate who/n resides in Raleigh, North Carolina. He has been in the IT industry for over forty years and/n taught OS/2 for IBM where he worked for over 20 years. While at IBM, he wrote the first/n training course for the original IBM PC in 1981. He has taught RHCE classes for Red Hat and has/n worked at MCI Worldcom, Cisco, and the State of North Carolina. He has been working with Linux/n and Open Source Software for almost 20 years. David has written articles for...

/n /n /n
/n/n

[Jun 02, 2010] Learn Linux, 101 Create and change hard and symbolic links by Ian Shields

developerWorks

Hard links

You use the ln command to create additional hard links to an existing file (but not to a directory, even though the system sets up . and .. as hard links).

Listing 1 shows how to create a directory containing two files and a subdirectory with two hard links to file1, one in the same directory and one in the subdirectory. We have added a word to file1, and then another word to file3 and displayed the contents of the link in the subdirectory to show that all do indeed point to the same data.


Listing 1. Creating hard links
ian@attic4:~$ mkdir -p lpi104-6/subdir
ian@attic4:~$ touch lpi104-6/file1
ian@attic4:~$ touch lpi104-6/file2
ian@attic4:~$ ln lpi104-6/file1 lpi104-6/file3
ian@attic4:~$ ln lpi104-6/file1 lpi104-6/subdir/file3sub
ian@attic4:~$ echo "something" > lpi104-6/file1
ian@attic4:~$ echo "else" >> lpi104-6/file3
ian@attic4:~$ cat lpi104-6/subdir/file3sub
something
else

You will get an error if you attempt to create hard links that cross file systems or that are for directories. Listing 2 shows that my home and research directories are on different file systems and that an attempt to create a hard link across these fails, as does an attempt to create a hard link to the lpi104-6 directory.


Listing 2. Failures with hard link creation
ian@attic4:~$ df . research
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/sda7             71205436   9355052  58233352  14% /
/dev/sdb3            137856204  27688208 103165264  22% /home/ian/ian-research
ian@attic4:~$ ln lpi104-6/file1 research/lpi104-6/file3
ln: creating hard link `research/lpi104-6/file3' => `lpi104-6/file1': No such file or dir
ectory
ian@attic4:~$ ln lpi104-6 lpidir104-6
ln: `lpi104-6': hard link not allowed for directory

Soft links

You use the ln command with the -s option to create soft links. Soft links use file or directory names, which may be relative or absolute. If you are using relative names, you will usually want the current working directory to be the directory where you are creating the link; otherwise, the link you create will be relative to another point in the file system. Listing 3 shows you two ways to create a soft link for the file1 that we just created, and also how to create soft links instead of the two hard links that failed in Listing 2.


Listing 3. Creating soft links
ian@attic4:~$ # Create symlink using absolute paths
ian@attic4:~$ ln -s ~/lpi104-6/file1 ~/lpi104-6/file4
ian@attic4:~$ # Create symlink using relative paths
ian@attic4:~$ cd lpi104-6/
ian@attic4:~/lpi104-6$ ln -s file1 file5
ian@attic4:~/lpi104-6$ cd ..
ian@attic4:~$ # Create symlink across file systems
ian@attic4:~$ mkdir ~ian/research/lpi104-6
ian@attic4:~$ ln -s ~/lpi104-6/file1 ~ian/research/lpi104-6/file4
ian@attic4:~$ # Create symlink for directory
ian@attic4:~$ ln -s lpi104-6 lpidir104-6

As before, you can use any of the links or the target file name to reference the file or directory. Listing 4 shows some examples.


Listing 4. Using soft links
ian@attic4:~$ echo "another line" >> ~ian/research/lpi104-6/file
ian@attic4:~$ # cat a symlink
ian@attic4:~$ cat lpi104-6/file5
something
else
another line
ian@attic4:~$ # cat a hard link
ian@attic4:~$ cat lpi104-6/file1
something
else
another line
ian@attic4:~$ # display directory contents using symlink
ian@attic4:~$ ls lpidir104-6
file1  file2  file3  file4  file5  subdir

While we're creating links, let's create a link using relative paths when our working directory is not the directory where we want the link. We'll look at what this does in the next section.


Listing 5. Creating a bad soft link
ian@attic4:~$ ln -s lpi104-6/file1 lpi104-6/file6

Identifying links

In the previous section, you saw how to create links, but not how to distinguish the links you created. Let's look at that now.

Finding information

On many systems today, the ls command is aliased to ls --color=auto, which prints different types of file system objects in different colors. The colors are configurable. If you use this option, hard links might show up with a dark blue background, and symlinks with cyan text,

While color might be convenient for sighted people who can distinguish them, they are not much use to others, and certainly not much use to shell scripts or programs. Without color, you need more information, such as that provided by a long listing using ls -l. In Listing 6 we explicitly disable color output, but you could also explicitly call the /bin/ls command.


Listing 6. Identifying links
ian@attic4:~$ ls --color=none -lR lpi104-6
lpi104-6:
total 12
-rw-r--r-- 3 ian ian   28 2010-05-27 17:17 file1
-rw-r--r-- 1 ian ian    0 2010-05-26 14:11 file2
-rw-r--r-- 3 ian ian   28 2010-05-27 17:17 file3
lrwxrwxrwx 1 ian ian   24 2010-05-27 17:15 file4 -> /home/ian/lpi104-6/file1
lrwxrwxrwx 1 ian ian    5 2010-05-27 17:15 file5 -> file1
lrwxrwxrwx 1 ian ian   14 2010-05-27 17:37 file6 -> lpi104-6/file1
drwxr-xr-x 2 ian ian 4096 2010-05-26 14:11 subdir

lpi104-6/subdir:
total 4
-rw-r--r-- 3 ian ian 28 2010-05-27 17:17 file3sub
ian@attic4:~$ /bin/ls -l ~ian/research/lpi104-6/file4
lrwxrwxrwx 1 ian ian 24 2010-05-25 11:51 /home/ian/research/lpi104-6/file4 -> /home/ian/
lpi104-6/file1
ian@attic4:~$ /bin/ls -l lpidir104-6
lrwxrwxrwx 1 ian ian 8 2010-05-27 17:16 lpidir104-6 -> lpi104-6

The second column of output is a link count showing the number of hard links to this file, so we know that file1, file3, and file3sub all have multiple hard links pointing to the object they represent, although we do not yet have enough information to know they all represent the same object. If you delete a file that has a link count greater than 1, the link count in the inode is reduced by 1, but the file is not deleted until the count goes to 0. All other hard links to the same file will show a link count that is now reduced by 1.

In the first column of output, you see the first character is an 'l' (lower-case L) for symbolic links. You also see the target of the link displayed after the -> characters. For example file4 -> /home/ian/lpi104-6/file1. Another tipoff is that the size is the number of characters in the link target's name. Note that the link counts in the directory listing are not updated for symbolic links. Deleting the link does not affect the target file. Symlinks do not prevent a file from being deleted; if the target file is moved or deleted, then the symlink will be broken. For this reason, many systems use colors in directory listings, often pale blue for a good link and red for a broken one.

You can use the -i option of the ls command to display inode numbers for file and directory entries. Listing 7 shows both short and long output for our lpi104-6 directory.


Listing 7. Displaying inode information
ian@attic4:~$ ls -i lpi104-6
1680103 file1  1680103 file3  1680107 file5  1680101 subdir
1680104 file2  1680108 file4  1680110 file6
ian@attic4:~$ ls -il lpi104-6
total 12
1680103 -rw-r--r-- 3 ian ian   28 2010-05-27 17:17 file1
1680104 -rw-r--r-- 1 ian ian    0 2010-05-26 14:11 file2
1680103 -rw-r--r-- 3 ian ian   28 2010-05-27 17:17 file3
1680108 lrwxrwxrwx 1 ian ian   24 2010-05-27 17:15 file4 -> /home/ian/lpi104-6/file1
1680107 lrwxrwxrwx 1 ian ian    5 2010-05-27 17:15 file5 -> file1
1680110 lrwxrwxrwx 1 ian ian   14 2010-05-27 17:37 file6 -> lpi104-6/file1
1680101 drwxr-xr-x 2 ian ian 4096 2010-05-26 14:11 subdir

You can also use the find command to search for symbolic links using the -type l find expression as shown in Listing 8.

Listing 8. Using find to locate symlinks
ian@attic4:~$ find lpi104-6 research/lpi104-6 -type l
lpi104-6/file6
lpi104-6/file5
lpi104-6/file4
research/lpi104-6/file4

Broken symlinks

In Listing 5, we claimed to create a bad soft link. This is one example of a broken symlink. Since hard links always point to an inode that represents a file, they are always valid. However, symlinks can be broken for many reasons, including:

None of these conditions raises an error, so you need to think carefully about what might happen to your symlinks as you create them. In particular, your choice of absolute or relative paths is likely to be influenced by what you expect to happen to the objects you are linking over the life of the link.

If you are using colored output, broken symlinks are likely to show up as red text on a black background, as is the case for file6 in Figure 1. Otherwise, you will need to use either the -H or -L options of ls to dereference the link and give you information about the target. The -H option dereferences links on the command line and the -L option dereferences those plus links that are part of the display. Listing 9 illustrates the difference in the output from these two options.


Listing 9. Dereferencing links with ls -H and ls -L
ian@attic4:~$ /bin/ls -lH lpidir104-6
total 12
-rw-r--r-- 3 ian ian   28 2010-05-27 17:17 file1
-rw-r--r-- 1 ian ian    0 2010-05-26 14:11 file2
-rw-r--r-- 3 ian ian   28 2010-05-27 17:17 file3
lrwxrwxrwx 1 ian ian   24 2010-05-27 17:15 file4 -> /home/ian/lpi104-6/file1
lrwxrwxrwx 1 ian ian    5 2010-05-27 17:15 file5 -> file1
lrwxrwxrwx 1 ian ian   14 2010-05-27 17:37 file6 -> lpi104-6/file1
drwxr-xr-x 2 ian ian 4096 2010-05-26 14:11 subdir
ian@attic4:~$ /bin/ls -lL lpidir104-6
/bin/ls: cannot access lpidir104-6/file6: No such file or directory
total 20
-rw-r--r-- 3 ian ian   28 2010-05-27 17:17 file1
-rw-r--r-- 1 ian ian    0 2010-05-26 14:11 file2
-rw-r--r-- 3 ian ian   28 2010-05-27 17:17 file3
-rw-r--r-- 3 ian ian   28 2010-05-27 17:17 file4
-rw-r--r-- 3 ian ian   28 2010-05-27 17:17 file5
l????????? ? ?   ?      ?                ? file6
drwxr-xr-x 2 ian ian 4096 2010-05-26 14:11 subdir

Note the error message indicating that file6 does not exist and also the output for it with all the '?' characters, again indicating that the file is not found.

One final point on our broken symbolic link. Attempts to read the file will fail as it does not exist. However, attempts to write it will work if you have the appropriate permission on the target file, as shown in Listing 10. Note that we need to create the lpi104-6/lpi104-6 before we can write the file.


Listing 10. Reading from and writing to a broken symlink
                    
ian@attic4:~$ cat lpi104-6/file6
cat: lpi104-6/file6: No such file or directory
ian@attic4:~$ echo "Testing file6" > lpi104-6/file6
bash: lpi104-6/file6: No such file or directory
ian@attic4:~$ mkdir lpi104-6/lpi104-6
ian@attic4:~$ cat lpi104-6/file6
cat: lpi104-6/file6: No such file or directory
ian@attic4:~$ echo "Testing file6" > lpi104-6/file6
ian@attic4:~$ cat lpi104-6/file6
Testing file6
ian@attic4:~$ ls lpi104-6/lpi104-6
file1

Who links to me?

To find which files are hard links to a particular inode, you can use the find command and the -samefile option with a filename or the -inum option with an inode number, as shown in Listing 11.


Listing 11. Finding hard links to the same file
                    
ian@attic4:~$ find lpi104-6 -samefile lpi104-6/file1
lpi104-6/subdir/file3sub
lpi104-6/file3
lpi104-6/file1
ian@attic4:~$ ls -i lpi104-6/file1
1680103 lpi104-6/file1
ian@attic4:~$ find lpi104-6 -inum 1680103
lpi104-6/subdir/file3sub
lpi104-6/file3
lpi104-6/file1

To find which files link symbolically to a particular file, you can use the find command and the -lname option with a filename, as illustrated in Listing 12. Links may use a relative or absolute path, so you probably want a leading asterisk in the name to find all matches.

Listing 12. Finding symbolic links to a file or directory
ian@attic4:~$ find lpi104-6 research/lpi104-6 -lname "*file1"
lpi104-6/file6
lpi104-6/file5
lpi104-6/file4
research/lpi104-6/file4

[Jul 07, 2007] Easing SUSE Server administration from the command line

How can hard links and symbolic links make administration of SLES 10 easier?

Van Vugt: Hard and symbolic links allow you to put information at the places where you would need it, without losing the option of management in one place. Think of the tmp directory, which is used for temporary files. This directory occurs on many locations in your file system. In general, all those different tmp directories are just links to one single /tmp directory. This allows you to put all temporary files on one centralized location, and to create a special volume for that, which prevents your server from running out of disk space.

Also, links can help you to deal with a program. One example is older graphical utilities that try to locate the X-server in /usr/X11. On a good day, that default location for the X-server changed and nowadays, /usr/X11R6 is used. Imagine that you have such an old application that still tries to locate program files in /usr/X11. It would be able to find those program files if you created a symbolic link with the name X11 that refers to the new X11R6 directory.

docs.sun.com Solaris 10 Release Notes

New ln Utility Requires -f Option

The behavior of /usr/bin/ln has changed to adhere to all of the standards from SVID3 through XCU6. If you use the ln command without the -f option to link to an existing target file, the link is not established. Instead, a diagnostic message is written to standard error, and the command proceeds to link any remaining source files. Finally, the ln command exits with an error value.

For example, if file b exists, the syntax ln a b generates the following message:

ln: b: File exists

This behavior change affects existing shell scripts or programs that include the ln command without the -f option. Scripts that used to work might now fail in Solaris 10 OS.

Workaround: Use the -f option with the ln command. If you have existing scripts that execute the link utility, make sure to modify these scripts to comply with the command's new behavior.

Recommended Links

Google matched content

Softpanorama Recommended

Top articles

Sites

Internal

Solaris File System Structure

External

Reference

docs.sun.com Solaris 10 Release Notes New ln Utility Requires -f Option

The behavior of /usr/bin/ln has changed to adhere to all of the standards from SVID3 through XCU6. If you use the ln command without the -f option to link to an existing target file, the link is not established. Instead, a diagnostic message is written to standard error, and the command proceeds to link any remaining source files. Finally, the ln command exits with an error value.

For example, if file b exists, the syntax ln a b generates the following message:

ln: b: File exists

This behavior change affects existing shell scripts or programs that include the ln command without the -f option. Scripts that used to work might now fail in Solaris 10 OS.

Workaround: Use the -f option with the ln command. If you have existing scripts that execute the link utility, make sure to modify these scripts to comply with the command's new behavior.

Solaris 10

Solaris 9 man pages for ln

Recommended Articles

Learn Linux, 101 Create and change hard and symbolic links

TTTT Links (see also other tips at Tuesday Tiny Techie Tips )

ln(1) creates a "link" to a file. To know what that means, you need to know a little bit about how the UNIX filesystem works. (note, I'm doing this off the top of my head without references, so if you want to go write your own UNIX filesystem, you might want to double check my misconceptions).

The UNIX File System

Most UNIX machines store their files on magnetic disk drives. A disk drive is a device that can store information by making electrical imprints on a magnetic surface. One or more heads skim close to the spinning magnetic plate, and can detect, or change, the magnetic state of a given spot on the disk. The drives use disk controllers to position the head at the correct place at the correct time to read from, or write to, the magnetic surface of the plate. It is often possible to partition a single disk drive into more than one logical storage area. This section describes how the UNIX operating system deals with a raw storage device like a disk drive, and how it manages to make organized use of the space.

How the UNIX file system works

Every item in a UNIX file system can de defined as belonging to one of four possible types:

Ordinary files

Ordinary files can contain text, data, or program information. An ordinary file cannot contain another file, or directory. An ordinary file can be thought of as a one-dimensional array of bytes.

Directories

In a previous section, we described directories as containers that can hold files, and other directories. A directory is actually implemented as a file that has one line for each item contained within the directory. Each line in a directory file contains only the name of the item, and a numerical reference to the location of the item. The reference is called an i-number, and is an index to a table known as the i-list. The i-list is a complete list of all the storage space available to the file system.

Special files

Special files represent input/output (i/o) devices, like a tty (terminal), a disk drive, or a printer. Because UNIX treats such devices as files, a degree of compatibility can be achieved between device i/o, and ordinary file i/o, allowing for the more efficient use of software. Special files can be either character special files, that deal with streams of characters, or block special files, that operate on larger blocks of data. Typical block sizes are 512 bytes, 1024 bytes, and 2048 bytes.

Links

A link is a pointer to another file. Remember that a directory is nothing more than a list of the names and i-numbers of files. A directory entry can be a hard link, in which the i-number points directly to another file. A hard link to a file is indistinguishable from the file itself. When a hard link is made, then the i-numbers of two different directory file entries point to the same inode. For that reason, hard links cannot span across file systems. A soft link (or symbolic link) provides an indirect pointer to a file. A soft link is implemented as a directory file entry containing a pathname. Soft links are distinguishable from files, and can span across file systems. Not all versions of UNIX support soft links.

The I-List

When we speak of a UNIX file system, we are actually referring to an area of physical memory represented by a single i-list. A UNIX machine may be connected to several file systems, each with its own i-list. One of those i-lists points to a special storage area, known as the root file system. The root file system contains the files for the operating system itself, and must be available at all times. Other file systems are removable. Removable file systems can be attached, or mounted, to the root file system. Typically, an empty directory is created on the root file system as a mount point, and a removable file system is attached there. When you issue a cd command to access the files and directories of a mounted removable file system, your file operations will be controlled through the i-list of the removable file system.

The purpose of the i-list is to provide the operating system with a map into the memory of some physical storage device. The map is continually being revised, as the files are created and removed, and as they shrink and grow in size. Thus, the mechanism of mapping must be very flexible to accomodate drastic changes in the number and size of files. The i-list is stored in a known location, on the same memory storage device that it maps.

Each entry in an i-list is called an i-node. An i-node is a complex structure that provides the necessary flexibility to track the changing file system. The i-nodes contain the information necessary to get information from the storage device, which typically communicates in fixed-size disk blocks. An i-node contains 10 direct pointers, which point to disk blocks on the storage device. In addition, each i-node also contains one indirect pointer, one double indirect pointer, and one triple indirect pointer. The indirect pointer points to a block of direct pointers. The double indirect pointer points to a block of indirect pointers, and the triple indirect pointer points to a block of double indirect pointers. By structuring the pointers in a geometric fashion, a single i-node can represent a very large file.

It now makes a little more sense to view a UNIX directory as a list of i-numbers, each i-number referencing a specific i-node on a specific i-list. The operating system traces its way through a file path by following the i-nodes until it reaches the direct pointers that contain the actual location of the file on the storage device.

The file system table

Each file system that is mounted on a UNIX machine is accessed through its own block special file. The information on each of the block special files is kept in a system database called the file system table, and is usually located in /etc/fstab. It includes information about the name of the device, the directory name under which it will be mounted, and the read and write privileges for the device. It is possible to mount a file system as "read-only," to prevent users from changing anything.

File system quotas

Although not originally part of the UNIX filesystem, quotas quickly became a widely-used tool. Quotas allow the system administrator to place limits on the amount of space the users can allocate. Quotas usually place restrictions on the amount of space, and the number of files, that a user can take. The limit can be a soft limit, where only a warning is generated, or a hard limit, where no further operations that create files will be allowed.

The command

quota

will let you know if you're over your soft limit. Adding the -v option will provide statistics about your disk usage.

File system related commands

Here are some commands related to file system usage, and other topics discussed in this section:

bdf

On HP-UX systems, reports file system usage statistics

df

On HP-UX systems, reports on free disk blocks, and i-nodes

du

Summarizes disk usage in a specified directory hierarchy

ln

Creates a hard link (default), or a soft link (with -s option)

mount, umount

Attaches, or detaches, a file system (super user only)

mkfs

Constructs a new file system (super user only)

fsck

Evaluates the integrity of a file system (super user only)

A brief tour of the UNIX filesystem

The actual locations and names of certain system configuration files will differ under different inplementations of UNIX. Here are some examples of important files and directories under version 9 of the HP-UX operating system:

/hp-ux

The kernel program

/dev/

Where special files are kept

/bin/

Executable system utilities, like sh, cp, rm

/etc/

System configuration files and databases

/lib/

Operating system and programming libraries

/tmp/

System scratch files (all users can write here)

/lost+found/

Where the file system checker puts detached files

/usr/bin/

Additional user commands

/usr/include/

Standard system header files

/usr/lib/

More programming and system call libraries

/usr/local/

Typically a place where local utilities go

/usr/man

The manual pages are kept here

Other places to look for useful stuff

If you get an account on an unfamiliar UNIX system, take a tour of the directories listed above, and familiarize yourself with their contents. Another way to find out what is available is to look at the contents of your PATH environment variable:

echo $PATH

You can use the ls command to list the contents of each directory in your path, and the man command to get help on unfamiliar utilities. A good systems administrator will ensure that manual pages are provided for the utilities installed on the system.


Etc

Society

Groupthink : Two Party System as Polyarchy : Corruption of Regulators : Bureaucracies : Understanding Micromanagers and Control Freaks : Toxic Managers :   Harvard Mafia : Diplomatic Communication : Surviving a Bad Performance Review : Insufficient Retirement Funds as Immanent Problem of Neoliberal Regime : PseudoScience : Who Rules America : Neoliberalism  : The Iron Law of Oligarchy : Libertarian Philosophy

Quotes

War and Peace : Skeptical Finance : John Kenneth Galbraith :Talleyrand : Oscar Wilde : Otto Von Bismarck : Keynes : George Carlin : Skeptics : Propaganda  : SE quotes : Language Design and Programming Quotes : Random IT-related quotesSomerset Maugham : Marcus Aurelius : Kurt Vonnegut : Eric Hoffer : Winston Churchill : Napoleon Bonaparte : Ambrose BierceBernard Shaw : Mark Twain Quotes

Bulletin:

Vol 25, No.12 (December, 2013) Rational Fools vs. Efficient Crooks The efficient markets hypothesis : Political Skeptic Bulletin, 2013 : Unemployment Bulletin, 2010 :  Vol 23, No.10 (October, 2011) An observation about corporate security departments : Slightly Skeptical Euromaydan Chronicles, June 2014 : Greenspan legacy bulletin, 2008 : Vol 25, No.10 (October, 2013) Cryptolocker Trojan (Win32/Crilock.A) : Vol 25, No.08 (August, 2013) Cloud providers as intelligence collection hubs : Financial Humor Bulletin, 2010 : Inequality Bulletin, 2009 : Financial Humor Bulletin, 2008 : Copyleft Problems Bulletin, 2004 : Financial Humor Bulletin, 2011 : Energy Bulletin, 2010 : Malware Protection Bulletin, 2010 : Vol 26, No.1 (January, 2013) Object-Oriented Cult : Political Skeptic Bulletin, 2011 : Vol 23, No.11 (November, 2011) Softpanorama classification of sysadmin horror stories : Vol 25, No.05 (May, 2013) Corporate bullshit as a communication method  : Vol 25, No.06 (June, 2013) A Note on the Relationship of Brooks Law and Conway Law

History:

Fifty glorious years (1950-2000): the triumph of the US computer engineering : Donald Knuth : TAoCP and its Influence of Computer Science : Richard Stallman : Linus Torvalds  : Larry Wall  : John K. Ousterhout : CTSS : Multix OS Unix History : Unix shell history : VI editor : History of pipes concept : Solaris : MS DOSProgramming Languages History : PL/1 : Simula 67 : C : History of GCC developmentScripting Languages : Perl history   : OS History : Mail : DNS : SSH : CPU Instruction Sets : SPARC systems 1987-2006 : Norton Commander : Norton Utilities : Norton Ghost : Frontpage history : Malware Defense History : GNU Screen : OSS early history

Classic books:

The Peter Principle : Parkinson Law : 1984 : The Mythical Man-MonthHow to Solve It by George Polya : The Art of Computer Programming : The Elements of Programming Style : The Unix Hater’s Handbook : The Jargon file : The True Believer : Programming Pearls : The Good Soldier Svejk : The Power Elite

Most popular humor pages:

Manifest of the Softpanorama IT Slacker Society : Ten Commandments of the IT Slackers Society : Computer Humor Collection : BSD Logo Story : The Cuckoo's Egg : IT Slang : C++ Humor : ARE YOU A BBS ADDICT? : The Perl Purity Test : Object oriented programmers of all nations : Financial Humor : Financial Humor Bulletin, 2008 : Financial Humor Bulletin, 2010 : The Most Comprehensive Collection of Editor-related Humor : Programming Language Humor : Goldman Sachs related humor : Greenspan humor : C Humor : Scripting Humor : Real Programmers Humor : Web Humor : GPL-related Humor : OFM Humor : Politically Incorrect Humor : IDS Humor : "Linux Sucks" Humor : Russian Musical Humor : Best Russian Programmer Humor : Microsoft plans to buy Catholic Church : Richard Stallman Related Humor : Admin Humor : Perl-related Humor : Linus Torvalds Related humor : PseudoScience Related Humor : Networking Humor : Shell Humor : Financial Humor Bulletin, 2011 : Financial Humor Bulletin, 2012 : Financial Humor Bulletin, 2013 : Java Humor : Software Engineering Humor : Sun Solaris Related Humor : Education Humor : IBM Humor : Assembler-related Humor : VIM Humor : Computer Viruses Humor : Bright tomorrow is rescheduled to a day after tomorrow : Classic Computer Humor

The Last but not Least Technology is dominated by two types of people: those who understand what they do not manage and those who manage what they do not understand ~Archibald Putt. Ph.D


Copyright © 1996-2021 by Softpanorama Society. www.softpanorama.org was initially created as a service to the (now defunct) UN Sustainable Development Networking Programme (SDNP) without any remuneration. This document is an industrial compilation designed and created exclusively for educational use and is distributed under the Softpanorama Content License. Original materials copyright belong to respective owners. Quotes are made for educational purposes only in compliance with the fair use doctrine.

FAIR USE NOTICE This site contains copyrighted material the use of which has not always been specifically authorized by the copyright owner. We are making such material available to advance understanding of computer science, IT technology, economic, scientific, and social issues. We believe this constitutes a 'fair use' of any such copyrighted material as provided by section 107 of the US Copyright Law according to which such material can be distributed without profit exclusively for research and educational purposes.

This is a Spartan WHYFF (We Help You For Free) site written by people for whom English is not a native language. Grammar and spelling errors should be expected. The site contain some broken links as it develops like a living tree...

You can use PayPal to to buy a cup of coffee for authors of this site

Disclaimer:

The statements, views and opinions presented on this web page are those of the author (or referenced source) and are not endorsed by, nor do they necessarily reflect, the opinions of the Softpanorama society. We do not warrant the correctness of the information provided or its fitness for any purpose. The site uses AdSense so you need to be aware of Google privacy policy. You you do not want to be tracked by Google please disable Javascript for this site. This site is perfectly usable without Javascript.

Last modified: February 19, 2020