Hard and symbolic links Weight

From Mintarc Forge

Examples:

  • ln file1 file2: Create a hard link named file2 pointing to file1
  • ln -s source target: Create a symbolic link named target pointing to source
  • ln -f existing_file new_link: Force creation of link, overwriting if it exists

Examples:

  • ls -l: Long listing format, shows link count and symlink targets
  • ls -i: Display inode number for each file
  • ls -lF: Append indicator to entries (/ for directories, @ for symlinks)

Hard links:

  • Share the same inode as the original file
  • Cannot span file systems
  • Cannot link to directories

Symbolic links:

  • Contain a path to the target file
  • Can span file systems
  • Can link to directories
  • Broken if target file is moved or deleted

Copying vs. Linking:

Copying (cp):

  • Creates a new file with a new inode
  • Changes to copy don't affect original
  • Takes up additional disk space

Linking (ln):

  • Hard link: New name for existing inode
  • Symbolic link: New file pointing to existing file
  • Changes affect all hard links
  • Symbolic links don't use extra space for file content

Version control:

  • ln -s current_version stable: Create a symlink named "stable" pointing to current version
  • ln -sf new_version stable: Update "stable" symlink to point to new version

Configuration management:

  • ln -s /etc/config.default /etc/config: Use default config as current
  • ln -sf /etc/config.custom /etc/config: Switch to custom config

Backup and restore:

  • ln important_file backup/: Create hard link for backup without duplicating data
  • ln -s /mnt/backup/file restored_file: Restore file using symlink to backup location