2013-04-30 3 views
18

주어진 숫자보다 큰 출력의 첫 번째 필드에있는 라인을 grep하려고합니다. 이 경우 숫자는 755입니다. 궁극적으로, 내가하고있는 일은 stat -c '%a %n' *을 사용하여 755보다 큰 (그리고 같지 않은) 권한을 가진 모든 파일을 나열하려고 시도한 다음이 최종 목록을 얻기 위해 grep'ing (또는 sed'ing?)으로 파이프합니다. 이것이 어떻게 달성 될 수 있는지에 대한 아이디어가 있습니까? 당신이 수, 당신은 당신의 최종 출력에 파일 이름을 원하는 경우주어진 숫자보다 큰 숫자의 그렙 라인

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

, 특권 번호를 건너 :

+2

'755'는 실제로 비트 열을 나타내는 8 진수이므로 "보다 큼"이란 정확히 무엇을 의미합니까? 666은 모든 사람에게 쓰기 권한을 부여합니다. 그것이 "755"보다 더 큽니까? '755'는''666'보다 더 큽니까? –

+0

OS X 사용자 :'stat -c '% a % n'*'(리눅스)는'stat -f '% Lp % N'*'' – mklement0

+0

인 것처럼 보입니다. awk? – Soncire

답변

20

이 시도

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

+0

공백이있는 파일 이름을 조심하십시오. –

+1

@ user2150250 note, 제 2 awk one-liner는 파일 이름에 공백이 없다고 가정합니다. – Kent

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 부분을 제거합니다.