link

file_managementLinux/Unix
The link command is one of the most frequently used commands in Linux/Unix-like operating systems. link Create a hard link to a file

Quick Reference

Command Name:

link

Category:

file_management

Platform:

Linux/Unix

Basic Usage:

link [options] [arguments]

Common Use Cases

    Syntax

    link source_file target_file

    Options

    Option Description
    --help Display help information and exit
    --version Display version information and exit
    Note: The link command in most systems has few or no options, with just the source and target arguments being required.

    Examples

    How to Use These Examples

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

    # Basic Examples Basic
    link file1.txt file2.txt
    Create a hard link named file2.txt pointing to file1.txt.
    link /path/to/source /path/to/target
    Create a hard link with absolute paths.
    # Advanced Examples Advanced
    link $(pwd)/data.txt /tmp/data_link Create a hard link using command substitution for the source path. if ! link source.txt target.txt; then echo "Link failed"; fi Handle errors when creating a link. link -v source.txt target.txt Use verbose output (not available on all systems). link source.txt target.txt && echo "Link created successfully" Confirm successful link creation.

    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

    The 'link' command creates a hard link between files, which is essentially an additional directory entry (filename) that points to the same inode (the data structure that stores all the information about a file except its name). It's a simple, low-level utility that provides an alternative to the more commonly used 'ln' command for creating links. Key features of the link command: 1. Hard Links Only: Unlike 'ln', which can create both hard and symbolic links, the 'link' command creates only hard links. 2. Simplicity: It has a straightforward syntax with just two arguments: the source file and the target link name, making it concise for scripts and command lines. 3. Minimal Overhead: As a simple utility focused on one task, it has less overhead than more feature-rich alternatives. 4. Direct System Call: The command essentially makes a direct wrapper around the link() system call, providing access to this fundamental filesystem operation. 5. Exit Status: Returns 0 on success and non-zero on failure, making it suitable for use in conditional expressions in scripts. 6. No Directory Links: Like all hard links, it cannot link directories, only regular files. 7. Same Filesystem Requirement: Hard links can only be created within the same filesystem, as they reference the same inode. Key limitations and considerations when using hard links: - Hard links cannot span different filesystems or partitions. - Hard links cannot reference directories (only files). - Deleting one hard link doesn't affect the file content until all links are removed. - Hard links are indistinguishable from the original file; they share the same permissions, ownership, and modification times. - Changes to the file content through any hard link affect all links, as they all point to the same data. Common use cases for the link command include: - Creating backup references to important files - Setting up multiple access points to the same file data - Creating efficient file organization systems without duplicating content - Implementing simple versioning systems - Scripting scenarios where a minimal command with predictable behavior is preferred While 'ln' is more commonly used due to its additional features (particularly symbolic link support), the 'link' command remains valuable for its simplicity and direct mapping to the underlying system call, especially in minimalist environments or when writing portable shell scripts that need to create hard links with minimal overhead.

    Related Commands

    These commands are frequently used alongside link 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 link command works in different scenarios.

    $ link
    View All Commands