命令仅更改文件而不是目录的权限

Leo*_*mon 5 permissions find chmod xargs

我有以下命令

find . -type f -print0 | xargs -0 chmod 644 
Run Code Online (Sandbox Code Playgroud)

如果文件名不包含嵌入的空格,这将成功地将 . 中所有文件的权限更改为 644。但是,它通常不起作用。

例如

touch "hullo world"
chmod 777 "hullo*"
find . -type f -print0 | xargs -0 chmod 644 
Run Code Online (Sandbox Code Playgroud)

返回

/bin/chmod: cannot access `./hello': No such file or directory
/bin/chmod: cannot access `world': No such file or directory
Run Code Online (Sandbox Code Playgroud)

有没有办法修改命令,以便它可以处理带有嵌入空格的文件?

非常感谢您的任何建议。

A.B*_*.B. 8

没有 xargs

find . -type f -exec chmod 644 {} \;
Run Code Online (Sandbox Code Playgroud)

或与 xargs

find . -type f -print0 | xargs -0 -I {} chmod 644 {}
Run Code Online (Sandbox Code Playgroud)

使用的xargs开关

  • -0 如果有空格或字符(包括换行符),许多命令将不起作用。此选项处理带有空格的文件名。

  • -I 用从标准输入读取的名称替换初始参数中出现的 replace-str。此外,未加引号的空格不会终止输入项;相反,分隔符是换行符。

这里采取的解释