Creating links in UNIX/Linux filesystems Which command is used to create links (hard or symbolic) between files or paths?

Difficulty: Easy

Correct Answer: ln

Explanation:

Introduction / Context:Links allow multiple directory entries to reference the same data (hard links) or act as pointers to other pathnames (symbolic links). They are fundamental to UNIX/Linux filesystem flexibility, enabling aliasing, versioned paths, and shared resources without duplication.

Given Data / Assumptions:

  • Filesystem supports hard links (most native UNIX filesystems do; not across filesystems for hard links).
  • Symbolic links can cross filesystems and point to directories.
  • Proper permissions exist for creating the link at the destination.

Concept / Approach:The ln command creates links. By default it creates hard links; with -s it creates symbolic links. A hard link increases a file’s link count and references the same inode. A symbolic link stores a pathname and resolves at access time, useful for redirects and version management.

Step-by-Step Solution:Create a hard link: ln original.txt alias.txtCreate a symbolic link: ln -s /opt/app/current/bin/app ~/bin/appVerify with ls -li (inodes) and ls -l (symlink arrow).Remove links safely; deleting a symlink does not delete the target file.

Verification / Alternative check:Use stat to view link counts on hard-linked files. Confirm that editing either hard-linked name changes the same underlying data.

Why Other Options Are Wrong:lk (Option A) is not a standard link command.cp (Option C) copies data, it does not create links.tar (Option D) archives; it does not create filesystem links.

Common Pitfalls:

  • Trying to hard-link directories (usually disallowed) or across filesystems (not possible).
  • Breaking symlinks by moving targets; relative symlinks often survive moves better within a tree.
  • Overwriting existing symlinks without using ln -sf carefully.

Final Answer:ln

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion