Technical skill

Linux command: ln

Subject: .../LINUX

Give one file another name

Sometimes two paths should reach the same file, or a short name should point to a chosen version of a file. Linux's ln command creates these links. By default it creates a hard link; add -s for a symbolic link. In the two-argument form, the first path is the target and the second is the new name:

ln -- report.txt report-copy.txt
ln -s -- releases/current.conf current.conf

A hard link gives the same stored file another directory entry. Both names refer to one inode, the filesystem record that identifies the file, so writing through either name changes the same contents. Hard links normally cannot cross filesystem boundaries, and Linux prevents ordinary users from making them for directories. Removing one name removes only that entry. The data remains while another hard link or an open file reference exists, and is reclaimed after both are gone.

A symbolic link is a separate file that stores a target path. It can point to a directory, cross filesystems, or name a target that does not exist yet. A relative target is resolved from the directory containing the link, not from the shell's current directory. Moving the link or target can break a relative link. An absolute target still names the same path when the link moves, as long as the target stays there. Removing a symbolic link removes the pointer, not the target.

Check what each name points to

Use a hard link when two names should refer to the same file on one filesystem. Use a symbolic link for a path relationship, such as current.conf selecting one versioned configuration file. These commands show the hard links' inode numbers and the symlink's stored target:

ls -li report.txt report-copy.txt
readlink current.conf

The hard-linked names should show the same inode number in ls -i. readlink prints the target path stored in a symlink; ls -l marks a symlink and displays that text. A dangling symlink can be intentional, so a target that does not currently resolve does not by itself mean the command failed.

ln normally refuses to replace a destination that already exists. GNU ln -i asks before replacing it; -f removes the destination without asking. In automation, -sf can replace a regular file or existing link at that path. When the destination is a directory, ln may use the target's basename for the new entry. Include the full link name when the exact destination matters.

Interactive exercises

Materials

Skillsets
Skillset Devops preview
image descriptionDevops
by Dio
3 users
Skillset Linux administrator preview
image descriptionLinux administrator
by Infinite Education
3080 users
Skillset Devops preview
image descriptionDevops
by Ajay Kumar
2004 users
Skillset Skyeng Senior Backend Developer preview
image descriptionSkyeng Senior Backend Developer
by Artem Naumenko (slack2)
39 users
Skillset Linux for backend developer preview
image descriptionLinux for backend developer
by Artem Naumenko (fb)
16 users
Skillset Skyeng QA Automation Hard Skillset v09.21 preview
image descriptionSkyeng QA Automation Hard Skillset v09.21
by Aleksandra Kulikova
16 users
Show more
Do you know that ...?
Any useful or interesting facts about the skill. Any peace of information would be useful