제외가 포함된 경로에서 가장 오래된 폴더 제거

제외가 포함된 경로에서 가장 오래된 폴더 제거

나는 다음 스레드를 참조했다.특정 디렉토리에서 가장 오래된 디렉토리를 삭제하는 방법은 무엇입니까? 그리고 수용된 솔루션은 완벽합니다. 하지만 가장 오래된 폴더 중 하나를 제외해야 합니다. 제공된 솔루션에서 이 요구 사항을 어떻게 충족할 수 있습니까?

답변1

당신은 조정할 수 있습니다독창적인 솔루션제외되지 않은 파일이 발견될 때까지 파일을 읽습니다.

while IFS= read -r -d '' line
do
    file="${line#* }"
    if [ "$file" = './my-excluded-directory' ]
    then
        continue
    fi
    [do stuff with $file]
    break
done < <(find . -maxdepth 1 -type d -printf '%T@ %p\0' | sort -z -n)

( -d ''문자열에 NUL 문자가 포함될 수 없으므로 원본과 동일합니다.)

read그러나 긴 목록은 읽기가 느리고@모스비예, 꽤 불쾌한 구문을 사용하여 필터링할 수 있습니다 find.

IFS= read -r -d '' < <(find . -maxdepth 1 -type d \( -name 'my-excluded-directory' -prune -false \) -o -printf '%T@ %p\0' | sort -z -n)
file="${REPLY#* }"
# do something with $file here

답변2

"비밀스러운" 방법은 처음에 제외된 디렉터리의 타임스탬프를 변경하고 마지막에 해당 타임스탬프를 복원하는 것입니다.

$ cd "$(mktemp --directory)"
$ echo foo > example.txt
$ modified="$(date --reference=example.txt +%s)" # Get its original modification time
$ touch example.txt # Set the modification time to now
[delete the oldest file]
$ touch --date="@$modified" example.txt # Restore; the "@" denotes a timestamp

일반적인 고려 사항이 적용됩니다.

  • 완료되기 전에(또는 정전 등으로) 종료되면 원래 타임스탬프가 복원되지 않습니다. 타임스탬프를 복구하는 것이 정말 중요한 경우 touch --reference="$path" "${path}.timestamp"타임스탬프를 실제 파일에 저장하고 touch --reference="${path}.timestamp" "$path".
  • 옵션 이름은 GNU coretools를 기반으로 합니다. 다른 *nix에서 동일한 작업을 수행하려면 읽기 어려운 옵션 이름을 사용해야 할 수도 있습니다.

답변3

그리고 zsh:

set -o extendedglob # best in ~/.zshrc
rm -rf -- ^that-folder-to-keep(D/Om[1])

다른 Q&A에 제공된 솔루션을 기반으로 합니다.

IFS= read -r -d '' line < <(
  find . -maxdepth 1 \
    ! -name that-folder-to-keep \
    -type d -printf '%T@ %p\0' 2>/dev/null | sort -z -n) &&
  rm -rf "${line#* }"

두 번째로 오래된 항목을 삭제하려면 다음 단계를 따르세요.

zsh:

rm -rf -- *(D/Om[2])

GNU 유틸리티:

{ IFS= read -r -d '' line &&
  IFS= read -r -d '' line &&; } < <(
  find . -maxdepth 1 \
    -type d -printf '%T@ %p\0' 2>/dev/null | sort -z -n) &&
  rm -rf "${line#* }"

답변4

#!/bin/bash
while IFS= read -r -d '' line
do
    file="${line#* }"
    if [ "$file" = './Development_Instance' ]
    then
        continue
fi

if [ "$file" = './lost+found' ]
then
continue
fi
    echo $file
    break
done < <(find . -maxdepth 1 -type d -printf '%T@ %p\0' | sort -z -n)

그것이 내가 한 방법입니다. 나는 이것이 우아한 방법이 아니라는 것을 알고 있습니다. 그것은 단지 나를 위해 일을 합니다.

관련 정보