How to Create a Symlink for a File or Directory in Linux

To Create a Symlink for a File or Directory in Linux, First, we need to understand what Symlink is and how it works.

Table of Contents

  1. What is a Symlink?
  2. Create a Symlink for a File in Linux:
  3. Create Symlink for a Directory in Linux:
  4. Force Overwriting of Existing Symlinks:
  5. How to Remove a Symlink:

Symlinks (symbolic links) are special types of files in Linux/Unix like systems, these files act as a pointer or reference (shortcut) to another file or directory. Thus, symlinks allow users to access a file or directory from a different location without duplicating the original content. Symlink works the same way as shortcuts in Windows, as they both depend on the existence of the original file, such that if the original file is deleted the symlink becomes invalid or broken. A symlink can point to files or directories located anywhere on the file system, even across partitions or network file systems.

  • A symlink contains a reference to the path of another file or directory, called the target file.
  • When the user accesses a symlink, the operating system redirects operations to the target file or directory.
  • Cross-File System Efficiency: Symlinks provide an efficient way to link files across different file systems.
  • Directory Linking: Symlinks can link to directories, a feature not supported by hard links.
  • Multiple Access Points: Symlinks allow multiple access points to a file without duplicating it. Since they do not duplicate content, they save storage space.
  • Direct Kernel Access: The kernel directly accesses the original file when a symlink is used, ensuring seamless navigation.
  • Dependency on Original Files: If the original file is deleted or moved, the symlink becomes unusable.
  • Not a Direct Link: Unlike hard links, symlinks do not link directly to the file data but reference the file name.
  • Increased Access Time: Accessing a symlink introduces an additional lookup step to find the target.
  • Circular References: Improper use of symlinks can result in circular dependencies that confuse software or users.
AspectSymlinksHard Links
Path or Data ReferencePoints to the file’s path.Points to the file’s data (inode).
Cross-File SystemSupported.Not supported.
DirectoriesCan link to directories.Cannot link to directories.
Broken LinksBecomes invalid if the target is removed.Remains functional unless all links are removed.
Inode SharingUnique inode.Same inode as the original file.

Step 1: Create a File

Use the command below to create a new file:

$> touch [FILENAME]

The touch command creates an empty file.

create a file in linux

The ln command on Linux is used to create a link between files or directories in Linux.

$> ln [option] [original file] [link file]

Original file: The original file or directory to which a link is to be created.

link file: The name of the new link file. If not specified, the link file will be created in the current directory with the same name as the target.

Use the command below to create a symbolic link:

$> ln -s [original file] [symbolic link file]

-s : Specifies that it is a symbolic link.

Create a Symlink for a File  in Linux

Use the command below to check the symlink:

$> ls -l -a

-l : Long listing format, provides detailed information about each file/directory.

-a : Lists all files, including hidden files.

verify created symlink for file in linux

Users can access the symlink similarly to the original file, such as:

$> cat [symlink file name]

The cat command will display the content of the symlinked file.

Step 1: Create a Directory

Use the command below to create a directory:

$> mkdir [DIRECTORYNAME]

The mkdir the command creates an empty directory.

Use the command below to create a symbolic link:

$> ln -s [original directory] [symbolic link directory]
Create a Symlink for a Directory in Linux

Use the ls command to confirm the symlink:

$> ls -l -a

If a symlink already exists and the user tries to create another one with the same name, an error occurs:

$> ln -s [original file] [existing symlink]

Thus, to overwrite an existing symlink file use -f or --force Option.

$> ln -s -f [original file] [existing symlink]

To delete or unlink a symlink, use either the rm or unlink command:

$> rm [symlink file]

$> unlink [symlink file]

Creating Symlinks (Symbolic links) for files and directories in Linux/UNIX are powerful tools for managing files and directories across different locations. They act like pointers, offering efficient file linking, access to directories, and multiple file access points. However, they rely on the existence of the original file, which can make them fragile. By understanding their creation, usage, and limitations, you can effectively use symlinks for file and directory management in Linux/UNIX environments.