Web Hosting Talk







View Full Version : Filename having spaces in it in bash scripting


scs5mn
09-12-2007, 11:02 AM
Hi!

i am working to find and replace the text in the files that contains space in their file names.

my code is:


for F in `find /home/script/ -type f -name "*.xml" -print`;
do
mv "$F" "$F.tmp";
sed 's/lfs/ /g' "$F.tmp">$F;
rm "$F.tmp";
done

it works fine for the files without having space in their file name but doesn't work for files having space in it.

any suggestions?

Thanks

sasha
09-12-2007, 11:50 AM
This seems bit easier

find /home/script/ -type f -name "*.xml" -exec replace "lfs" " " -- {} \;


If you do not have "replace" installed (script that gets installed as part of mysql package) you can make some kind of function that will do what you need


#!/bin/bash

function replacelfs(){
local myfile=$@
cat $myfile | sed 's/lfs/ /g' > $myfile
}

find /home/script/ -type f -name "*.xml" -exec replacelfs {} \;



Do test any of this on temporary data before you try using it. I just typed it here and it might misbehave.

scs5mn
09-12-2007, 11:58 AM
Hi!

Thanks sasha, but it is not working. it gives me the following error

find: missing argument to '-exec'

i created the method replacelfs() but still it is not working

and i am using ingres database not mysql

Can anybody suggest something diferent rather than creating new package or function?

Thanks

Bangalore Job Mob
09-12-2007, 12:52 PM
Don't you need quotes around that last $F?

axelilly
09-12-2007, 01:27 PM
Hi!

Thanks sasha, but it is not working. it gives me the following error

find: missing argument to '-exec'


Sounds like you did not type in the complete find command line.
Please make sure that you typed ALL of it in.

Try it again.

scs5mn
09-13-2007, 04:45 AM
Hi!

i tried again and again but still it is having same problem

gives me an error message

ta

axelilly
09-13-2007, 09:32 AM
Please paste in the exact command that you are using.

scs5mn
09-13-2007, 10:20 AM
HI!

Thanks to all who replied to my query

i solved my problem by replacing the for loop with while loop in original code and now it is working perfectly

thanks