"sort" 명령에 파이프를 연결하여 "find -print0" 출력 정렬

"sort" 명령에 파이프를 연결하여 "find -print0" 출력 정렬

find출력 을 명령으로 연결하기 전에 출력을 알파벳순으로 정렬할 수 있어야 합니다 . 사이에 입력해도 | sort |작동하지 않습니다. 어떻게 해야 합니까?

find folder1 folder2 -name "*.txt" -print0 | xargs -0 myCommand

답변1

find평소대로 사용 하고 NUL로 줄을 구분하십시오. GNU는 sort-z 스위치를 사용하여 이를 처리할 수 있습니다:

find . -print0 | sort -z | xargs -r0 yourcommand

답변2

일부 버전에는 Null 종료 레코드를 허용하는 옵션이 sort있습니다 .-z

find folder1 folder2 -name "*.txt" -print0 | sort -z | xargs -r0 myCommand

또한 이를 수행하기 위해 고급 스크립트를 작성할 수도 있습니다.

find folder1 folder2 -name "*.txt" -print0 | python -c 'import sys; sys.stdout.write("\0".join(sorted(sys.stdin.read().split("\0"))))' | xargs -r0 myCommand

인수와 함께 호출되는지 확인하려면 -r옵션을 추가하세요 .xargsmyCommand

답변3

정렬을 위한 플래그가 필요한 것 같습니다 -n#

인간 정렬에 따르면:

-n, --numeric-sort
    compare according to string numerical value

편집하다

print0이 이것과 관련이 있을 수 있습니다. 방금 테스트했습니다. print0을 꺼내면 플래그를 사용하여 문자열을 null로 종료할 수 있습니다 -z.

답변4

일부 구현에서는 매개변수를 find통해 직접 순회 순서를 지정하도록 지원합니다 -s.

$ find -s . -name '*.json'

FreeBSD에서 찾기매뉴얼 페이지:

-s       Cause find to traverse the file hierarchies in lexicographical
         order, i.e., alphabetical order within each directory.  Note:
         `find -s' and `find | sort' may give different results.

관련 정보