2013-04-30 57 views
18

我试图在大于给定数字的输出的第一个字段中grep行。在这种情况下,该号码是755。最终,我在做的是试图通过使用stat -c '%a %n' *列出每个文件的权限大于(并且不等于)755,然后管道到grep'ing(或可能sed'ing?)以获得最终列表。任何想法如何最好地完成?对于大于给定数字的数字的grep行

+2

由于'755'实际上是一个代表比特序列的八进制数,“大于”意味着什么? '666'给大家写入权限;是“大于”'755'? “755”大于“666”吗? – 2013-05-01 00:04:31

+0

OS X用户:相当于'stat -c'%a%n'*'(Linux)似乎是'stat -f'%Lp%N'*' – mklement0 2013-05-01 03:11:04

+0

该人要求grep,而人们仍然给予AWK? – Soncire 2014-01-23 04:27:51

回答

20

试试这个:

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)}' 

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

+0

噢,感谢那个小插件。我只是想削减场地2,但是你的场地只需一个管道就可以完成! – user2150250 2013-04-30 22:35:36

+0

注意带空格的文件名。 – 2013-04-30 22:37:21

+1

@ user2150250注意,我的第二个awk单行假定你的文件名没有空格。 – Kent 2013-04-30 22:38:05

5

我会使用awk(1):第一个字段是大于755,如果你想打印线或一些不同的子集,你可以添加一个动作

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

awk模式匹配线, (请参阅@肯特的答案)。

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的一部分,如果你只需要文件名。

相关问题