5

I'm trying to rename some files on my Synology Diskstation via SSH. The available shell is the BusyBox built-in shell:

BusyBox v1.16.1 (2013-04-16 20:13:10 CST) built-in shell (ash)

The move command always yields two errors when I try using a space character either in the source or destination filename. Escaping space characters or quoting the filename seems to have no effect.

Example for renaming a file with a space character in the destination:

/volumeUSB1/usbshare/directory $ touch test
/volumeUSB1/usbshare/directory $ ls
test
/volumeUSB1/usbshare/directory $ mv test 'te st'
mv: can't rename 'test': No such file or directory
mv: can't rename 'te': No such file or directory
/volumeUSB1/usbshare/directory $ mv test te\ st
mv: can't rename 'test': No such file or directory
mv: can't rename 'te': No such file or directory

Renaming a file with a space character in the source yields similar results:

/volumeUSB1/usbshare/directory $ touch 'te st'
/volumeUSB1/usbshare/directory $ ls
te st
/volumeUSB1/usbshare/directory $ mv 'te st' test
mv: can't rename 'te': No such file or directory
mv: can't rename 'st': No such file or directory
/volumeUSB1/usbshare/directory $ mv te\ st test
mv: can't rename 'te': No such file or directory
mv: can't rename 'st': No such file or directory

type mv returns mv is /bin/mv. The file command is not available on my machine. cat /bin/mv revealed that it's a small script that ends with calling /bin/busybox mv $@.

Where is my mistake?

  • 4
    Since the touch command works, it looks more like it's a problem with mv than with the shell. I suspect that mv is a (poorly written) shell script. What does type mv tell you. What about file /path/to/mv assuming file is available? – Stéphane Chazelas Jun 17 '14 at 10:57
  • type mv returns mv is /bin/mv. The file command is not available on my machine.. cat /bin/mv revealed that it's a small script that ends with calling /bin/busybox mv $@. So I guess the BusyBox implementation of mv is faulty? – Karl Jun 17 '14 at 11:53
  • I personnaly test on busybox 1.21.1 included in Ubuntu 14.04 and mv a file using \ does work so maybe you're busybox version is not working correctly. to identify the faulty function does cp temp tm\ mp gives you an error also ? – Kiwy Jun 17 '14 at 12:45
  • 3
    @Karl I do not know what the mv script does extra, but the busybox call should most certainly be /bin/busybox mv "$@". It doesn't work with spaces because the $@ is not quoted. Did you test /bin/busybox mv test 'te st' directly? – Dubu Jun 17 '14 at 12:52
  • Of course! I've added the quotation marks to the BusyBox call in the /bin/mv script. It works like a charm, thank you! (I'm still quite new to the StackExchange network. @Dubu Would you like to post the solution as an answer so I can flag it as the correct solution?) – Karl Jun 17 '14 at 16:30
3

As you added, the mv command is a script in /bin/mv with this last line:

/bin/busybox mv $@

This line is missing quotation marks around $@:

/bin/busybox mv "$@"

$@ denotes the list of parameters given to the script. Quoting this variable has the special meaning that, when expanded, every parameter will be quoted separately. This is valid for at least bash, dash and also busybox.

This way, the mv command should also work when an argument contains quoted whitespace.

  • 1
    Also should be aware that /bin/rm has the same problem (which is how I got to the 'mv' issue myself. The last line is: /bin/busybox rm $@, which should be corrected to: /bin/busybox rm "$@" – user87617 Oct 12 '14 at 23:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.