user283145
user283145

Reputation:

Is this correct way to copy symlinked directory in bash?

I have directory a that is symlinked somewhere. I want to copy its contents to directory b. Doesn't the following simple solution break in some corner cases (e.g. hidden files, exotic characters in filenames, etc.)?

mkdir b
cp -rt b a/*

Upvotes: 2

Views: 1303

Answers (1)

jordanm
jordanm

Reputation: 34954

Simply adding a trailing '/' will follow the symlink and copy the contents rather than the link itself.

cp -a symlink/ dest

Bash globbing does not choke on special characters in filenames. This is the reason to use globbing, rather than parsing the output of a command such as ls. The following would also be fine.

shopt -s dotglob
mkdir -p dest
cp -a symlink/* dest/

Upvotes: 3

Related Questions