• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer navigation

The Geek Diary

  • OS
    • Linux
    • CentOS/RHEL
    • Solaris
    • Oracle Linux
    • VCS
  • Interview Questions
  • Database
    • oracle
    • oracle 12c
    • ASM
    • mysql
    • MariaDB
  • DevOps
    • Docker
    • Shell Scripting
  • Big Data
    • Hadoop
    • Cloudera
    • Hortonworks HDP

ln Command Examples in Linux

by admin

The ln command is used to create a link to a file. Linking enables a file name in one directory (the link) to point to a file in another directory (the target). A link does not contain data of its own, only a reference to the target file. Any changes to the link will reflect in the target file. If you don’t specify the link name, the ln command will create the link in your current working directory.

Syntax

The syntax of the ln command is:

# ln [options] {target name} [link name]

ln Command Options

The ln command has various options. Some of the frequently used options are given in the following table.

Option Used To
–backup Back up existing destination files.
-f Remove existing destination files.
-s Make symbolic links instead of hard links. -i Prompt to remove destination files.
-v Print the name of a file before linking.

Types of Links

Using the ln command, you can create two types of links: hard and symbolic (soft). Hard and symbolic links are a feature of the file system and are common in most file systems supported by Linux. The ext2, ext3, ext4, and XFS file systems all support hard and symbolic links.

Hard Links

A hard link is a reference to another file; it enables the file’s data to have more than one name in different locations in the same file system. Applications treat a hard link as a real file. If the original file is deleted after a hard link is created, all its contents will still be available in the linked file. This is because the inode of a hard link is the same as its target; in other words, it points to the same object on the file system. Hard links cannot be created between two directories, nor can they be created between two files in different file systems.

Soft Links (Symbolic Links)

A symbolic link is a reference to a file or directory that can span multiple file systems. If the original file or directory is deleted after a symbolic link is created, then the original content is lost. This is because the inode of a symbolic link is different than its target; in other words, it points to a different object on the file system. A symbolic link is also known as a soft link.

Examples of links

The following is an example of creating a hard link using the ln command, where / backup/backup-report is the target of the link, and ~/backup-report is the link itself:

$ ln /backup/backup-report ~/backup-report

The following is an example of the same, but creating a symbolic link instead of a hard link:

$ ln -s /backup/backup-report ~/backup-report

ln Command Examples

1. To create a link of an existing file:

# ln file.txt file.ln 

2. To create an soft link for the existing file:

# ln -s file.txt file.ln 

3. To make a backup of each destination file:

# ln --backup file.txt file.bk 

4. To allow super user to attempt to create hard linked directories:

# ln -d 

5. To create the links with removing the existing destination files:

# ln -f
# ln --force

6. To prompt whether to remove the destination file:

# ln -i
# ln --interactive 

7. To make hard links to symbolic link references:

# ln -L
# ln --logical 

8. To treat the destination symlink directory as file:

# ln -n
# ln --no-dereference 

9. To make hard links directly to symbolic links:

# ln -P
# ln --physical 

10. To make symbolic links instead of hard links:

# ln -s 

11. To override the usual backup suffix:

# ln -S
# ln --suffix=SUFFIX 

12. To specify the directory to which the directory should be created:

# ln -t
# ln --target-directory=DIRECTORY 

13. To treat the link name as normal file:

# ln -T
# ln --no-target-directory 

14. To print the name of each linked file:

# ln -v
# ln --verbose 

15. To get the help for ln:

# ln --help

16. To get the version info:

# ln --version 

Filed Under: Linux

Some more articles you might also be interested in …

  1. What is Anacron and usage of Anacron in Linux
  2. CentOS / RHEL 6,7 : How to recover an interrupted yum package installation
  3. CentOS / RHEL : How to change password hashing algorithm
  4. How To Change Pacemaker Cluster Heartbeat Timeout In CentOS/RHEL 7
  5. CentOS / RHEL : How to install a specific version of rpm package using YUM
  6. CentOS / RHEL : How to change the UUID of the filesystem
  7. CentOS / RHEL : Resize (extend) non-root EXT3/4 filesystem on non-LVM device (hard disk partition)
  8. Linux OS Service ‘squid’
  9. CentOS / RHEL : How to extend Physical Volume in LVM by extending the Disk Partition used
  10. How to set custom device names using udev in CentOS/RHEL 7

You May Also Like

Primary Sidebar

Recent Posts

  • JavaFX ComboBox: Set a value to the combo box
  • Nginx load balancing
  • nginx 504 gateway time-out
  • Images preview with ngx_http_image_filter_module

© 2022 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright