Your code can be written in a simpler way also, if you can provide us your exact requirement, what I understand from your code is that you have to search for files which have "dw or DW" somewhere in their names and then uncompress those files to a specific directory, is it like that?
For your current problem, you can try something like this:
for i in `find $SRC_PATH -name "$SEARCH_FILENAME"`
The directory "/db01/dat/march 2006" contains many files like *DW*.03B and others. But in directory name "march 2006" there is a space in between. (While user creating the folder in ftp through internet explorer, they have created with spaces also).
There is a test files like
/db01/dat/march 2006/7001DW06.03B
My sample script is as follows....
SRC_PATH = /db01/dat
SEARCH_FILENAME = *DW06.03B
for i in `find $SRC_PATH -name $SEARCH_FILENAME`
do
echo $i
IDX=\`expr index $i .\`
if [ $IDX -ne 0 ]
then
STRTPOS=\`expr $IDX - 8\`
FILENAME=\`expr substr $i $STRTPOS 12\`
FILETYPE=\`expr substr $FILENAME 5 2\`
if [ "$FILETYPE" = "DW" ] || [ "$FILETYPE" = "dw" ]
then
echo Extracting $FILENAME ...
\# unzip -jo $i -d $DEST_PATH/$1
fi
fi
done
echo Done
While running the above script,
echo $i Displays "/db01/dat/march" and "2006/7001DW06.03B" instead of /db01/dat/march 2006/7001DW06.03B
In /db01/dat/march, the index is zero(0), so i ignore. But in 2006/7001DW06.03B the index is > 0 so I try to extract the file. Since it gets the path($i) as 2006/7001DW06.03B instead of /db01/dat/march 2006/7001DW06.03B it fails.
My requirement is, $i should get /db01/dat/march 2006/7001DW06.03B. Then my problem is solved.
for i in `find $SRC_PATH -name "$SEARCH_FILENAME"`
i.e. double quotes around variable name? Try to use double quotes around all variable names.
Still you didn't mention your requirement clearly, you are emphasizing on the space in filename problem but not telling what exactly you are trying to do.
We have a ftp site. User open this ftp site using intertnet explorer and upload compressed file(these files has a pattern). Sometimes they create folders and while creating folders they use white spaces in between like "march 2006".
While extracting I search for the files and extract. My problem is if they created folder having white spaces I can not exttract using the script given above.
Yes, I tried giving the search variable inside double quotes and still not working.
Ok, in that case, what we can try to do is that, simply replace each space with a pattern which is extremely unlikely to occur ("__"), run below script to replace white spaces from file and directory names with ("") before you run your own script.
#! /bin/ksh
SRC_PATH = /db01/dat
# first, we fix the directories
for name in $(find $SRC_PATH -type d -print | sed 's/ /____/g')
do
if [ $name != "." ] ; then
oldname="$(echo $name | sed 's/____/ /g')"
newname="$(echo $name | sed 's/____/_/g')"
if [ "$oldname" != "$newname" ] ; then
mv "$oldname" "$newname"
fi
fi
done
#fixing individual files...
for name in $(find $SRC_PATH -type f -print | sed 's/ /____/g')
do
oldname="$(echo $name | sed 's/____/ /g')"
newname="$(echo $name | sed 's/____/_/g')"
if [ "$oldname" != "$newname" ] ; then
mv "$oldname" "$newname"
fi
done
You can rename back those directory names to original.
I'm sure there would be a better way to handle white spaces in filenames with find, wait until someone else replies, I have seen this problem first time and I could only think of rename directory names and then reverse the rename script after you have done with find command.