using find, xargs, grep when the directory or file names contain space
March 10, 2012 Leave a comment
The following command doens’t return any results :
command:
find . -name ‘struts*prop*’|xargs -t grep -si objectFactory
result:
grep -si objectFactory ./jive war/jive-5.0.2.1.war/WEB-INF/classes/struts.properties ./jive war/jive-sbs-employee-4.0.12.war/WEB-INF/classes/struts.properties
As you can see the directory name ‘jive war’ has a space. To overcome this limitation, use the following command:
command:
find . -name ‘struts*prop*’ -print0|xargs -t0 grep -si objectFactory
result:
grep -si objectFactory ./jive war/jive-5.0.2.1.war/WEB-INF/classes/struts.properties ./jive war/jive-sbs-employee-4.0.12.war/WEB-INF/classes/struts.properties
./jive war/jive-5.0.2.1.war/WEB-INF/classes/struts.properties:### Alternatively, you can provide a com.opensymphony.xwork2.ObjectFactory subclass name here
Note the number zero in -print0 and -t0. That is a way to map end of line to a character in find and then tell xargs to ignore white space and tread character as end of string.
You can also use find with ‘-exec‘ option.