Skip to main content

find+xargs and paths with white spaces

Posted in

Today I had to modify permissions on a set of files, so I start with find and xargs:

$ find . -type f | xargs chmod o-r

but I got tons of errors similar to these:

chmod: cannot access `-': No such file or directory
chmod: cannot access `So': No such file or directory

I don't see such errors so often with my files because I avoid white spaces in paths. As a result I didn't know such parameters as find's -print0 and xargs's -0

-print0
True; print the full file name on the standard output, followed by a null character (instead of the newline character that -print uses). This allows
file names that contain newlines or other types of white space to be correctly interpreted by programs that process the find output. This option corre-
sponds to the -0 option of xargs.

and

--null
-0 Input items are terminated by a null character instead of by whitespace, and the quotes and backslash are not special (every character is taken literal-
ly). Disables the end of file string, which is treated like any other argument. Useful when input items might contain white space, quote marks, or
backslashes. The GNU find -print0 option produces input suitable for this mode.

So, I did

$ find . -type f -print0 | xargs -0 chmod o-r

and it worked.

Post new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
         o      'O           .oOOOo.             .oOOOo.  
.oOOo. O o .oOOo. .O o. .oOOo. .O o
O o o O O o O O o o
o O o o o O o o O o
`OooOo O O' O' o O `oOOo' o
O `o o O O Oo o O o O
o `o O .O `o O' o O `o .o
`OooO' `o' oOoOoO `OoooO Oo `OooO' `OoooO'

Enter the code depicted in ASCII art style.