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.
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communityCan 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.
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