Site icon NetworkHunt

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

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.

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.

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.

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.

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]

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.

Exit mobile version