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