ln

file managementlinux
The ln command is one of the most frequently used commands in Linux/Unix-like operating systems. ln The ln command is used to create links between files in Linux/Unix systems. It can create both hard links and symbolic (soft) links, allowing multiple filenames to point to the same file or directory.

Quick Reference

Command Name:

ln

Category:

file management

Platform:

linux

Basic Usage:

ln [options] [arguments]

Common Use Cases

    Syntax

    ln [OPTION]... [-T] TARGET LINK_NAME
    ln [OPTION]... TARGET
    ln [OPTION]... TARGET... DIRECTORY
    ln [OPTION]... -t DIRECTORY TARGET...

    Options

    Option Description
    -s, --symbolic Make symbolic links instead of hard links
    -f, --force Remove existing destination files
    -b, --backup Make a backup of each existing destination file
    -n, --no-dereference Treat destination that is a symlink to a directory as if it were a normal file
    -i, --interactive Prompt whether to remove destinations
    -r, --relative Create symbolic links relative to link location
    -t, --target-directory=DIRECTORY Specify the DIRECTORY in which to create the links
    -T, --no-target-directory Treat LINK_NAME as a normal file always
    -v, --verbose Print name of each linked file

    Examples

    How to Use These Examples

    The examples below show common ways to use the ln command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

    #

    Basic Examples:

    ln file.txt link.txt

    Creates a hard link named link.txt that points to file.txt. Both filenames will access the same data.

    ln -s file.txt symlink.txt

    Creates a symbolic link named symlink.txt that points to file.txt. The symbolic link is a special file that refers to the target file.

    ln -s /path/to/directory linkname

    Creates a symbolic link to a directory. This is only possible with symbolic links, not hard links.

    ln -s /absolute/path/to/file /absolute/path/to/symlink

    Creates a symbolic link using absolute paths. This ensures the link works regardless of the current working directory.

    Advanced Examples:

    ln -sf target.txt existing_symlink.txt

    Creates or replaces a symbolic link. The -f option forces the creation by removing the destination file if it exists.

    ln -s file1.txt file2.txt destination_directory/

    Creates symbolic links to multiple files in the specified destination directory. The links will have the same names as the original files.

    ln -b original.txt backup.txt

    Creates a hard link with backup. If backup.txt already exists, it will be backed up before the new link is created.

    ln -sr ../source.txt destination/symlink.txt

    Creates a symbolic link using relative paths. The -r option makes the link relative to the link's location, which is useful when directories might be moved together.

    ln -sf nonexistent.file symlink.txt

    Forces the creation of a symbolic link even if the target file doesn't exist yet. This creates a "dangling" symlink that will work once the target file is created.

    Try It Yourself

    Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

    Understanding Syntax

    Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

    Notes

    Understanding Links:

    • Hard Links: Reference the same inode as the original file, share the same content, and only work on the same filesystem. Cannot link to directories.
    • Symbolic Links (Symlinks): Special files that point to another file or directory, can span filesystems, and can link to directories.

    Common Use Cases:

    • Creating aliases for commands or files
    • Maintaining multiple versions of configuration files
    • Creating shortcuts to frequently accessed directories
    • Simplifying complex directory structures
    • Supporting application plugins and extensions

    Tips:

    • Use symbolic links (-s) for most purposes, especially when linking to directories
    • Hard links save disk space as they point to the same data
    • Deleting a hard link reduces the link count but preserves the file until the last link is removed
    • If the original file of a symlink is deleted, the symlink becomes "broken" and points to nothing
    • Use absolute paths in symlinks when the link and target aren't in related directories
    • Use relative paths (-r) when creating links that might move together

    Limitations:

    • Hard links cannot span different filesystems or partitions
    • Hard links cannot be created for directories (except by the superuser, and even then it's not recommended)
    • Symbolic links can cause confusion when used in scripts without proper error handling

    Related Commands

    These commands are frequently used alongside ln or serve similar purposes:

    Use Cases

    Learn By Doing

    The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the ln command works in different scenarios.

    $ ln
    View All Commands