9

I have the following command

find . -type f -print0 | xargs -0 chmod 644 

which would successfully change to 644 the permissions on all files in ., provided that the filenames contained no embedded spaces. However it doesn't work in general.

For example

touch "hullo world"
chmod 777 "hullo*"
find . -type f -print0 | xargs -0 chmod 644 

returns

/bin/chmod: cannot access `./hello': No such file or directory
/bin/chmod: cannot access `world': No such file or directory

Is there a way to modify the command so that it can deal with files with embedded spaces?

Thanks very much for any advice.

Leo Simon
  • 1,549

1 Answers1

12

Without xargs

find . -type f -exec chmod 644 {} \;

or with xargs

find . -type f -print0 | xargs -0 -I {} chmod 644 {}

the used xargs switches

  • -0 If there are blank spaces or characters (including newlines) many commands will not work. This option take cares of file names with blank space.

  • -I Replace occurrences of replace-str in the initial-arguments with names read from standard input. Also, unquoted blanks do not terminate input items; instead the separator is the newline character.

Explanation taken from here

A.B.
  • 92,125