Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

find output when directory names have spaces in them 2

Status
Not open for further replies.

philipose

Programmer
Dec 24, 2003
137
US
Hi Unix Gurus,
I have an issue with the output of the find command. I am doing a find on a directory /mntpt/dir2, where some user directories have spaces in the names. So while I am expecting the values of $myfile to be
"/mntpt/dir2/myfolder1/myfile1",
"/mntpt/dir2/my folder1/myfile2",
"/mntpt/dir2/my folder1/myfile3",
I am getting
/mntpt/dir2/myfolder1/myfile1,
/mntpt/dir2/my,
folder1/myfile2,
/mntpt/dir2/my,
folder1/myfile3,

Any suggestions ??
I am giving the loop that I am trying to manipulate below. Maybe an example of executing a user defined function in ksh and calling it from Ksh will be sufficient.
Thanks in advance


for myfile in `find "/mntpt/dir2" -type f -name '*'` ; do

mynewfile=`echo $myfile | sed -e "s/^\/mntpt\/newmntpt//" | sed -e "s/$/\.ext/"`

# Verify existance of the file
if [[ ! -f "$mynewfile" ]]; then
/usr/bin/echo "$mynewfile does not exist for $mynewfile"
fi

done
 
not checked but should work.


#!/bin/ksh

find "/mntpt/dir2" -type f -name '*' |&

while read -p
do

mynewfile=`echo $REPLY | sed -e "s/^\/mntpt\/newmntpt//" | sed -e "s/$/\.ext/"`

# Verify existance of the file
if [[ ! -f "$mynewfile" ]]; then
/usr/bin/echo "$mynewfile does not exist for $mynewfile"
fi

done



 
I've never understood the need for '-name "*"', if you want all the files then don't use the file parameter
Try this
Code:
#!/bin/ksh
IFS='
' #set IFS to newline

for file in $(find /mntpt/dir2 -type f | sed -e "s/^\/mntpt\/newmntpt//" | sed -e "s/$/\.ext/")
do
  [[ -f $file ]] || echo $file does not exist for $file
done
but, judging by the line 'echo $file does not exist for $file' I would guess that what you want is
Code:
#!/bin/ksh
IFS='
' #set IFS to newline

for oldfile in $(find /mntpt/dir2 -type f)
do
  newfile=$(echo $oldfile | sed -e "s/^\/mntpt\/newmntpt//" | sed -e "s/$/\.ext/")
  [[ -f $newfile ]] || echo $newfile does not exist for $oldfile
done
P.S. This is not tested but setting the IFS to newline is a standard way of gettig round the spaces issue.

Ceci n'est pas une signature
Columb Healy
 
Thanks for your wonderful suggestions. The reason I used the name flag in the find was I was looking for only certain extension, which I forgot to mention here.

So in that case ogniemi's first suggestion would not work. But ogniemi's second suggestion and columb's suggestion on IFS works like a charm. I liked the IFS suggestion as it involved minimal modification.

Thanks a lot guys. Stars to both of you.
philipose
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top