How can I symlink a file in Linux?

 

In Linux, you can create symbolic links (symlinks) using the ln command. A symlink is a reference to another file or directory, and it allows you to create a shortcut or alias to access the target file or directory. Here's how you can create a symlink with an example:

Syntax:

bash
ln -s target source
  • target: This is the existing file or directory you want to link to.
  • source: This is the name of the symlink you want to create.

Example: Suppose you have a file named original.txt, and you want to create a symlink called shortcut.txt that points to it.

bash
ln -s original.txt shortcut.txt

After running this command, a symlink named shortcut.txt will be created in the same directory where you executed the ln command. You can verify this by listing the contents of the directory using ls -l:

bash
ls -l

The output will show something like this:

sql
-rw-r--r-- 1 user user 0 Sep 2 10:00 original.txt lrwxrwxrwx 1 user user 11 Sep 2 10:01 shortcut.txt -> original.txt

In the output:

  • original.txt is the target file.
  • shortcut.txt is the symlink that points to original.txt.
  • The arrow (->) indicates the link.

You can now access original.txt through shortcut.txt, and changes made to original.txt will be reflected in shortcut.txt since it's just a reference to the same file. Symlinks are useful for creating shortcuts to files and directories, especially when you want to avoid duplicating data or need to reference files in different locations.

Comments