Monday, March 25, 2013

Links: Hard and Symbolics - Linux

Let's quickly talk about links, and see how to deal with them.

First an exemple, to see  hardlinks:

$ mkdir DIR1
$ cd DIR1/
$ echo "FILE 1" > FILE-1.txt
$ ll
total 4,0K
-rw-r--r-- 1 philippe philippe 7 25 mars  17:15 FILE-1.txt

$ cat FILE-1.txt
FILE 1

$ ln FILE-1.txt FILE-2.txt
$ ll
total 8,0K
-rw-r--r-- 2 philippe philippe 7 25 mars  17:15 FILE-1.txt
-rw-r--r-- 2 philippe philippe 7 25 mars  17:15 FILE-2.txt

$ cat FILE-2.txt
FILE 1

$ rm FILE-1.txt
rm : supprimer fichier « FILE-1.txt » ? y

$ ll
total 4,0K
-rw-r--r-- 1 philippe philippe 7 25 mars  17:15 FILE-2.txt

$ cat FILE-2.txt
FILE 1

 

As you may seen, i' ve created a directory, put inside a file. The content of this file is 'File 1'. Then i create a hardlink to it, named file2, so when i list the directory content, i can see two regular files, with same size, and same modification time. I've verified that the content are the same for file1 and file2.

We can also see, if i delete file1, that file2 remain, and that the content also remain available.

Let's do the same thing with symbolic

$ ln -s FILE-2.txt FILE-3.txt
$ ll
total 4.0K
-rw-r--r-- 1 philippe philippe  7 Mar 25 17:15 FILE-2.txt
lrwxrwxrwx 1 philippe philippe 10 Mar 25 17:46 FILE-3.txt -> FILE-2.txt
$ cat FILE-3.txt
FILE 1
$ ll
total 0
lrwxrwxrwx 1 philippe philippe 10 Mar 25 17:46 FILE-3.txt -> FILE-2.txt
$ cat FILE-3.txt
cat: FILE-3.txt: No such file or directory
[Exit 1 ]

Now, we created a symbolic file, this can be seen with the arrow in the ls -l output (ll). In this case, when i removed the file File2, the content of File3 wasn't avaible anymore.

This post was the introduction for the next one, handling links in windows, soon.

 

No comments: