views:

88

answers:

2

I have a list of files that I'm trying to copy and move (using cp and mv) in a bash shell script. The problem that I'm running into, is that I can't get either command to recognize a huge number of files, seemingly because the filenames contain spaces and/or unicode characters. I couldn't find any switches to decode/re-encode these characters. Instead, for example, if I copy "file name.xml", I get "*.xml" and a script error that the file wasn't found for my result. Does anyone know settings or commands that will deal with these files?

EDIT(adding current code): When I run:

MacBookPro:Desktop$ ./script.sh

#!/bin/sh
dateVar=`date +%Y-%m-%d`
mkdir /Volumes/Documents/SMSarchive/$dateVar
cd /Volumes/Documents/SMSarchive/SMSdrop
for i in *.xml
do
cp $i /Volumes/Documents/SMSarchive/$dateVar/$dateVar-$i
done

I get the message:

usage: cp [-R [-H | -L | -P]] [-fi | -n] [-pvX] source_file target_file cp [-R [-H | -L | -P]] [-fi | -n] [-pvX] source_file ... target_directory

...when it hits the "cp" command. There's actually more to the script, that processes the copied files further. With a "regular" file name e.g. 'file.xml', everything works fine. It's only files with spaces, or Unicode characters, where I have problems.

+4  A: 

Problems with spaces indicates that insufficient quoting has been done. The following is incorrect:

someprogram $file

The correct version is as follows:

someprogram "$file"
Ignacio Vazquez-Abrams
Thank you! Changing the code to:cp "$i" /Volumes/Documents/SMSarchive/$dateVar/"$dateVar"-"$i"...worked perfectly!
LOlliffe
or even: `cp "$i" "/Volumes/Documents/SMSarchive/$dateVar/$dateVar-$i"`. In a shell script, *always* quote variables unless you have a specific reason not to.
glenn jackman
A: 

watch out for code errors when $i is null! This can be the result of fatal rm -Rf errors!

The HCD