28

我正在尝试在输出的第一个字段中查找大于给定数字的行。在这种情况下,该数字是755。最终,我正在做的是尝试列出具有大于(不等于)权限的每个文件,755方法是使用stat -c '%a %n' *然后管道到一些 grep'ing(或者可能是 sed'ing?)以获得这个最终列表。有什么想法可以最好地完成吗?

4

3 回答 3

32

尝试这个:

stat -c '%a %n' *|awk '$1>755'

如果您只想在最终输出中使用文件名,请跳过权限编号,您可以:

stat -c '%a %n' *|awk '$1>755{print $2}'

编辑

实际上你可以chmod在 awk 内做。但是您应该确保执行 awk 行的用户有权更改这些文件。

stat -c '%a %n' *|awk '$1>755{system("chmod 755 "$2)}'

再次假设文件名没有空格。

于 2013-04-30T22:32:02.570 回答
7

我会使用awk(1)

stat -c '%a %n' * | awk '$1 > 755'

awk模式匹配第一个字段大于 755 的行。如果要打印行的子集或其他内容,也可以添加一个操作(参见@Kent 的答案)。

于 2013-04-30T22:32:34.693 回答
4

也不grep擅长sed算术。awk可以帮助(不幸的是我不知道)。但请注意,这find在这里也很方便:

   -perm mode
          File's permission bits are exactly  mode  (octal  or  symbolic).
          Since  an  exact match is required, if you want to use this form
          for symbolic modes, you may have to  specify  a  rather  complex
          mode  string.  For example -perm g=w will only match files which
          have mode 0020 (that is, ones for which group  write  permission
          is  the  only  permission set).  It is more likely that you will
          want to use the `/' or `-' forms, for example -perm -g=w,  which
          matches  any file with group write permission.  See the EXAMPLES
          section for some illustrative examples.

   -perm -mode
          All of the permission bits mode are set for the file.   Symbolic
          modes  are accepted in this form, and this is usually the way in
          which would want to use them.  You must specify `u', `g' or  `o'
          if  you use a symbolic mode.   See the EXAMPLES section for some
          illustrative examples.

   -perm /mode
          Any of the permission bits mode are set for the file.   Symbolic
          modes  are  accepted in this form.  You must specify `u', `g' or
          `o' if you use a symbolic mode.  See the  EXAMPLES  section  for
          some  illustrative  examples.  If no permission bits in mode are
          set, this test matches any file (the idea here is to be  consis‐
          tent with the behaviour of -perm -000).

所以对你有用的是:

find . -perm -755 -printf '%m %p\n'

-printf如果您只需要文件名,只需删除该部分。

于 2013-04-30T22:38:37.503 回答