Commands traversing a file tree.
The following commands either optionally or always traverse file trees: chflags(1), chgrp(1), chmod(1), cp(1), du(1), find(1), ls(1), pax(1), rm(1), tar(1) and chown(8). It is important to realize that the following rules apply equally to symbolic links encountered during the file tree traversal and symbolic links listed as command line arguments.
The first rule applies to symbolic links that reference files that are not of type directory. Operations that apply to symbolic links are performed on the links themselves, but otherwise the links are ignored.
The command ""rm -r slink directory"" will remove "slink", as well as any symbolic links encountered in the tree traversal of "directory", because symbolic links may be removed. In no case will rm affect the file which "slink" references in any way.
The second rule applies to symbolic links that reference files of type directory. Symbolic links which reference files of type directory are never "followed" by default. This is often referred to as a "physical" walk, as opposed to a "logical" walk (where symbolic links referencing directories are followed).
As consistently as possible, you can make commands doing a file tree walk follow any symbolic links named on the command line, regardless of the type of file they reference, by specifying the -H (for "half-logical") flag. This flag is intended to make the command line name space look like the logical name space. (Note, for commands that do not always do file tree traversals, the -H flag will be ignored if the -R flag is not also specified.)
For example, the command ""chown -HR user slink"" will traverse the file hierarchy rooted in the file pointed to by "slink". Note, the -H is not the same as the previously discussed -h flag. The -H flag causes symbolic links specified on the command line to be dereferenced both for the purposes of the action to be performed and the tree walk, and it is as if the user had specified the name of the file to which the symbolic link pointed.
As consistently as possible, you can make commands doing a file tree walk follow any symbolic links named on the command line, as well as any symbolic links encountered during the traversal, regardless of the type of file they reference, by specifying the -L (for "logical") flag. This flag is intended to make the entire name space look like the logical name space. (Note, for commands that do not always do file tree traversals, the -L flag will be ignored if the -R flag is not also specified.)
For example, the command ""chown -LR user slink"" will change the owner of the file referenced by "slink". If "slink" references a directory, chown will traverse the file hierarchy rooted in the directory that it references. In addition, if any symbolic links are encountered in any file tree that chown traverses, they will be treated in the same fashion as "slink".
As consistently as possible, you can specify the default behavior by specifying the -P (for "physical") flag. This flag is intended to make the entire name space look like the physical name space.
For commands that do not by default do file tree traversals, the -H , -L and -P flags are ignored if the -R flag is not also specified. In addition, you may specify the -H , -L and -P options more than once; the last one specified determines the commands behavior. This is intended to permit you to alias commands to behave one way or the other, and then override that behavior on the command line.
The ls(1) and rm(1) commands have exceptions to these rules. The rm command operates on the symbolic link, and not the file it references, and therefore never follows a symbolic link. The rm command does not support the -H , -L or -P options.
To maintain compatibility with historic systems, the ls command acts a little differently. If you do not specify the -F , -d or -l options, ls will follow symbolic links specified on the command line. If the -L flag is specified, ls follows all symbolic links, regardless of their type, whether specified on the command line or encountered in the tree walk.
SEE ALSO
chflags(1), chgrp(1), chmod(1), cp(1), du(1), find(1), ln(1), ls(1), mv(1), pax(1), rm(1), tar(1), lchflags(2), lchmod(2), lchown(2), lstat(2), lutimes(2), readlink(2), rename(2), symlink(2), unlink(2), fts(3), remove(3), chown(8)