Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How to handle files with their names containing spaces in a shell script.

Here is what I am trying

    find /abc/xyz -name 'BY567*.csv' | while read fname
        mv "$fname" ./archive/$(basename $fname)-$(date +%Y%m%d-%T)

But when I do this it strips of the file name after space. Like if file name is BY567_Test file.csv , it will be changed to BY567_Test-datetimestamp and not BY567_Test file.csv-datetimestamp.

share|improve this question
up vote 5 down vote accepted

Put double-quotes arround the end of line and in basename :

mv "$fname" "./archive/$(basename "$fname")-$(date +%Y%m%d-%T)"

The variables will be interpreted and the spaces should be OK.

share|improve this answer
cool, it worked. Can I use the double quotes if I am using sftp to transfer the file to other server? Like in above loop if I am trying to connect to the other server and then using put $fname as command to transfer the file. – yogsma Oct 25 '10 at 16:02
I think so but, I never try. Try and tell us ! – Dom Oct 25 '10 at 16:08
It worked. Thanks – yogsma Oct 25 '10 at 20:15

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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