shelljs / shx

Portable Shell Commands for Node
MIT License
1.72k stars 44 forks source link

wildcards not working on mv #149

Closed AaronNGray closed 6 years ago

AaronNGray commented 6 years ago

The following is not working :-

shx mv test/*.js test/*.mjs

getting the following error :-

mv: dest is not a directory (too many sources)
nfischer commented 6 years ago

This is expected behavior, so I'll close this (but I'm happy to answer further questions).


Unix and Windows handle glob (wildcard) expansion differently. I think this blog post has a pretty good explanation. TL;DR Unix shells expand globs before launching the command, Windows shells don't (the program must expand globs itself).

If you're running your command on unix, you're seeing unix expansion at play (and shx can't turn that off). If you're running on Windows, shx intentionally emulates unix expansion for consistent behavior across the platforms.


In your specific case, I recommend writing a for-loop:

for file in test/*.js; do
  shx mv "$file" "test/${file%.js}.mjs"
done

Or, use shelljs to make it cross-platform:

shell.ls('test/*.js').forEach(file => {
  shell.mv(file, file.replace(/\.js$/, '.mjs'));
});