특정 결말을 가진 파일이없는 디렉토리 찾기 디렉토리를 표시하고

특정 파일 끝을 가진 파일을 포함하지 않는 모든 디렉토리를 표시하고 싶습니다. 따라서 다음 코드를 사용해 보았습니다.

find . -type d \! -exec test -e '{}/*.ENDING' \; -print

이 예제에서는 ending 파일을 포함하지 않는 모든 디렉토리를 표시하고 .ENDING싶지만 작동하지 않습니다.

내 실수는 어디입니까?



답변

다음은 세 단계로 구성된 솔루션입니다.

temeraire:tmp jenny$ find . -type f -name \*ENDING -exec dirname {} \; |sort -u > /tmp/ending.lst
temeraire:tmp jenny$ find . -type d |sort -u > /tmp/dirs.lst
temeraire:tmp jenny$ comm -3 /tmp/dirs.lst /tmp/ending.lst 


답변

우리는 간다!

#!/usr/bin/env python3
import os

for curdir,dirnames,filenames in os.walk('.'):
  if len(tuple(filter(lambda x: x.endswith('.ENDING'), filenames))) == 0:
    print(curdir)

또는 교대로 (그리고 더 많은 파이썬) :

#!/usr/bin/env python3
import os

for curdir,dirnames,filenames in os.walk('.'):
    # Props to Cristian Cliupitu for the better python
    if not any(x.endswith('.ENDING') for x in filenames):
        print(curdir)

보너스 DLC 컨텐츠!

find 명령의 (대부분) 수정 된 버전 :

find . -type d \! -exec bash -c 'set nullglob; test -f "{}"/*.ENDING' \; -print


답변

쉘은을 확장 *하지만 귀하의 경우 쉘이 없으며 find 명령으로 실행되는 테스트 명령 만 있습니다. 따라서 존재 여부를 테스트 한 파일의 이름은 그대로 입니다.*.ENDING

대신 다음과 같은 것을 사용해야합니다.

find . -type d \! -execdir sh -c 'test -e {}/*.ENDING' \; -print

이로 인해 테스트 가 실행될 때 sh가 확장 *.ENDING됩니다 .

출처 : UX.SE에서 globbing 찾기


답변

Dennis NolteMikeyB의 답변에서 영감을 얻은 이 솔루션을 생각해 냈습니다.

find . -type d                                                           \
  \! -exec bash -c 'shopt -s failglob; echo "{}"/*.ENDING >/dev/null' \; \
  -print 2>/dev/null

그것은 사실에 근거하여 작동합니다

경우 failglob의 쉘 옵션을 설정하고, 더 일치가 발견되지 않는 오류 메시지가 인쇄되고, 명령이 실행되지 않습니다.

그건 그렇고, 그래서 stderr 이 (으)로 리디렉션되었습니다 /dev/null.


답변

개인적으로 펄에서하겠습니다

#!/usr/bin/perl

use strict;
use warnings;

use File::Find;


#this sub is called by 'find' on each file it traverses. 
sub checkdir {
    #skip non directories. 
    return unless ( -d $File::Find::name );

    #glob will return an empty array if no files math - which evaluates as 'false'
    if ( not glob ( "$File::Find::name/*.ENDING" ) ) {
        print "$File::Find::name does not contain any files that end with '.ENDING'\n";
    }
}

#kick off the search on '.' - set a directory path or list of directories to taste.
#  - you can specify multiple in a list if you so desire. 
find (  \&checkdir, "." );

트릭을 수행해야합니다 (매우 간단한 테스트 사례에서 작동).


답변

여기에 하나의 라이너가 있습니다.

find ./ -type d ! -regex '.*.ENDING$' -printf "%h\n" | sort -u

편집 : 죄송합니다, 작동하지 않습니다.


답변

find . -type d '!' -exec sh -c 'ls -1 "{}"|egrep -q "*ENDING$"' ';' -print
  • q egrep에서 조용합니다

  • 으로 egrep당신은 당신이 필요로하는 정규식을 교환 할 수 있습니다

  • ls -1 "{}" find 명령에서 파일 이름을 출력합니다