Links is a way to replicate a file and share it with other users (of course with a proper permission applied). Any changes made to any of the link files, all will be affected. This was used last time during software programming development on unix (and yes that will be >10 years ago, vi is the best editor and cobol was widely used).

Example will be, you have just created one common C functions file which other programmers might want to call from their programs and they can modify/add a new functions later. What you can do is make a link of this library file and move the linked file to other programmers folders that want to use it, so any changes made all will be updated, and if you accidentally deleted one of the linked file, you can just make another one. The Bad thing is when someone accidentally mess the contents of the file, everybody will get the same ! so in this case Backup is the only solution then.

Lets go to our sample working folders, ls -il will display the inode information of the files, as the only clue that the file is linked is by looking at their inode number. The same inode number indicate that it is the same file or it is a link.

user1$ ls -il
total 16
917508 -rw------- 1 user1 users 36 Feb 21 14:36 file1.txt
917509 -rw------- 1 user1 users 60 Feb 21 14:37 file2.txt
917510 -rw------- 1 user1 users 144 Feb 21 14:37 file3.txt
917511 -rw------- 1 user1 users 96 Feb 21 14:37 file4.txt
user1$

In the example above the inode number is the left-most number and another thing you can check later is the number at column 3, in this case is 1 – later we will see more on this.

Syntax for link command is:

ln sourcefile targetfile

Lets make a link of file1.txt

user1$ ln file1.txt file1_copy.txt

Now check the inode number for file1.txt and file1_copy.txt, it sharing the same number ! and the number in the column 3 is now 2 indicating that it has 2 files now with the same inode number.

user1$ ls -il
total 20
917508 -rw------- 2 user1 users 36 Feb 21 14:36 file1.txt
917508 -rw------- 2 user1 users 36 Feb 21 14:36 file1_copy.txt
917509 -rw------- 1 user1 users 60 Feb 21 14:37 file2.txt
917510 -rw------- 1 user1 users 144 Feb 21 14:37 file3.txt
917511 -rw------- 1 user1 users 96 Feb 21 14:37 file4.txt

Now lets append some new line to the file1.txt (observe the changes in filesize of both files).

user1$ echo "I just appended this line" >> file1.txt
user1$ ls -il
total 20
917508 -rw------- 2 user1 users 62 Mar 2 08:41 file1.txt
917508 -rw------- 2 user1 users 62 Mar 2 08:41 file1_copy.txt
917509 -rw------- 1 user1 users 60 Feb 21 14:37 file2.txt
917510 -rw------- 1 user1 users 144 Feb 21 14:37 file3.txt
917511 -rw------- 1 user1 users 96 Feb 21 14:37 file4.txt

and also a new line for file1_copy.txt (observe the changes in filesize of both files)

user1$ echo "I just appended this line 2" >> file1_copy.txt
user1$ ls -il
total 20
917508 -rw------- 2 user1 users 90 Mar 2 08:41 file1.txt
917508 -rw------- 2 user1 users 90 Mar 2 08:41 file1_copy.txt
917509 -rw------- 1 user1 users 60 Feb 21 14:37 file2.txt
917510 -rw------- 1 user1 users 144 Feb 21 14:37 file3.txt
917511 -rw------- 1 user1 users 96 Feb 21 14:37 file4.txt

Now lets see the contents of both files.

user1$ cat file1.txt
file1
file1
file1
file1
file1
file1
I just appended this line
I just appended this line 2
user1$
user1$ cat file1_copy.txt
file1
file1
file1
file1
file1
file1
I just appended this line
I just appended this line 2

As expected, both will have the same contents. Any changes made to any of the files will affects all.

Deleting one of the link files will affects the other.

user1$ ln file1.txt file1_copy2.txt
user1$ ls -il
total 20
917508 -rw------- 3 user1 users 36 Feb 21 14:36 file1.txt
917508 -rw------- 3 user1 users 36 Feb 21 14:36 file1_copy.txt
917508 -rw------- 3 user1 users 36 Feb 21 14:36 file1_copy2.txt
917509 -rw------- 1 user1 users 60 Feb 21 14:37 file2.txt
917510 -rw------- 1 user1 users 144 Feb 21 14:37 file3.txt
917511 -rw------- 1 user1 users 96 Feb 21 14:37 file4.txt
user1$ rm file1_copy2.txt
user1$ ls -il
total 20
917508 -rw------- 3 user1 users 36 Feb 21 14:36 file1.txt
917508 -rw------- 3 user1 users 36 Feb 21 14:36 file1_copy.txt
917509 -rw------- 1 user1 users 60 Feb 21 14:37 file2.txt
917510 -rw------- 1 user1 users 144 Feb 21 14:37 file3.txt
917511 -rw------- 1 user1 users 96 Feb 21 14:37 file4.txt

You cannot do a link on directory.

user1$ ln tmp2 tmp3
ln: `tmp2': hard link not allowed for directory

Symbolic links

Syntax for symbolic link command is:

ln -s sourcefile targetfile

Symbolic links is more to shortcut in windows than link, all it does basically just redirect it to the original file. The inode number of the symbolic link is different from the source.

user1$ ln -s file2.txt file2_copy.txt
user1$ ls -il
total 20
917508 -rw------- 2 user1 users 36 Feb 21 14:36 file1.txt
917508 -rw------- 2 user1 users 36 Feb 21 14:36 file1_copy.txt
917509 -rw------- 1 user1 users 60 Feb 21 14:37 file2.txt
917563 lrwxrwxrwx 1 user1 users 9 Mar 2 08:39 file2_copy.txt -> file2.txt
917510 -rw------- 1 user1 users 144 Feb 21 14:37 file3.txt
917511 -rw------- 1 user1 users 96 Feb 21 14:37 file4.txt

Deleting the symbolic link file will not affecting the source file.

user1$ rm file2_copy.txt
user1$ ls -il
total 20
917508 -rw------- 2 user1 users 36 Feb 21 14:36 file1.txt
917508 -rw------- 2 user1 users 36 Feb 21 14:36 file1_copy.txt
917509 -rw------- 1 user1 users 60 Feb 21 14:37 file2.txt
917510 -rw------- 1 user1 users 144 Feb 21 14:37 file3.txt
917511 -rw------- 1 user1 users 96 Feb 21 14:37 file4.txt

Lets re-create the symbolic link file and then this time try delete the source file instead.

user1$ ln -s file2.txt file2_copy.txt
user1$ ls -il
total 20
917508 -rw------- 2 user1 users 90 Mar 2 08:41 file1.txt
917508 -rw------- 2 user1 users 90 Mar 2 08:41 file1_copy.txt
917509 -rw------- 1 user1 users 60 Feb 21 14:37 file2.txt
917563 lrwxrwxrwx 1 user1 users 9 Mar 2 08:42 file2_copy.txt -> file2.txt
917510 -rw------- 1 user1 users 144 Feb 21 14:37 file3.txt
917511 -rw------- 1 user1 users 96 Feb 21 14:37 file4.txt
user1$ rm file2.txt
user1$ ls -il
total 16
917508 -rw------- 2 user1 users 90 Mar 2 08:41 file1.txt
917508 -rw------- 2 user1 users 90 Mar 2 08:41 file1_copy.txt
917563 lrwxrwxrwx 1 user1 users 9 Mar 2 08:42 file2_copy.txt -> file2.txt
917510 -rw------- 1 user1 users 144 Feb 21 14:37 file3.txt
917511 -rw------- 1 user1 users 96 Feb 21 14:37 file4.txt
user1$ cat file2_copy.txt
cat: file2_copy.txt: No such file or directory
user1$

When you delete the source file, all the symbolic link file will be useless as its now redirecting to a file that doesn’t exists.

You can create a symbolic link of folder/directory, this is very helpful as you can create shortcut for a long sub-folders.

user1$ mkdir tmp2
user1$ ln -s tmp2 tmp3
user1$ ls -il
total 24
917508 -rw------- 3 user1 users 90 Mar 2 08:41 file1.txt
917508 -rw------- 3 user1 users 90 Mar 2 08:41 file1_copy.txt
917508 -rw------- 3 user1 users 90 Mar 2 08:41 file1_copy2.txt
917563 lrwxrwxrwx 1 user1 users 9 Mar 2 08:42 file2_copy.txt -> file2.txt
917510 -rw------- 1 user1 users 144 Feb 21 14:37 file3.txt
917511 -rw------- 1 user1 users 96 Feb 21 14:37 file4.txt
2228384 drwx------ 2 user1 users 4096 Mar 2 08:51 tmp2/
917509 lrwxrwxrwx 1 user1 users 4 Mar 2 08:51 tmp3 -> tmp2/
user1$

Tags: , , ,

Leave a Reply

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>