
GNU가 지정된 파일 이름까지 제외 항목을 찾도록 노력 중입니다.
다음 샘플 트리를 살펴보세요.
./foo
./foo/another.txt
./foo/bar
./foo/bar/world
./foo/bar/world/test.txt
./foo/bar/world/hello.txt
(및 다른 디렉토리에도 다른 파일이 많이 있을 것이므로 world
단순히 검색하는 것이 아닙니다 hello.txt
.)
나는 'test.txt'와 그 상위 디렉토리를 제외한 모든 것을 일치시키고 싶습니다.
출력은 단순히 foo/bar/world/hello.txt
.
이 명령은 올바른 결과를 생성하지만 꽤 지저분하며 동일한 이름을 가진 디렉터리가 여러 개 있는 경우 잘못된 결과를 생성합니다.
find * ! -name test.txt -a ! -name foo -a ! -name bar -a ! -name world
답변1
find
디렉토리가 아닌 파일에만 관심이 있다고 알려주세요 .
find ./foo -type f ! -name test.txt
업데이트:
다음과 같은 약간 더 복잡한 예가 있다고 가정해 보겠습니다.
$ find ./foo
./foo
./foo/baz
./foo/baz/b.csv
./foo/baz/a.txt
./foo/bar
./foo/bar/c.txt
./foo/bar/world
./foo/bar/world/hello.txt
./foo/bar/world/test.txt
목표가 항목을 삭제하는 것이라면 -depth
파일이 디렉터리 앞에 표시되도록 지정해야 합니다.
$ find ./foo -depth
./foo/baz/b.csv
./foo/baz/a.txt
./foo/baz
./foo/bar/c.txt
./foo/bar/world/hello.txt
./foo/bar/world/test.txt
./foo/bar/world
./foo/bar
./foo
유지하려는 경로를 알고 있다면 해당 경로와 일치하는 정규식을 고안할 수 있습니다.
$ find ./foo -depth -regextype posix-extended -regex '^\./foo(/bar(/world(/test.txt)?)?)?'
./foo/bar/world/test.txt
./foo/bar/world
./foo/bar
./foo
그런 다음 정규식을 무효화하여 제거하려는 항목을 얻을 수 있습니다.
$ find ./foo -depth -regextype posix-extended ! -regex '^\./foo(/bar(/world(/test.txt)?)?)?'
./foo/baz/b.csv
./foo/baz/a.txt
./foo/baz
./foo/bar/c.txt
./foo/bar/world/hello.txt
답변2
트리의 정확한 깊이를 알고 있는 경우(예: 세 번째 폴더부터 파일을 가져오려는 경우) 다음을 사용할 수 있습니다.
find . -mindepth $depth ! -name test.txt
귀하의 디렉토리 구조를 통해 다음을 얻습니다.
$ find . -mindepth 4 ! -name test.txt
./foo/bar/world/hello.txt
정확히 당신이 기대하는 것입니다.
편집: 이것이 더 좋을 것입니다(그러나 더 추악합니다!). 가 위치한 디렉토리를 찾고 test.txt
그 안의 모든 파일을 찾습니다. 장점은 상위 경로와 완전히 독립적이며 자동으로 계산된다는 것입니다.
EXCLUDEFNAME="test.txt"; find $(find . -name "$EXCLUDEFNAME" -exec dirname {} \; -quit) -mindepth 1 ! -name "$EXCLUDEFNAME"
여러 줄이 더 좋습니다:
EXCLUDEFNAME="test.txt"
targetpath="$(find . -name "$EXCLUDEFNAME" -exec dirname {} \;)"
find "$targetpath" -mindepth 1 ! -name "$EXCLUDEFNAME"