A link in UNIX is a pointer to a file. Like pointers in any programming languages, links in UNIX are pointers pointing to a file or a directory. Creating links is a kind of shortcuts to access a file. Links allow more than one file name to refer to the same file, elsewhere.
There are two types of links :
- Soft Link or Symbolic links
- Hard Links
These links behave differently when the source of the link (what is being linked to) is moved or removed. Symbolic links are not updated (they merely contain a string which is the pathname of its target); hard links always refer to the source, even if moved or removed.
For example, if we have a file a.txt. If we create a hard link to the file and then delete the file, we can still access the file using hard link. But if we create a soft link of the file and then delete the file, we can’t access the file through soft link and soft link becomes dangling. Basically hard link increases reference count of a location while soft links work as a shortcut (like in Windows)
1. Hard Links
- Each hard linked file is assigned the same Inode value as the original, therefore they reference the same physical file location. Hard links more flexible and remain linked even if the original or linked files are moved throughout the file system, although hard links are unable to cross different file systems.
- ls -l command shows all the links with the link column shows number of links.
- Links have actual file contents
- Removing any link, just reduces the link count, but doesn’t affect other links.
- We cannot create a hard link for a directory to avoid recursive loops.
- If original file is removed then the link will still show the content of the file.
- Command to create a hard link is:
$ ln [original filename] [link name]
2. Soft Links
- A soft link is similar to the file shortcut feature which is used in Windows Operating systems. Each soft linked file contains a separate Inode value that points to the original file. As similar to hard links, any changes to the data in either file is reflected in the other. Soft links can be linked across different file systems, although if the original file is deleted or moved, the soft linked file will not work correctly (called hanging link).
- ls -l command shows all links with first column value l? and the link points to original file.
- Soft Link contains the path for original file and not the contents.
- Removing soft link doesn’t affect anything but removing original file, the link becomes “dangling” link which points to nonexistent file.
- A soft link can link to a directory.
- Link across filesystems: If you want to link files across the filesystems, you can only use symlinks/soft links.
- Command to create a Soft link is:
$ ln -s [original filename] [link name]
This article is contributed by Sahil Rajput. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Getting System and Process Information Using C Programming and Shell in Linux
- Terminal Mail and Linux Security Mail
- Zombie Processes and their Prevention
- Initializing and Cache Mechanism in Linux Kernel
- Reset a lost Linux administrative password and Explanation
- dup() and dup2() Linux system call
- mindepth and maxdepth in Linux find() command for limiting search to a specific directory.
- Network configuration and trouble shooting commands in Linux
- Introduction to Linux Shell and Shell Scripting
- Operations on Audio/Video files using ffmpeg, avconv, and youtube-dl
- Important Linux Commands (leave, diff, cal, ncal, locate and ln)
- Difference between grep and fgrep command
- getppid() and getpid() in Linux
- dos2unix and unix2dos commands
- Create a shared Folder between Host OS and Guest OS ( Virtual Box)
- Internal and External Commands in Linux
- Useful and time saving bash commands in Linux
- Using fork() to produce 1 parent and its 3 child processes
- halt, poweroff and reboot Commands in Linux
- Conditional wait and signal in multi-threading
Improved By : Akash Gajjar