Skip to content

Latest commit

 

History

History
101 lines (71 loc) · 2.42 KB

File metadata and controls

101 lines (71 loc) · 2.42 KB

The ln command

The ln command is used to create links between files in Linux. It can create both hard links and symbolic (soft) links, which are essential for file system management and organization.

Syntax

ln [options] target linkname
ln [options] target... directory

Types of Links

Hard Links

  • Point directly to the file's inode
  • Cannot span across different filesystems
  • Cannot link to directories
  • If original file is deleted, hard link still contains the data

Symbolic (Soft) Links

  • Point to the file path (like shortcuts)
  • Can span across different filesystems
  • Can link to directories
  • If original file is deleted, symbolic link becomes broken

Options

Some popular option flags include:

-s	Create symbolic (soft) links instead of hard links
-f	Force creation by removing existing destination files
-v	Verbose output, show what's being linked
-n	Treat destination as normal file if it's a symlink to a directory
-r	Create relative symbolic links
-t	Specify target directory for links

Examples

  1. Create a hard link
ln file.txt hardlink.txt
  1. Create a symbolic link
ln -s /path/to/original/file.txt symlink.txt
  1. Create a symbolic link to a directory
ln -s /var/log logs
  1. Create multiple symbolic links in a directory
ln -s /usr/bin/python3 /usr/bin/gcc /usr/local/bin/
  1. Create a relative symbolic link
ln -sr ../config/app.conf current_config
  1. Force create a link (overwrite existing)
ln -sf /new/target existing_link
  1. Create links with verbose output
ln -sv /source/file /destination/link

Use Cases

  • Creating shortcuts to frequently used files or directories
  • Maintaining multiple versions of configuration files
  • Organizing files without duplicating storage space
  • Creating backup references to important files
  • Setting up development environments with shared libraries

Important Notes

  • Use ls -l to see if a file is a symbolic link (indicated by ->)
  • Use ls -i to see inode numbers for hard links
  • Be careful with symbolic links to avoid creating circular references
  • Hard links share the same inode and disk space
  • Symbolic links take minimal disk space (just the path information)

The ln command is essential for efficient file system organization and is widely used in system administration and development workflows.

For more details, check the manual: man ln