0

Can someone elaborate on this description in the official docs?

When copying from a symbolic link, cp normally follows the link only when not copying recursively or when --link (-l) is used.

1 Answer 1

2

Say you have a symlink foo/bar -> baz:

% mkdir foo
% ln -s baz foo/bar
% tree foo
foo
└── bar -> baz

1 directory, 1 file

If you copy foo/baz directly, cp will follow the symlink and copy the target:

% cp foo/bar .
cp: cannot stat 'foo/bar': No such file or directory

Here, cp isn't reaching foo/bar by copying recursively, so the target file will be copied.

If, instead, you copy foo and tell cp to recurse, it will copy the symlink itself, and not the target:

% cp -r foo foo2
% tree foo2
foo2
└── bar -> baz

1 directory, 1 file

And if you try to copy recursively but also set the -l option, cp will again try to follow the symlink:

% cp -rl foo bar3
cp: cannot stat 'foo/bar': No such file or directory

You must log in to answer this question.

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