A symbolic link is a file that refers to another file (a target file) without pointing directly to the target file: It is a reference to the target file. To dereference a symbolic link means to follow the link to the target file rather than work with the link itself. When you dereference a symbolic link, you end up with a pointer to the file (the filename of the target file). The term no-dereference is a double negative: It means reference. To no-dereference a symbolic link means to work with the link itself (do not dereference the symbolic link).
Many Linux utilities have dereference and no-dereference options, usually invoked by the –L (– –dereference) and the –P (– –no-dereference) options, respectively. Some utilities, such as cp and ls, also have a partial dereference option that is usually invoked by –H. With a –H option, a utility dereferences files listed on the command line only, not files found by traversing the directory hierarchy of a file listed on the command line. This section uses examples to explain each of these options.
Most utilities assume no-dereference as the default. The GNU ls utility, which is used in most Linux distributions does not have a –P (– –no-dereference) option, although the BSD ls utility does. Following, ls with the –l option displays information about the files in the working directory and does not dereference the memoA symbolic link; it displays the symbolic link including the pathname of the file the link points to (the target file). The first character of the memoA line is an l, indicating the line is describing a link; Max created the symbolic link and owns it.
$ ls -l
lrwxrwxrwx. 1 max max 19 01-27 16:19 memoA -> /home/max/sam/memoA
-rw-r--r--. 1 max max 39016 01-27 15:10 memoD
-rw-r--r--. 1 max max 25291 01-27 15:09 memoE
The next command specifies on the command line the file the symbolic link points to (the target file) and displays information about that file. The file type, permissions, owner, and time for the file are different from that of the link. Sam created the file and owns it.
$ ls -l /home/max/sam/memoA
-rw-r--r--. 1 sam sam 38889 01-27 15:07 /home/max/sam/memoA
Next, the –L (– –dereference) option to ls displays information about the files in the working directory and dereferences the memoA symbolic link; it displays the file the link points to (the target file). The first character of the memoA line is a –, indicating the line is describing a regular file; the command displays the same information about memoA as the preceding command.
$ ls -lL
-rw-r--r--. 1 sam sam 38889 01-27 15:07 memoA
-rw-r--r--. 1 max max 39016 01-27 15:10 memoD
-rw-r--r--. 1 max max 25291 01-27 15:09 memoE
When you do not specify a symbolic link as an argument to ls, the –H (partial dereference; this short option has no long version) option displays the same information as the –l option.
$ ls -H
lrwxrwxrwx. 1 max max 19 01-27 16:19 memoA -> /home/max/sam/memoA
-rw-r--r--. 1 max max 39016 01-27 15:10 memoD
-rw-r--r--. 1 max max 25291 01-27 15:09 memoE
When you specify a symbolic link as an argument to ls, the –H option causes ls to dereference the symbolic link; it displays information about the file the link points to (the target file; memoA in the example).
$ ls -H memoA
-rw-r--r--. 1 sam sam 38889 01-27 15:07 memoA
In the following example, the shell expands the * to a list of the names of the files in the working directory and passes that list to ls. Specifying an ambiguous file reference that expands to a symbolic link produces the same results as explicitly specifying the symbolic link (because ls does not know it was called with an ambiguous file reference, it just sees the list of files).
$ ls -H *
-rw-r--r--. 1 sam sam 38889 01-27 15:07 memoA
-rw-r--r--. 1 max max 39016 01-27 15:10 memoD
-rw-r--r--. 1 max max 25291 01-27 15:09 memoE