빈 디렉터리 찾기
지정된 디렉토리 목록에 대한 빈 디렉토리를 찾아야 합니다.일부 디렉토리에는 디렉토리가 포함되어 있습니다.
내부 디렉토리도 비어 있으면 메인 디렉토리가 비어 있다고 할 수 있습니다.그렇지 않으면 비어 있지 않습니다.
이거 어떻게 테스트해요?
예를 들어 다음과 같습니다.
A>A1(file1),A2 this is not empty beacuse of file1
B>B1(no file) this is empty
C>C1,C2 this is empty
빈 디렉토리로 무엇을 하고 싶은지에 따라 다릅니다.트리 내의 빈 디렉토리를 모두 삭제하려면 다음 명령을 사용합니다.test디렉토리로 이동합니다.
find test -depth -empty -delete
위의 명령어에서는 빈 파일도 삭제되므로 -type d 옵션을 사용하여 이를 방지하십시오.
find test -depth -type d -empty -delete
떨어지다-delete파일 및 디렉토리가 일치하는지 확인합니다.
빈 디렉토리 트리의 정의가 파일을 포함하지 않는 경우, 다음과 같은 조건에 근거해 어떤 것을 조합할 수 있습니다.find test -type f아무것도 반환하지 않습니다.
find는 뛰어난 유틸리티이며, RTFM을 조기에 도입하여 많은 경우 RTFM이 얼마나 많은 기능을 수행할 수 있는지를 실제로 이해할 수 있습니다.
다음 명령을 사용할 수 있습니다.
find . -type d -empty
확인하다find <dir> -type f모든 것을 출력합니다.다음은 예를 제시하겠습니다.
for dir in A B C; do
[ -z "`find $dir -type f`" ] && echo "$dir is empty"
done
find directory -mindepth 1 -type d -empty -delete
이것이 내가 가장 흥미로웠던 버전이다.내부 디렉토리에서 실행되면 아래 빈 디렉토리가 모두 삭제됩니다(빈 디렉토리만 포함된 디렉토리는 빈 디렉토리로 간주됩니다).
mindepth 옵션을 사용하면 디렉토리가 비어 있을 때 디렉토리 자체가 삭제되지 않습니다.
find . -type d -empty
는 현재 트리의 빈 디렉토리 및 서브 디렉토리를 검색하여 나열합니다.예: 빈 dir 및 subdir 목록:
./2047
./2032
./2049
./2063
./NRCP26LUCcct1/2039
./NRCP26LUCcct1/2054
./NRCP26LUCcct1/2075
./NRCP26LUCcct1/2070
디렉토리 조작은 행해지지 않습니다.간단하게 리스트 되어 있습니다.난 이거면 돼.
빈 디르만 찾으면 됩니다.
빈 디렉토리(질문 제목에 지정된 대로)를 찾기 위해서는 mosg의 답변이 정확합니다.
find -type d -empty
그렇지만-empty아주 오래된 제품에서는 구할 수 없을지도 모른다find(예를 들어 HP-UX의 경우)이 경우 아래 섹션에서 설명하는 기술을 참조하십시오.디렉토리는 비어있습니까?
빈 dir 삭제
이것은 조금 까다롭습니다.디렉토리라고 가정MyDir에 빈 디렉토리가 포함되어 있습니다.이러한 빈 디렉토리를 삭제하면,MyDir는 빈 디렉토리가 되므로 삭제해야 합니다.따라서 명령어를 사용합니다.rmdir옵션으로--parents(또는-p는 가능한 경우 부모 디렉토리도 삭제합니다.
find -type d -empty -exec rmdir -vp --ignore-fail-on-non-empty {} +
find을 +는 아직되지 않습니다. 을 할 수 .;★★★★
find -type d -empty -exec rmdir -vp --ignore-fail-on-non-empty {} `;`
디렉토리가 비어 있습니까?
이러한 답변의 대부분은 디렉토리가 비어 있는지 확인하는 방법을 설명합니다.따라서 여기에서는 제가 알고 있는 세 가지 다른 기술을 제시하겠습니다.
[ $(find your/dir -prune -empty) = your/dir ]d=your/dir if [ x$(find "$d" -prune -empty) = x"$d" ] then echo "empty (directory or file)" else echo "contains files (or does not exist)" fi변형:
d=your/dir if [ x$(find "$d" -prune -empty -type d) = x"$d" ] then echo "empty directory" else echo "contains files (or does not exist or is not a directory)" fi설명:
find -prunefind -maxdepth 0less 자find -type ddirectorys only (디렉토리만)find -empty및> mkdir -v empty1 empty2 not_empty mkdir: created directory 'empty1' mkdir: created directory 'empty2' mkdir: created directory 'not_empty' > touch not_empty/file > find empty1 empty2 not_empty -prune -empty empty1 empty2
(( ${#files} ))는 100%다
bash하지만 서브셸을 호출합니다.그 아이디어는 Bruno De Fraine에서 나왔고 팀밥의 코멘트로 개선되었다.bash 를 사용하고, 스크립트를 휴대할 필요가 없는 경우는, 이것을 추천합니다.files=$(shopt -s nullglob dotglob; echo your/dir/*) if (( ${#files} )) then echo "contains files" else echo "empty (or does not exist or is a file)" fi주의: 빈 디렉토리와 존재하지 않는 디렉토리의 차이는 없습니다(또한 제공된 경로가 파일인 경우에도 마찬가지).
[ $(ls -A your/dir) ]이 트릭은 2007년에 게재된 nixCraft의 기사에서 영감을 얻었다.Andrew Taylor는 2008년에 답했고 gr8can8dian은 2011년에 답했습니다.
if [ "$(ls -A your/dir)" ] then echo "contains files" else echo "empty (or does not exist or is a file)" fi또는 한 줄 배시즘 버전:
[[ "$(ls -A your/dir)" ]] && echo "contains files" || echo "empty or ..."ls$?=2츠요시그러나 파일과 빈 디렉토리의 차이는 없습니다.
는 요?rmdir *이 명령어는 빈 디렉토리가 아닌 디렉토리에서 실패합니다.
이 재귀 함수는 다음과 같은 효과를 발휘합니다.
# Bash
findempty() {
find ${1:-.} -mindepth 1 -maxdepth 1 -type d | while read -r dir
do
if [[ -z "$(find "$dir" -mindepth 1 -type f)" ]] >/dev/null
then
findempty "$dir"
echo "$dir"
fi
done
}
다음의 디렉토리 구조의 예를 나타냅니다.
.|--dir1/|--dir2/| '-- dirB/|--dir3/| '-- dirC/| '-- file5|--dir4/|-- dirD/| '-- file4"--dir5/"--dirE/"--dir_V/
이 기능을 실행하면 다음과 같이 됩니다.
./param1./dir5/dirE/dir_V./param5/param5/paramse./param5./dir2/dirB./param2
/dir4/dirD 콜을 findempty "$dir"그 fi이 함수는 결과에 해당 디렉토리를 포함합니다.
있는 않는 ) 1을 반환하고, 그렇지 않은 0을 코드를 "da"로할 수 있습니다).!★★★★★★★★★★★★★★★★★★★★★)
find $dir -type d -prune -empty -exec false {} +
다음과 같이 간단한 구조를 만들었습니다.
test/
test/test2/
test/test2/test2.2/
test/test3/
test/test3/file
test/test3/file에 정크 텍스트가 포함되어 있습니다.
<고객명>의 find test -emptysyslog"를 반환한다.test/test2/test2.2빈 디렉토리로만 사용됩니다.
간단한 접근법은 다음과 같습니다.
$ [ "$(ls -A /path/to/direcory)" ] && echo "not empty" || echo "its empty"
또한.
if [ "$(ls -A /path/to/direcory)" ]; then
echo "its not empty"
else
echo "empty directory"
find . -name -type d -ls |awk '($2==0){print $11}'
언급URL : https://stackoverflow.com/questions/2810838/finding-empty-directories
'programing' 카테고리의 다른 글
| Git에서 최신 로컬 커밋을 취소하려면 어떻게 해야 하나요? (0) | 2023.04.15 |
|---|---|
| Go 문자열을 인쇄하지 않고 포맷하시겠습니까? (0) | 2023.04.15 |
| Android-Facebook 앱의 주요 해시 (0) | 2023.04.15 |
| 대소문자를 구분하지 않는 'Contains(string)' (0) | 2023.04.15 |
| 회선 끝 설정 변경 방법 (0) | 2023.04.15 |