Cheat Sheet #day50 - ln

Cheat Sheet #day50 - ln

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.

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.

ln TARGET LINK_NAME

Example

ln file1.txt link_to_file1.txt

A symbolic link is a special type of file that points to another file or directory. It is similar to a shortcut in Windows.

ln -s TARGET LINK_NAME

Example

ln -s /path/to/original/file1.txt symlink_to_file1.txt

Examples and Use Cases

ln original.txt hardlink.txt
  • This creates a hard link named hardlink.txt that points to original.txt.
ln -s /home/user/original.txt symlink.txt
  • This creates a symbolic link named symlink.txt that points to /home/user/original.txt.
ln -sf new_target.txt existing_link.txt
  • This forces the creation of the link, overwriting existing_link.txt if it already exists.
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.

Did you find this article valuable?

Support Cloud Tuned by becoming a sponsor. Any amount is appreciated!