linux_wiki:create_hard_and_soft_links

Create Hard And Soft Links

General Information

Creating “short cuts” to files/directories.


Soft link

  • Link to a specific file location (can cross file systems)
  • Also known as symbolic link or symlink.
  • Like a simple shortcut; deleting a symlink does not remove original file.
  • Deleting original file breaks the symlink and data is gone.

Hard link

  • Link to specific inode (cannot cross filesystems)
  • Data not gone until all hard links have been deleted

Example listing of a symbolic link

ls -l /etc/systemd/system/
lrwxrwxrwx. 1 root root   37 Oct 17  2014 default.target -> /lib/systemd/system/multi-user.target

Create symlink

ln -s /etc/motd motd-local
  • Creates a symlink to “/etc/motd” in the current directory, called “motd-local”

Create hardlink

touch file1
ln file1 file2

List the files

ll
-rw-------. 1 root root       9227 Oct 17  2014 anaconda-ks.cfg
-rw-r--r--. 2 root root          0 Jun 20 15:11 file1
-rw-r--r--. 2 root root          0 Jun 20 15:11 file2
  • Notice the column with “2”, meaning 2 links to the inode)

List with inode (-i) information

ls -li
  8419076 -rw-------. 1 root root       9227 Oct 17  2014 anaconda-ks.cfg
  8442605 -rw-r--r--. 2 root root          0 Jun 20 15:11 file1
  8442605 -rw-r--r--. 2 root root          0 Jun 20 15:11 file2
  • Notice the first column shows the same inode for file1 and file2.

  • linux_wiki/create_hard_and_soft_links.txt
  • Last modified: 2019/05/25 23:50
  • (external edit)