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 \
?
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.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:
In my TV directory, I see output like:
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.