数字大于给定数字的Grep行

时间:2013-04-30 22:24:16

标签: bash shell awk

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

3 个答案:

答案 0 :(得分:26)

试试这个:

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

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

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

修改

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

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

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

答案 1 :(得分:6)

我使用awk(1)

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

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

答案 2 :(得分:3)

grepsed都不擅长算术。 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部分。