Bash Wiki
Posts (Latest 30 updated) :
Read all
Contents:
  1. [Linux] Bash ln Uso equivalente: Create links between files
    1. Overview
    2. Usage
    3. Common Options
    4. Common Examples
      1. Creating a Hard Link
      2. Creating a Symbolic Link
      3. Forcing Link Creation
      4. Verbose Output
    5. Tips

[Linux] Bash ln Uso equivalente: Create links between files

Overview

The ln command in Bash is used to create links between files. It allows users to create both hard links and symbolic (soft) links, enabling multiple references to a single file in the filesystem.

Usage

The basic syntax of the ln command is as follows:

ln [options] [source_file] [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.
  • -n: Treat the destination as a normal file if it is a symbolic link to a directory.
  • -v: Verbosely show what is being done, providing feedback during the link creation process.

Common Examples

To create a hard link named link_to_file that points to original_file.txt, use:

ln original_file.txt link_to_file

To create a symbolic link named link_to_file that points to original_file.txt, use:

ln -s original_file.txt link_to_file

If you want to force the creation of a link and overwrite any existing file named link_to_file, use:

ln -f original_file.txt link_to_file

Verbose Output

To see detailed output while creating a link, you can use the verbose option:

ln -v original_file.txt link_to_file

Tips

  • Use symbolic links when you need to link to directories or when you want the link to point to a file that may move locations.
  • Be cautious with hard links, as they share the same inode and can lead to confusion if you delete the original file.
  • Always check the link after creation using ls -l to ensure it points to the correct target.