ln (linking files)


A file can have more than one name without using the system space or storage. This can be done by linking files together by ln command. 

"link" command can also be used as an alternate for this command. It is useful while maintaining multiple copies of a file in many places without using storage for the extra copies; instead creating a link points to the original copy.  


Syntax:

ln source_file target_file

link source_file target_file






Example:


$pwd
/home/pinku/my_dir
$ls
A_dir           B_dir           C_dir           G_dir           file1.txt       file2.txt       file3.txt       file4.txt
$

$cat file1.txt
This is file content.
we are covering linking files.

$

​$ln file1.txt ./C_dir/linkfile1.txt
$

$cd C_dir
$ls
A1              file1.txt       file2.txt       file6.txt       linkfile1.txt
$cat linkfile1.txt
This is file content.
we are covering linking files.

$

$link file1.txt file5
$ls 

$link file1.txt file5
$ls

A1        file1.txt       file2.txt       file6.txt       linkfile1.txt      file5

$


Concept behind link is i-node :


This is inode number by which you can create a hard link, having two or more names for the same file. 


We used to think about a directory containing files. This is really an illusion. The data of the files are not stored in the directory. A directory is really just a file. File is stored in two different parts of the disk - the "data-blocks" and the "inodes". The "data-blocks" contain the contents of the file and the file information is stored in the "inode". 


Both "inodes" and "data-blocks" are stored in a file system and that is how a disk partition is organized.


i-node contains the following information about the files:

  • Permission field
  • Owner ID
  • Group ID
  • Size of file
  • Number of hard links to the file
  • Time last accessed
  • Time last modified
  • Time inode last modified


An inode contains various information of the file except the "file-name" and "path". It is the directory which stores file-name and path. A directory is just like a table which contains the filenames and the matching inode (give "ls" command with "-i" option to see).


There are first two entries in every directory "." and ".." and we term it as hidden system directories which can be listed by "ls" command with "-a" option. Here the first points to the inode of the current directory, and the second points to the inode of the parent directory. 


Whenever you create a hard link, a new name is created in a directory along with its inode without moving the file. When you move a file (or rename it), you don't actually copy the data. You just create the (name, inode) entry in a new directory, and delete the old entry in the table inside the old directory entry. 








There are two types of links:


  • Hard links 
  • Symbolic link / Soft link



Difference between Hard link and Symbolic link:


There are few limitations of the hard link which is removed by the Symbolic link:


  • you can't create a hard link for the file which is in different file system, i.e: you can't link one file from /usr file system to another /home file system. But it can be done using soft link.


  • you can't link directory even within the same file system using hard link, you can do it using soft link.



Options to use with ln command:


  • ln -s - create symbolic link


  • ln -L - default, create a hard link

 

  • ln -i  - interactively, ask you for your confirmation then press "y" or "n"


  • ln -v  - verbose mode, prompt you with message on the screen


  • ln -h  - If the target_file or target_dir is a symbolic link, do not follow it


  • ln -w  - Warn if the source of a symbolic link does not currently exist



Examples:


$pwd
/home/pinku/my_dir


creating a symbolic link


​$ls
A_dir           B_dir           C_dir           G_dir           file1.txt       file2.txt       file3.txt       file4.txt       file5
$ln -s file1.txt linkfile1
$file linkfile1
linkfile1: symbolic link to `file1.txt'
$


creating a hard link


​$ln -L file1.txt linkfile1
ln: linkfile1: File exists

$
$ln -L linkfile1 hardlinkfile1_pointing_softlink

$
$ls -m
A_dir, B_dir, C_dir, G_dir, file1.txt, file2.txt, file3.txt, file4.txt, file5, hardlinkfile1_pointing_softlink, linkfile1

$

interactively, asking you for your confirmation then press "y" or "n"


$ln -i file2.txt linkfile2
$ln -i file2.txt file5
replace file5? n
not replaced

$


If the target_file or target_dir is a symbolic link, do not follow it


$ln -hi file2.txt hardlinkfile1_pointing_softlink
replace hardlinkfile1_pointing_softlink? n
not replaced

$


Warn if the source of a symbolic link does not currently exist


$ln -w file0.txt linkfile0
ln: file0.txt: No such file or directory
$



Questions & Answers


Qs: What is the difference between hard link and soft link?


please refer above


Qs: What are the other options to create a link file other than "ln command" ?


  • link command
  • cp -l


Qs: what is an i-node number and where it is stored ?


i-node number is an unique number assigned to every file in unix environment and it is stored inside directory with its corresponding filenames. The inode number indexes a table of inodes in a known location on the device. From the inode number, the kernel's file system driver can access the inode contents, including the location of the file - thus allowing access to the file.


Qs: what is "." (dot) and ".." (double dot) in directory?


These are the hidden files created by default when we create any directory file, these are the link to current directory and parent directory. Dot "." points to current and ".." points to parent directory.



NEXT ->

Cracking IT Interview