更改文件的chmod而不是目录

时间:2022-09-01 21:17:28

I need to use chmod to change all files recursivly to 664. I would like to skip the folders. I was thinking of doing something like this

我需要使用chmod将所有文件递归更改为664.我想跳过这些文件夹。我在考虑做这样的事情

ls -lR | grep ^-r | chmod 664

This doesn't work, I'm assuming because I can't pipe into chmod Anyone know of an easy way to do this?

这不起作用,我假设因为我无法管道进入chmod任何人都知道一个简单的方法来做到这一点?

Thanks

5 个解决方案

#1


A find -exec answer is a good one but it suffers from the usually irrelevant shortcoming that it creates a separate sub-process for every single file. However it's perfectly functional and will only perform badly when the number of files gets really large. Using xargs will batch up the file names into large groups before running a sub-process for that group of files.

查找-exec答案是一个很好的答案,但它通常无关紧要的缺点是它为每个文件创建一个单独的子进程。然而,它功能完善,只有当文件数量变得非常大时才会表现不佳。在为该组文件运行子进程之前,使用xargs会将文件名批处理为大组。

You just have to be careful that, in using xargs, you properly handle filenames with embedded spaces, newlines or other special characters in them.

您必须要小心,在使用xargs时,您可以正确处理带有嵌入空格,换行符或其他特殊字符的文件名。

A solution that solves both these problems is (assuming you have a decent enough find and xargs implementation):

解决这两个问题的解决方案是(假设您有足够的查找和xargs实现):

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

The -print0 causes find to terminate the file names on its output stream with a NUL character (rather than a space) and the -0 to xargs lets it know that it should expect that as the input format.

-print0导致find使用NUL字符(而不是空格)终止其输出流上的文件名,并且-0到xargs让它知道它应该期望它作为输入格式。

#2


Another way to do this is to use find ... -exec ... as follows:

另一种方法是使用find ... -exec ...如下:

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

The problem is that the -exec starts a chmod process for every file. The xargs approach avoids this, and is superior provided that you have a version of find and xargs that can cope with the "spaces in pathnames" problem; see the accepted answer.

问题是-exec为每个文件启动chmod进程。 xargs方法可以避免这种情况,并且只要你有一个find和xargs版本可以应对“路径名中的空格”问题,它就是优越的。看到接受的答案。

And for the record, using back-ticks is going to break if there are too many files to be chmoded, or the aggregated length of the pathnames is too large.

而对于记录,如果有太多要编码的文件,或者路径名的聚合长度太大,则使用反向标记将会中断。

#3


My succinct two cents...

我的简洁两分钱......

Linux:

$ chmod 644 `find -type f`

OSX:

$ chmod 644 `find . -type f`

This works to recursively change all files contained in the current directory and all of its sub-directories. If you want to target a different directory, substitute . with the correct path:

这可以递归地更改当前目录及其所有子目录中包含的所有文件。如果要定位其他目录,请替换。使用正确的路径:

$ chmod 644 `find /home/my/special/folder -type f`

#4


via http://www.linuxquestions.org/questions/aix-43/chmod-recursion-files-only-208798/?s=a70210fb5e5d0aa7d3c69d8e8e64e3ed

"find . -type f -print | xargs chmod 444 "shoud work, isn't it ? If not, find . -print >myfile.sh and vi myfile.sh removing the directories (they should not be soo many), and then 1,$s/^/chmod 444/ and sh myfile.sh.

“找。-type f -print | xargs chmod 444”干什么工作,不是吗?如果没有,找到。 -print> myfile.sh和vi myfile.sh删除目录(它们不应该太多),然后是1,$ s / ^ / chmod 444 /和sh myfile.sh。

#5


with GNU find

用GNU查找

find /path -type f -exec chmod 644 {} +;

find / path -type f -exec chmod 644 {} +;

#1


A find -exec answer is a good one but it suffers from the usually irrelevant shortcoming that it creates a separate sub-process for every single file. However it's perfectly functional and will only perform badly when the number of files gets really large. Using xargs will batch up the file names into large groups before running a sub-process for that group of files.

查找-exec答案是一个很好的答案,但它通常无关紧要的缺点是它为每个文件创建一个单独的子进程。然而,它功能完善,只有当文件数量变得非常大时才会表现不佳。在为该组文件运行子进程之前,使用xargs会将文件名批处理为大组。

You just have to be careful that, in using xargs, you properly handle filenames with embedded spaces, newlines or other special characters in them.

您必须要小心,在使用xargs时,您可以正确处理带有嵌入空格,换行符或其他特殊字符的文件名。

A solution that solves both these problems is (assuming you have a decent enough find and xargs implementation):

解决这两个问题的解决方案是(假设您有足够的查找和xargs实现):

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

The -print0 causes find to terminate the file names on its output stream with a NUL character (rather than a space) and the -0 to xargs lets it know that it should expect that as the input format.

-print0导致find使用NUL字符(而不是空格)终止其输出流上的文件名,并且-0到xargs让它知道它应该期望它作为输入格式。

#2


Another way to do this is to use find ... -exec ... as follows:

另一种方法是使用find ... -exec ...如下:

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

The problem is that the -exec starts a chmod process for every file. The xargs approach avoids this, and is superior provided that you have a version of find and xargs that can cope with the "spaces in pathnames" problem; see the accepted answer.

问题是-exec为每个文件启动chmod进程。 xargs方法可以避免这种情况,并且只要你有一个find和xargs版本可以应对“路径名中的空格”问题,它就是优越的。看到接受的答案。

And for the record, using back-ticks is going to break if there are too many files to be chmoded, or the aggregated length of the pathnames is too large.

而对于记录,如果有太多要编码的文件,或者路径名的聚合长度太大,则使用反向标记将会中断。

#3


My succinct two cents...

我的简洁两分钱......

Linux:

$ chmod 644 `find -type f`

OSX:

$ chmod 644 `find . -type f`

This works to recursively change all files contained in the current directory and all of its sub-directories. If you want to target a different directory, substitute . with the correct path:

这可以递归地更改当前目录及其所有子目录中包含的所有文件。如果要定位其他目录,请替换。使用正确的路径:

$ chmod 644 `find /home/my/special/folder -type f`

#4


via http://www.linuxquestions.org/questions/aix-43/chmod-recursion-files-only-208798/?s=a70210fb5e5d0aa7d3c69d8e8e64e3ed

"find . -type f -print | xargs chmod 444 "shoud work, isn't it ? If not, find . -print >myfile.sh and vi myfile.sh removing the directories (they should not be soo many), and then 1,$s/^/chmod 444/ and sh myfile.sh.

“找。-type f -print | xargs chmod 444”干什么工作,不是吗?如果没有,找到。 -print> myfile.sh和vi myfile.sh删除目录(它们不应该太多),然后是1,$ s / ^ / chmod 444 /和sh myfile.sh。

#5


with GNU find

用GNU查找

find /path -type f -exec chmod 644 {} +;

find / path -type f -exec chmod 644 {} +;