Directory names that contain spaces and other suggestions?

The script below was written to select files and convert a particular string to something other and replace that file. However, I came across some issues with filenames that contain spaces, any suggestions to get around this? Any other suggestions that may apply to this code would also be appreciated.

user=`whoami�|�tr�"[A-Z]"�"[a-z]"`
rtime=`date�+'%H.%M.%S�%d-%m-%y'`
ENVIRON=`pwd`
find�$ENVIRON�-name�"*.pde"�-type�f�-exec�egrep�-ily�"DMRG[�]*3,1"�{}�\;|�while�read�pde
���do
��������directory=`dirname�$dir`
��������if�[�-w�$directory�]�;�then��������
������������cp�$pde�${pde}.pdb
������������cat�$pde�|�sed�'s/\(DMRG[�,]*3,\)1/\10/g'�>�${pde}.tmp
������������if�[�-s�${pde}.tmp�]�;�then
����������������mv�${pde}.tmp�$pde
����������������echo�"PDE�Conversion�Success�Log�Generated�at�$rtime"�\
����������������>�/syd/devel/code/training/steves/pdeSCS.log
����������������echo�"$pde�File�has�been�converted�Backup�file�is�${pde}.pdb"��\
����������������>>�/syd/devel/code/training/steves/pdeSCS.log����
������������else
����������������echo�"PDE�Conversion�Error�Log�Generated�at�$rtime"�>�pdeERR.log
����������������echo�"Failure�writing�${pde}.tmp�Backup�file�is�${pde}.pdb"
����������������echo�"Failure�writing�${pde}.tmp�Backup�file�is�${pde}.pdb"�>>�pdeERR.log
������������fi
��������else
������������echo�"$directory�is�not�writeable"�
��������fi
���done

hi!!,
i am not clear whether the filename has spaces or the directory name has spaces.... U have written diferent things in the main body of post and the Title..

anyway.. i assume that the directory name has space..

try replacing the line..

if [ -w $directory ] ; then

with

if [ -w "$directory" ] ; then

it may help you..
:slight_smile:

sorry for being unclear

its actually the directory name that contains the space.

The error first occurs with the dirname command. I got around that before by quoting specific things, however the find command dies if you are for example under a directory that contains a name with spaces.

ie /dir/spec/test/new\ test\ directory/script.sh

but the find command wont recognise the path, i tried doing something like this unsuccessfully ENVIRON=`pwd | sed s/\ /\\\\\ /g

Any other suggestions?

shaik,
replace the line

find $ENVIRON -name "*.pde" -type f -exec egrep -ily "DMRG

with

find "$ENVIRON" -name "*.pde" -type f -exec egrep -ily "DMRG

I have used "find" bY CREATING A DIRECTORY WITH NAME CONTAINING SPACE. AND IT WORKS FINE ON SOLARIS-5.6.

which OS are u using??

i have tested by creating 3-4 levels of directory names with spaces inside the directories having spaces in names. and find works fine in all those levels just by puttting $ENVIRON in quotes.

Actually, I tried that before, but it didn't work running from the directory with the space in the directory name. But I just tried it again from the command line in a test directory and it worked.

Anyway, I tried it again adapting it to the script and it worked. I think the last time I tried I must have missed something else, being too complex or something else was at play.

Thanks again for the help!

Anyway this is the next version of the script:

#!/usr/bin/sh

user=`whoami�|�tr�"[A-Z]"�"[a-z]"`
rtime=`date�+'%H.%M.%S�%d-%m-%y'`
ENVIRON=`pwd`
find�"$ENVIRON"�-name�"*.pde"�-type�f�-exec�egrep�-ily�"DMRG[�]*3,1"�{}�\;|�while�read�pde
���do
��������directory=`dirname�"$pde"`
echo�$directory
��������if�[�-w�"$directory"�]�;�then��������
������������cp�"$pde"�"${pde}.pdb"
������������cat�"$pde"�|�sed�'s/\(DMRG[�,]*3,\)1/\10/g'�>�${pde}.tmp
������������if�[�-s�"${pde}.tmp"�]�;�then
����������������mv�"${pde}.tmp"�"$pde"
����������������echo�"PDE�Conversion�Success�Log�Generated�at�$rtime"�\
����������������>�/syd/devel/code/training/steves/pdeSCS.log
����������������echo�"$pde�File�has�been�converted�Backup�file�is�${pde}.pdb"��\
����������������>>�/syd/devel/code/training/steves/pdeSCS.log����
������������else
����������������echo�"PDE�Conversion�Error�Log�Generated�at�$rtime"�>�pdeERR.log
����������������echo�"Failure�writing�${pde}.tmp�Backup�file�is�${pde}.pdb"
����������������echo�"Failure�writing�${pde}.tmp�Backup�file�is�${pde}.pdb"�>>�pdeERR.log
������������fi
��������else
������������echo�"$directory�is�not�writeable"�
��������fi
���done�