Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

My OS states the following in the man page of cp:

   -H     follow command-line symbolic links

   -L, --dereference
          always follow symbolic links

I am having difficulty figuring out what a "command-line symbolic link" is.

share|improve this question
Have you already It was the first hit when you Google for symbolic link. Did you have some specific questions? Did you not understand some of that? – Zoredache May 8 '13 at 19:05
@Zoredache: It seems to me that the OP knows what a symbolic link is, but doesn't understand the difference between the -H and -L switch. – Dennis May 8 '13 at 19:10
@Zoredache Does your user agent not render emphasis in text? It's pretty clear that the OP is asking not what a symbolic link might be, but where lies the distinction between a command-line symbolic link, and the more usual sort. (There isn't one, but the lousy phrasing in the man page makes it seem as though there might be, and I don't blame the OP for being confused by it; I was as well, and had to work through the example described in my answer to understand what was actually going on.) – Aaron Miller May 8 '13 at 19:21
Yup, that's why I bolded command-line. A Google search didn't reveal anything about that type of symlink. – barrrista May 8 '13 at 19:22
up vote 9 down vote accepted

There's no such thing as a command-line symbolic link. The man page refers to links that occur on the command line, i.e., as arguments to the cp command.

Without the -R switch, the behavior of cp -H and cp -L is identical.

With the -R switch, cp -H only dereferences symbolic links that occur in the cp command itself, while -L also dereferences symbolic links inside the recursively copied directories.

The command

cp -R -H source/ dest/

will copy the directory source and all its contents inside the directory dest. If source is a symlink to a directory, cp will dereference the symlink, i.e., copy the directory source points to. However, if cp encounters any symlinks in source's subdirectories, it will create symlinks in the destination folder.

The command

cp -R -L source/ dest/

behaves similarly. However, it will also dereference symlinks in source's subdirectories, i.e., copy the files those symlinks point to.

share|improve this answer
Thanks. That explains it really well. The confusing part was about "command-line" symbolic links. I guess that means that if I did cp -HR folder mysymlink destinationFolder, then mysymlink would be followed, but not the symbolic links within destination folder. Basically, anything that appeared on the command-line after cp -H or cp -HR gets followed once (because they appear on the command-line), but any other linking after that does not. – barrrista May 8 '13 at 20:28

The -H option to cp modifies the behavior of -R (recursive copy) as follows:

If -H is not passed, and the source path given on the command line is a symbolic link to a directory, then the link will be copied to the destination path.

If -H is passed, and the source path &c., &c., then the link will be dereferenced, and the referenced directory recursively copied to the destination path.

That doesn't help much, I think; perhaps an example will clarify it better. (In ls -pF output, a trailing / indicates a directory, while a trailing @ indicates a symbolic link.)

1  me@box tmp $ for dir in foo bar; do mkdir $dir; done
2  me@box tmp $ ls -pF
bar/ foo/
3  me@box tmp $ touch foo/file
4  me@box tmp $ ln -s foo baz
5  me@box tmp $ ls -pF
bar/ baz@ foo/
6  me@box tmp $ cp -R baz bar
7  me@box tmp $ ls -pF bar
8  me@box tmp $ unlink bar/baz
9  me@box tmp $ cp -HR baz bar
10 me@box tmp $ ls -pFR bar

11 me@box tmp $

See how that works? At line 6, I issued cp -R without the -H option, and the symbolic link baz got copied into bar/. Then, at line 9, I issued cp -HR, and cp dereferenced baz and recursively copied its contents (i.e., the contents of foo/, as per line 4) into bar/.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .