Common Operations of Symbolic Links on Linux
Posted on In LinuxSymbolic link or soft link files are very common and useful on Linux/Unix systems. It works as a alias file for a file. You can create a symbolic links and it can operate transparently for most operations just as normal files. Programs that read or write to files named by a symbolic link behaves as if operating directly on the target file.
Let’s look at the common techniques and tools to play with symbolic link files on Linux.
Table of Contents
Create a symbolic link
To create a symbolic link file in the current directory to a target file, say, ../target
:
$ ln -s ../target
This will create a symbolic link file target
in the current directory.
You can also give it a different name, say tt
:
$ ln -s ../target ./tt
Check whether a file is a symbolic link in Bash
In Bash script, we can easily test whether a file is a symbolic link by the file test operator -h
. For example:
if [ -h $file ]; then echo "a symbolic link" fi
Find out the target of a symbolic link file
Use the [[man:1|readlink|readlink]] command.
To find out the target of the ./tt
we created:
$ readlink ./tt
With these basic tools, we can combine them with scripts to be some powerful tools.
To find out the canonical target (dereference every symlink in every component) of the ./tt
we created:
$ readlink -f ./tt
First command is wrong
It is wrong because … ?