ln
Command Cheatsheet
The ln
command in Unix/Linux is used to create links between files. There are two types of links: hard links and symbolic (soft) links.
Syntax
ln [OPTION]... TARGET [LINK_NAME]
Common Options
-s
: Create a symbolic link instead of a hard link.-f
: Force the creation of the link by removing any existing destination files.-i
: Prompt before overwriting an existing file.-v
: Verbosely print the name of each linked file.-n
: Treat the destination that is a symlink to a directory as if it were a normal file.
Hard Links
A hard link is essentially an additional name for an existing file. It points directly to the inode of the file, and both the original and the hard link are indistinguishable.
Create a Hard Link
ln TARGET LINK_NAME
Example
ln file1.txt link_to_file1.txt
Symbolic Links
A symbolic link is a special type of file that points to another file or directory. It is similar to a shortcut in Windows.
Create a Symbolic Link
ln -s TARGET LINK_NAME
Example
ln -s /path/to/original/file1.txt symlink_to_file1.txt
Examples and Use Cases
Creating a Hard Link
ln original.txt hardlink.txt
- This creates a hard link named
hardlink.txt
that points tooriginal.txt
.
Creating a Symbolic Link
ln -s /home/user/original.txt symlink.txt
- This creates a symbolic link named
symlink.txt
that points to/home/user/original.txt
.
Overwriting an Existing Link
ln -sf new_target.txt existing_link.txt
- This forces the creation of the link, overwriting
existing_link.txt
if it already exists.
Creating a Verbose Symbolic Link
ln -sv /path/to/target verbose_symlink
- This creates a symbolic link and verbosely prints the name of each linked file.
Linking to a Directory
ln -s /home/user/documents my_docs
- This creates a symbolic link named
my_docs
that points to the/home/user/documents
directory.
This cheatsheet provides a quick reference to the most common usages and options for the ln
command. For more detailed information, you can always refer to the ln
man page by typing man ln
in your terminal.