using find, xargs, grep when the directory or file names contain space

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.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: