Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

my needs is to copy all files and folders of a "master" folder into all the usb keys mounted.

So, i wrote this code:

for usb_key in `ls /media/`;
    do
        for file in `ls -a /home/daniele/USB/MASTER`;
            do
                cp /home/daniele/USB/MASTER/"$file" /media/$usb_key/"$file"
            done;
    done;

for usb_key in `ls /media`;
    do
        umount /media/$usb_key
    done;
#alert me that the work is done
totem /home/daniele/USB/0016.mp3 &
exit

But, if i have a folder named "DO NOT DELETE" into /home/daniele/USB/MASTER, this throw an error becose of the filenames.

So, how can i do solve that? There is a way to replace all the whitespaces in the $file var with \?

share|improve this question
add comment

1 Answer 1

up vote 2 down vote accepted

What about this? I don't know why you have to iterate each file and why you can't just copy the lot... But this should work slightly better than by parsing ls's output.

for usb_key in /media/*;
do
    cp -Ra /home/daniele/USB/MASTER/* $usb_key
    umount /media/$usb_key
done;

#alert me that the work is done
totem /home/daniele/USB/0016.mp3 &
exit

Just to explain what's happening, for breaks on all spaces when parsing external input (as it was with your code). You can see that in operation by going to a directory with spaced files in and running:

for file in `ls -1`; do echo $file; done

In my TV directory, I see output like:

The
Big
Bang
Theory
The
Cleveland
Show
The

I think there is a way to change the break sequences but it's just easier to use bash's native file-finding syntax. Or use find ... -exec ... which is very powerful itself.

share|improve this answer
add comment

Your Answer

 
discard

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.