#!/linuxSucks/Symbolic links in Linux systems
Apr 30, 2020Symbolic link, also know as a Symlink or soft link. Is a kind of shortcut that point to a specific file or directory.
Links Types
There are two types of links in Linux/UNIX systems:
- Hard links. You can think a hard link as an additional name for an existing file. Hard links are associating two or more file names with the same node. You can create one or more hard links for a single file. Hard links cannot be created for directories and files on a different filesystem or partition.
- Soft links. A soft link is something like a shortcut in Windows. It is an indirect pointer to a file or directory. Unlike a hard link, a symbolic link can point to a file or a directory on a different filesystem or partition.
How to Use the ln Command
ln is a command-line utility for creating links between files. By default, the ln command creates hard links. To create a symbolic link use, the -s (–symbolic) option.
The ln command syntax for creating symbolic links is as follows:
ln -s [OPTIONS] FILE LINK
Symlink To a File
To create a symbolic link to a given file, run:
ln -s source_file symbolic_link
The symbolic_link parameter is optional. If you do not specify the symbolic link, the ln command will create a new link in your current directory:
Symlinks To a Directory
To create a symbolic link to a directory is the same as when creating a symbolic link to a file, run:
ln -s /mnt/my_drive/anime ~/Videos/anime
Overwriting Symlinks
If you try to create a symbolic link that already exists, the ln command will print an error message.
ln -s my_file.txt my_link.txt
ln: failed to create symbolic link 'my_link.txt': File exists
To overwrite the destination path of the symlink, use the -f (–force) option.
ln -sf my_file.txt my_link.txt
Check if symlink was created
To verify that the symlink was successfully created, use the ls command:
ls -l
The output will look something like this:
my_link.txt -> my_file.txt
Removing or Deleting Symlinks
To delete/remove symbolic links use either the unlink or rm command. The syntax is simple:
unlink symlink_to_remove
Removing a symbolic link using the rm command:
rm symlink_to_remove
No matter which command you use, when removing a symbolic link not append the / trailing slash at the end of its name.
If you delete or move the source file to a different location, the symbolic file will be left broken and should be removed.