changing chmod for files but not directories

一笑奈何 提交于 2019-11-28 13:58:43

问题


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

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?

Thanks


回答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.

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

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

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.




回答2:


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

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.

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.




回答5:


with GNU find

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



来源:https://stackoverflow.com/questions/1163294/changing-chmod-for-files-but-not-directories

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!