[Linux] Create Hard Links and Symbolic Links with the ln Command

目次

Overview

The ln command creates “links” to files or directories in the Linux file system. You can create two types of links: “Symbolic Links,” which are like Windows shortcuts, and “Hard Links,” which allow a single file to have multiple names or access paths. These are frequently used to group log files in one place or to switch between different versions of an application by updating a “current” path.

Specifications (Arguments and Options)

Syntax

ln [options] target_file link_name
ln [options] target_file directory_name

Main Arguments and Options

OptionDescription
-sCreates a symbolic link. This is the most common choice.
-fForces the link creation by overwriting an existing file with the same name.
-iAsks for confirmation (interactive) before overwriting an existing file.
-LIf the target is a symbolic link, creates a hard link to the actual file it points to.
--backupCreates a backup of any existing file with the same name before creating the link.
-S <suffix>Sets the suffix for backup files (the default is ~).

Basic Usage

If you do not specify any options, the command creates a “Hard Link.” A hard link points to the exact same “inode” (the actual data on the disk) as the original file. If you change the content of one, the other changes too. If you delete one name, the data stays on the disk as long as the other name exists.

Command

# Create a hard link named hardlink_data.csv for target_data.csv
ln target_data.csv hardlink_data.csv

Execution Result (Verification)

Using the ls -li command shows that the inode numbers (the numbers at the start of the line) match. This is different from a normal copy (cp).

# Compare inode numbers using the -i option
ls -li
1234567 -rw-r--r-- 2 user user 1024 Jan 18 10:00 hardlink_data.csv
1234567 -rw-r--r-- 2 user user 1024 Jan 18 10:00 target_data.csv
9876543 -rw-r--r-- 1 user user 1024 Jan 18 10:05 copied_data.csv

Note: both hardlink_data.csv and target_data.csv point to inode 1234567.

Practical Commands

Creating Symbolic Links

This is the most common use. It creates a reference (shortcut) to a file or directory located elsewhere. You must use this option when linking to a directory.

# Create a symbolic link in the current directory for the Nginx access log
ln -s /var/log/nginx/access.log ./access_log_symlink

# Verify the link (the -l option shows an arrow)
ls -l access_log_symlink
lrwxrwxrwx 1 user user 25 Jan 18 10:10 access_log_symlink -> /var/log/nginx/access.log

Confirmation Before Overwriting

If a file with the same name already exists, use this to avoid overwriting it by mistake.

# Ask for confirmation before replacing config.conf
ln -i new_config.conf config.conf
ln: replace 'config.conf'? y

Updating Links with Backups

When placing a new configuration file, you can automatically rename the old one as a backup instead of deleting it.

# Rename the existing app_settings.json to .bak and create a new link
ln -s --backup -S .bak new_settings.json app_settings.json

Customization Points

  • Links to Directories: Hard links to directories are usually forbidden to keep the system stable. Always use -s for directories.
  • Omitting the Link Name: If you run ln -s /path/to/file without a second name, a link with the same name as the target is created in your current directory.
  • Forced Updates: In deployment scripts, ln -sf (force) is often used to quickly point a link to a new version of a file.

Important Notes

  • Relative Paths for Symbolic Links: When using relative paths (e.g., ln -s ../target.txt link.txt), the path must be relative to the “location of the link file,” not where you are currently running the command. Using absolute paths (starting with /) is often safer.
  • Hard Link Limits: You cannot create hard links across different disk partitions or file systems. Use symbolic links in these cases.
  • Deleting the Original File: If the original file is deleted, a symbolic link becomes “broken.” A hard link will still keep the data accessible until all links pointing to it are removed.

Application (Deployment)

This method is widely used for web application deployments. You store different versions in separate directories and switch the current symbolic link to point to the newest version instantly.

# Force the 'current' link to point to version 2.0
# The -n option is important when the target is a directory
ln -sfn /opt/app/v2.0 /opt/app/current

Accessing /opt/app/current will now point to the v2.0 directory.

Summary

The ln command is a vital tool for making file paths more convenient in Linux. Symbolic links (-s) are especially useful for accessing deep log folders or moving large directories to different partitions. Understanding the difference between hard links (referencing the data) and symbolic links (referencing the path) is key to effective system management.

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

私が勉強したこと、実践したこと、してることを書いているブログです。
主に資産運用について書いていたのですが、
最近はプログラミングに興味があるので、今はそればっかりです。

目次