[bash] Bash에서 폴더 크기 확인

디렉토리 크기를 계산하는 스크립트를 작성하려고하는데 크기가 10GB 미만이고 2GB보다 큰 경우 조치를 수행하십시오. 폴더 이름은 어디에서 언급해야합니까?

# 10GB
SIZE="1074747474"

# check the current size
CHECK="`du /data/sflow_log/`"
if [ "$CHECK" -gt "$SIZE" ]; then
  echo "DONE"
fi



답변

넌 할 수있어:

du -h your_directory

대상 디렉토리의 크기를 제공합니다.

짧은 출력을 원한다면 du -hcs your_directory좋습니다.


답변

하위 폴더가 아닌 폴더 크기 만 보려면 다음을 사용할 수 있습니다.

du -hs /path/to/directory

최신 정보:

du사용 된 디스크 공간 을 보여줍니다. 파일 크기가 아닙니다.

--apparent-size실제 파일 크기의 합계를 보려는 경우 사용할 수 있습니다 .

--apparent-size
      print  apparent  sizes,  rather  than  disk  usage; although the apparent size is usually smaller, it may be larger due to holes in ('sparse')
      files, internal fragmentation, indirect blocks, and the like

물론 -h스크립트 안에 (인간이 읽을 수있는) 옵션이 필요하지 않습니다 .

대신 -b스크립트 내에서보다 쉽게 ​​비교할 수 있습니다 .

그러나 그 자체로 -b적용 --apparent-size됩니다. 그리고 그것은 당신이 필요로하지 않을 수도 있습니다.

-b, --bytes
      equivalent to '--apparent-size --block-size=1'

그래서 당신은 --block-size또는-B

#!/bin/bash
SIZE=$(du -B 1 /path/to/directory | cut -f 1 -d "   ")
# 2GB = 2147483648 bytes
# 10GB = 10737418240 bytes
if [[ $SIZE -gt 2147483648 && $SIZE -lt 10737418240 ]]; then
    echo 'Condition returned True'
fi


답변

요약 ( -s)과 바이트 ( -b)를 사용하십시오 . 을 사용하여 요약의 첫 번째 필드를 잘라낼 수 있습니다 cut. 함께 모아서:

CHECK=$(du -sb /data/sflow_log | cut -f1)


답변

디렉토리의 크기를 얻으려면 더 이상 아무것도 없습니다.

du --max-depth=0 ./directory

출력은 다음과 같습니다

5234232       ./directory


답변

디렉토리 내의 모든 디렉토리의 크기를 확인하려면 다음을 사용할 수 있습니다.

du -h --max-depth=1


답변

폴더의 전체 크기와 MB 또는 GB 형식을 보려면 아래 스크립트를 시도하십시오.

$du -s --block-size=M /path/to/your/directory/


답변

# 10GB
SIZE="10"


# check the current size
CHECK="`du -hs /media/662499e1-b699-19ad-57b3-acb127aa5a2b/Aufnahmen`"
CHECK=${CHECK%G*}
echo "Current Foldersize: $CHECK GB"

if (( $(echo "$CHECK > $SIZE" |bc -l) )); then
        echo "Folder is bigger than $SIZE GB"
else
        echo "Folder is smaller than $SIZE GB"
fi