PowerShell / PowerShell

PowerShell for every system!
https://microsoft.com/PowerShell
MIT License
43.66k stars 7.08k forks source link

'Copy-Item -Recursive <source folder> <target folder>' doesn't copy symlink as 'cp -r' does on Linux #5240

Closed daxian-dbw closed 2 weeks ago

daxian-dbw commented 6 years ago

Steps to reproduce

echo test > /tmp/test.txt
mkdir /tmp/source > $null
New-Item -Force -ItemType SymbolicLink -Target /tmp/test.txt -Path /tmp/source/link > $null
Copy-Item -Recurse /tmp/source /tmp/dest
cp -r /tmp/source /tmp/dest2

Expected behavior

Copy-Item -Recursive should copy the symbolic link to /tmp/dest, just like cp -r

ls /tmp/dest -l
link -> /tmp/test.txt

Actual behavior

bash:80> ls /tmp/dest -l
total 4
-rw-rw-r-- 1 auser auser 12 Oct 25 16:30 link
bash:81> ls /tmp/dest2 -l
total 0
lrwxrwxrwx 1 auser auser 13 Oct 25 16:32 link -> /tmp/test.txt

Environment data

> $PSVersionTable
Name                           Value                                                               
----                           -----                                                               
PSVersion                      6.0.0-beta.8                                                        
PSEdition                      Core                                                                
GitCommitId                    v6.0.0-beta.8                                                       
OS                             Linux 4.10.0-37-generic #41~16.04.1-Ubuntu SMP Fri Oct 6 22:42:59...
Platform                       Unix                                                                
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}                                             
PSRemotingProtocolVersion      2.3                                                                 
SerializationVersion           1.1.0.1                                                             
WSManStackVersion              3.0   
mklement0 commented 6 months ago

That is indeed problematic.

cp -R's (sensible) default behavior, as you've shown, is to copy symlinks as links, instead of copying their targets, which with directory targets could result in a lot of unwanted content getting copied.
Wanting to copy link targets requires opt-in via -L, for instance.

While on Windows this default behavior is unfortunately reversed in xcopy.exe and robocopy.exe, these utilities at least offer opt-ins for copying links as such: /B and /SL, respectively.

It looks like, due to its age, this issue has fallen through the cracks; please label it appropriately so it can be revived.

microsoft-github-policy-service[bot] commented 3 weeks ago

This issue has not had any activity in 6 months, if there is no further activity in 7 days, the issue will be closed automatically.

Activity in this case refers only to comments on the issue. If the issue is closed and you are the author, you can re-open the issue using the button below. Please add more information to be considered during retriage. If you are not the author but the issue is impacting you after it has been closed, please submit a new issue with updated details and a link to this issue and the original.