Tuesday, March 13, 2012

Linux Command - ln

ln command - It is used to create a link between files. There are two types of links available in the Linux system.

  1. Hard Link
  2. Symbolic Link

Hard Link – It points the exact location of the specific file on the system.
Syntax: # ln target_file hardlink_name

Create a file named "file1"
# echo "This is file1" > file1

List the information of the current directory with inode value. Inode has the information of where the data is stored on the disk.
# ll –i
total 8
656011 -rw-r--r-- 1 root root 14 Mar 13 14:23 file1

Create a hard link “file2” for file1
# ln file1 file2

If you check the content of these two files, it will be same.
# cat file1 file2
This is file1
This is file1

If you check the inode value of both files, it will be same.
# ll -i
total 16
656011 -rw-r--r-- 2 root root 14 Mar 13 14:23 file1
656011 -rw-r--r-- 2 root root 14 Mar 13 14:23 file2

Even though we remove file1, the file2 is still accessible as the hard link directly points to location of the data.
# rm -f file1

# ll -i
total 8
656011 -rw-r--r-- 1 root root 14 Mar 13 14:23 file2
# cat file2
This is file1

Symbolic Link

Syntax: # ln –s file1 file2

Create a file
# echo "This is file1" > file1

Check the content of the file1
# cat file1
This is file1

Create a symbolic link for the file and list the information. The inode for these two files will be different. The file2 get the content through the file1 in the name of symbolic link. It is similar to short cut in windows.
# ln -s file1 file2
# ll -i

total 12
656013 -rw-r--r-- 1 root root 14 Mar 13 15:07 file1
656014 lrwxrwxrwx 1 root root  5 Mar 13 15:07 file2 -> file1

Check the type of the file2.
# file file2
file2: symbolic link to `file1'

If we remove the file1, the symbolic link file2 will not work.
# rm -f file1

#cat file2
cat: file2: No such file or directory

# file file2
file2: broken symbolic link to `file1'

Linux Command - File

File Command

This command is used to find the data type contained in a file

# file file_name

# file test.txt