nushell icon indicating copy to clipboard operation
nushell copied to clipboard

mv/rm fails with paths containing certain special characters

Open naefl opened this issue 4 years ago • 5 comments

Describe the Bug

When trying to rename files that contain special characters, mv chokes on files that contain certain characters. I haven't finished triaging, but it's likely (at least) ;

To Reproduce

Searching for pdf's with special chars

ls  **/*.pdf | match name "[;:#]" | get name | into string | each { mv $'($it)' $'($it | str find-replace "[;:#]" "" )' }

Results in

error: Invalid file or pattern
   ┌─ shell:55:28
   │
55 │ ls  **/*.pdf | match name "[;:#]" | get name | into string | each { mv $'($it)' $'($it | str find-replace "[;:#]" "" )' }
   │                            ^^^^ invalid file or pattern

However, the following tells me that these are valid paths

ls  **/*.pdf | match name "[;:#]" | get name  | each {  $'($it | into path | path exists)' }
  0   true  
  1   true  
  2   true  
  3   true  

Echoing, as well, works

 ls  **/*.pdf | match name "[;:#]" | get name | into string | each { echo $'($it)' $'($it | str find-replace "[;:#]" "" )' }

A possible workaround is to use the external ^mv command

 ls  **/*.pdf | match name "[;:#]" | get name | into string | each { ^mv $'($it)' $'($it | str find-replace "[;:#]" "" )' }

which works

Expected behavior

move the files

Screenshots

No response

Configuration

No response

Additional Context

No response

naefl avatar Aug 03 '21 21:08 naefl

Here's another one

touch '[test'
'[test' | path exists
true
 rm '[test'
error: Pattern syntax error near position 25: invalid range pattern
    ┌─ shell:132:1
    │
132 │ rm '[test'
    │ ^^ Pattern syntax error near position 25: invalid range pattern
mv '[test' 'test1'
error: Invalid file or pattern
    ┌─ shell:133:4
    │
133 │ mv '[test' 'test1'
    │    ^^^^^^^ invalid file or pattern

naefl avatar Aug 04 '21 21:08 naefl

and here, filepaths with ~$ will somehow bork the $it

touch '~$test [Autosaved]'

 echo  '~$test [Autosaved]' | each { ^rm  $it }
rm: ~ [Autosaved]: No such file or directory
error: External command failed
   ┌─ shell:22:38
   │
22 │ echo  '~$test [Autosaved]' | each { ^rm  $it }
   │                                      ^^ command failed

echo  '~$test [Autosaved]' | each { rm  $it }
error: No valid paths
   ┌─ shell:23:37
   │
23 │ echo  '~$test [Autosaved]' | each { rm  $it }
   │                                     ^^ no valid paths
^rm  '~$test [Autosaved]' # works

naefl avatar Aug 04 '21 21:08 naefl

This issue is being marked stale because it has been open for 90 days without activity. If you feel that this is in error, please comment below and we will keep it marked as active.

github-actions[bot] avatar Nov 03 '21 01:11 github-actions[bot]

This issue is quite similar to https://github.com/nushell/nushell/issues/4041, I think the latest version will work, Can you check it again? Thx

hustcer avatar Jun 06 '22 11:06 hustcer

The problem still exists.

~/.dotfiles
❯ [config languages] | each {|it| mv $"~/.config/helix/($it).toml" .config/helix}
Error: nu::shell::eval_block_with_input (link)

  × Eval block failed with pipeline input
   ╭─[entry #70:1:1]
 1 │ [config languages] | each {|it| mv $"~/.config/helix/($it).toml" .config/helix}
   ·  ───┬──
   ·     ╰── source value
   ╰────

Error:
  × Invalid file or pattern
   ╭─[entry #70:1:1]
 1 │ [config languages] | each {|it| mv $"~/.config/helix/($it).toml" .config/helix}
   ·                                    ──────────────┬──────────────
   ·                                                  ╰── invalid file or pattern
   ╰────

mo8it avatar Nov 06 '22 14:11 mo8it

Running into this trying to pipe find into mv as well. If I try to use the external mv command ... | each { |it| ^mv $it ./folder } I get something like mv: cannot stat ''$'\033''[37m'$'\033''[0m'$'\033''[41;37m./bin/'$'\033''[0m'$'\033''[37mSystem.Linq.Queryable.dll'$'\033''[0m': No such file or directory -- which looks like the coloring from find is messing up the file names

edit: nvm, was user error. Looks like that stuff gets stripped by nu's mv

gsuuon avatar Nov 12 '22 14:11 gsuuon

I wanted to check this issue, and I think it stems from mv and rm using glob. In glob, characters like { and [ have to be escaped. This means that a valid file name supplied by ls won't always work.

uutils' utilities do not have this issue, as they do not support Nushell glob. But this does raise the question of where glob patters are appropriate.

kaathewisegit avatar Oct 30 '23 14:10 kaathewisegit