OnWorks Linux and Windows Online WorkStations

Logo

Free Hosting Online for WorkStations

< Previous | Contents | Next >

Creating Hard Links

Now we'll try some links. First the hard links. We’ll create some links to our data file like so:



[me@linuxbox playground]$ ln fun fun-hard [me@linuxbox playground]$ ln fun dir1/fun-hard [me@linuxbox playground]$ ln fun dir2/fun-hard

[me@linuxbox playground]$ ln fun fun-hard [me@linuxbox playground]$ ln fun dir1/fun-hard [me@linuxbox playground]$ ln fun dir2/fun-hard


So now we have four instances of the file “fun”. Let's take a look at our playground di- rectory:


image

[me@linuxbox playground]$ ls -l

total 16


drwxrwxr-x

2

me

me

4096

2016-01-14

16:17

dir1

drwxrwxr-x

2

me

me

4096

2016-01-14

16:17

dir2

-rw-r--r--

4

me

me

1650

2016-01-10

16:33

fun

-rw-r--r--

4

me

me

1650

2016-01-10

16:33

fun-hard


One thing you notice is that the second field in the listing for fun and fun-hard both contain a “4” which is the number of hard links that now exist for the file. You'll remem- ber that a file will aways have at least one link because the file's name is created by a link. So, how do we know that fun and fun-hard are, in fact, the same file? In this case, ls is not very helpful. While we can see that fun and fun-hard are both the same size (field 5), our listing provides no way to be sure. To solve this problem, we're


going to have to dig a little deeper.

When thinking about hard links, it is helpful to imagine that files are made up of two parts: the data part containing the file's contents and the name part which holds the file's name. When we create hard links, we are actually creating additional name parts that all refer to the same data part. The system assigns a chain of disk blocks to what is called an inode, which is then associated with the name part. Each hard link therefore refers to a specific inode containing the file's contents.

The ls command has a way to reveal this information. It is invoked with the “-i” option:


image

[me@linuxbox playground]$ ls -li

total 16


12353539

drwxrwxr-x

2

me

me

4096

2016-01-14

16:17

dir1

12353540

drwxrwxr-x

2

me

me

4096

2016-01-14

16:17

dir2

12353538

-rw-r--r--

4

me

me

1650

2016-01-10

16:33

fun

12353538

-rw-r--r--

4

me

me

1650

2016-01-10

16:33

fun-hard


In this version of the listing, the first field is the inode number and, as we can see, both fun and fun-hard share the same inode number, which confirms they are the same file.


Top OS Cloud Computing at OnWorks: