LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   How to use foreach on filenames with spaces? (https://www.linuxquestions.org/questions/linux-general-1/how-to-use-foreach-on-filenames-with-spaces-351360/)

BrianK 08-08-2005 10:27 PM

How to use foreach on filenames with spaces?
 
say I have some file names with spaces, like so:

$ ls
New Text Document (2).txt New Text Document.txt


(there are two files there)

and I want to loop over them and do something to them in a foreach loop, like so:


$ foreach f ( `find . -name \* -print0` )
foreach? echo $f
foreach? end
./New
Text
Document.txt
./New
Text
Document
(2).txt


obviously, that's not the output I want, I need to get the actual file name as opposed to their bits so I can do something to the file. Is there a way to do this? I'm using tcsh

Thanks

Matir 08-08-2005 11:02 PM

Have you considered:
Code:

find . -print0 | xargs -0 THECOMMANDTOEXEC
? It might not work for your specific case, but it works a lot of the time.

javeree 08-09-2005 04:24 AM

just before issuing the command set IFS='\t\n'
after the command re-set it to ' \t\n' (a space as first character)
this results in bash not recognizing spaces as word separators. It remains a problem if the command you execute relies on that.

Matir 08-09-2005 12:15 PM

You can't set IFS to ' \t\n'. For some silly reason, it doesn't expand escaped characters in it, you must set them as literals. So do "IFS='<space><tab><newline>'"


All times are GMT -5. The time now is 01:51 AM.