Skip to content

Move-Item, Remove-Item don't work with paths containing \, [, ] on unix #3414

Closed

Description

@vors

Steps to reproduce

On macOS or Linux touch '1\2'
Try mv 1\2 2, try rm 1\2

The real-world example of when you are getting \ without knowing it is https://github.com/PowerShell/Microsoft.PowerShell.Archive/issues/11

The problem happens not only with Compress-Archive, but with other archive programs too. So macOS users need a way to bulk-rename files with \ in the name and PS doesn't provide a way to do it at the moment.

UPD: same problem for paths like [foo].

Expected behavior

PowerShell should be able to perform the operations

Actual behavior

Paths are getting normalized to 1/2

rm : Cannot find path '/Users/vors/dev/PowerShell/1/2' because it does not exist.

Environment data

> $PSVersionTable

Name                           Value                                                                                
----                           -----                                                                                
PSVersion                      6.0.0-alpha                                                                          
PSEdition                      Core                                                                                 
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}                                                              
BuildVersion                   3.0.0.0                                                                              
GitCommitId                    v6.0.0-alpha.17                                                                      
CLRVersion                                                                                                          
WSManStackVersion              3.0                                                                                  
PSRemotingProtocolVersion      2.3                                                                                  
SerializationVersion           1.1.0.1                                                                              

Activity

  1. lzybkr commented on Mar 24, 2017

    @lzybkr
    Contributor

    Aren't mv and rm the Unix utilities? Assuming so, we shouldn't assume an argument that looks like a path is actually a Unix path.

  2. daxian-dbw commented on Mar 24, 2017

    @daxian-dbw
    Member

    Weird, I cannot reproduce this:

    PS /home/dongbo/temp> touch '1\2'                                                                   
    PS /home/dongbo/temp> dir                                                                           
    
        Directory: /home/dongbo/temp/1
    
    Mode                LastWriteTime         Length Name                                              
    ----                -------------         ------ ----                                              
    ------          3/24/17   2:11 PM              0 1\2                                               
    
    PS /home/dongbo/temp> mv 1\2 2
    PS /home/dongbo/temp> dir
    
        Directory: /home/dongbo/temp
    
    Mode                LastWriteTime         Length Name                                              
    ----                -------------         ------ ----                                              
    ------          3/24/17   2:11 PM              0 2      
    
    PS /home/dongbo/temp> touch '1\2'                                                                   
    PS /home/dongbo/temp> dir                                                                           
    
        Directory: /home/dongbo/temp/1
    
    Mode                LastWriteTime         Length Name                                              
    ----                -------------         ------ ----                                              
    ------          3/24/17   2:13 PM              0 1\2                                               
    
    PS /home/dongbo/temp> 
    PS /home/dongbo/temp> rm 1\2                                                                        
    PS /home/dongbo/temp> dir                                                                           
    PS /home/dongbo/temp>
    

    Environment

    Name                           Value                                                               
    ----                           -----                                                               
    PSVersion                      6.0.0-alpha                                                         
    PSEdition                      Core                                                                
    PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}                                             
    BuildVersion                   3.0.0.0                                                             
    GitCommitId                    v6.0.0-alpha.16                                                     
    CLRVersion                                                                                         
    WSManStackVersion              3.0                                                                 
    PSRemotingProtocolVersion      2.3                                                                 
    SerializationVersion           1.1.0.1
    
    RID:         ubuntu.16.04-x64
    
  3. vors commented on Mar 24, 2017

    @vors
    CollaboratorAuthor

    Oh yeah, I mean our versions of rm and mv (aliases)

  4. changed the title [-]mv, rm don't work with \-paths on unix[/-] [+]Move-Item, Remove-Item don't work with \-paths on unix[/+] on Mar 24, 2017
  5. joeyaiello commented on Mar 24, 2017

    @joeyaiello
    Contributor

    Update the title to reflect the issue more accurately.

  6. changed the title [-]Move-Item, Remove-Item don't work with \-paths on unix[/-] [+]Move-Item, Remove-Item don't work with paths containing \, [, ] on unix[/+] on Apr 26, 2017
  7. vors commented on Apr 26, 2017

    @vors
    CollaboratorAuthor

    The other thing with path like [foo] is that you cannot use remove-item -recurse -force . to get rid of them.
    I.e.

    PS /Users/svorobev/dev/PowerShell> mkdir junk                                                                                                                                  
    PS /Users/svorobev/dev/PowerShell> cd junk                                                                                                                                     
    PS /Users/svorobev/dev/PowerShell/junk> touch '[foo]'                                                                                                                          
    PS /Users/svorobev/dev/PowerShell/junk> ls                                                                                                                                     
    [foo]
    PS /Users/svorobev/dev/PowerShell/junk> rm -Recurse -Force *                                                                                                                   rm : An object at the specified path /Users/svorobev/dev/PowerShell/junk/[foo] does not exist.                                                                                 
    At line:1 char:1
    + rm -Recurse -Force *
    + ~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [Remove-Item], IOException
        + FullyQualifiedErrorId : System.IO.IOException,Microsoft.PowerShell.Commands.RemoveItemCommand
     
    PS /Users/svorobev/dev/PowerShell/junk> 
    
  8. jeffbi commented on Apr 26, 2017

    @jeffbi

    Regarding the operations on [foo]: [foo] is a shell globbing pattern, so you should probably use -LiteralPath when trying to give that filename to Remove-Item.

    It probably still won't work right now, but I have a fix for #2799 coming that seems to also fix that, as well as the Remove-Item -Recurse -Force * issue. I haven't looked at this for the \ character, so I can't yet say whether my changes will fix that part.

  9. vors commented on Apr 26, 2017

    @vors
    CollaboratorAuthor

    jeffbi oh, awesome to see that you working on it!

    rm -LiteralPath '[foo]'
    

    give the same result. I tried few things like explicit escaping with backticks and nothing seems to work.

  10. 9 remaining items

  11. modified the milestones: , on May 15, 2017
  12. modified the milestones: , on Aug 1, 2017
  13. anmenaga commented on Aug 28, 2017

    @anmenaga

    Verified on latest release (v6.0.0-beta.6) on Ubuntu14 that Move-Item, Remove-Item work as expected with [, ], * when -LiteralPath parameter is used.

  14. anmenaga commented on Aug 28, 2017

    @anmenaga

    The remaining thing for this issue are backslashes (\) that are covered in greater detail in a linked issue #3666 (On Unix, PowerShell will not find files with backslashes in their names), so closing this one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions