bash에서 현재 파티션의 사용 가능한 디스크 공간 확인
bash에서 설치 프로그램을 작성 중입니다. 사용자는 대상 디렉토리로 이동하여 설치 스크립트를 실행하므로 첫 번째 조치는 충분한 공간이 있는지 확인하는 것입니다. df가 모든 파일 시스템을보고한다는 것을 알고 있지만 대상 디렉토리가있는 파티션에 대해서만 여유 공간을 확보하는 방법이 있는지 궁금합니다.
편집-내가 생각해 낸 대답
df $PWD | awk '/[0-9]%/{print $(NF-2)}'
df가 터미널에 맞게 출력을 형식화하는 것처럼 보이기 때문에 약간 마운트 포인트 이름이 길면 출력이 한 줄 아래로 이동합니다.
예:
df -k .
현재 디렉토리.
df -k /some/dir
특정 디렉토리를 확인하려면
stat(1)
시스템에 명령이 있으면 명령 을 점검 할 수도 있습니다 . 스크립트를보다 쉽게 구문 분석 할 수 있도록 출력 형식을 지정할 수 있습니다. 여기 작은 예가 있습니다 :
$ echo $(($(stat -f --format="%a*%S" .)))
df
command : 파일 시스템 디스크 공간 사용량보고du
명령 : 파일 공간 사용량 추정
사용 가능한 디스크 공간을 입력 df -h
하거나 df -k
나열하려면 다음을 수행하십시오.
$ df -h
또는
$ df -k
du
하나 이상의 파일 또는 디렉토리가 사용중인 공간을 표시합니다.
$ du -sh
-s
옵션은 디렉토리가 사용중인 공간을 요약하고 -h
옵션은 사람이 읽을 수있는 출력을 제공합니다 .
나는 이것이 바로이 질문에 대한 ThinkingMedia의 답변에 대한 의견이거나 편집해야한다고 생각합니다 ( bash에서 현재 파티션의 사용 가능한 디스크 공간 확인 ). (댓글이 충분하지 않음) 의견을 말할 수 없으며 편집이 거부되었습니다 (이유 : "이것은 의견 또는 답변이어야합니다"). 그러니 제발, SO 우주의 힘은 다른 사람의 "답변"을 반복하고 고친 것에 대해 저를 망설이지 마십시오. 그러나 인터넷상의 누군가가 잘못되었습니다! ™ 그들은 그것을 고치게하지 않을 것입니다.
코드
df --output=avail -h "$PWD" | sed '1d;s/[^0-9]//g'
상당한 결함이 있습니다. 예, 50G
50으로 무료로 출력 되지만 5.0M
50 3.4G
으로, 34 15K
로, 15 로 무료로 출력 됩니다.
사용 가능한 디스크 공간을 확인하기 위해 스크립트를 만들려면 확인하려는 장치를 알아야합니다. sed
위의 예에서와 같이 숫자가 더 이상 의미가 없어 제거하십시오 .
실제로 작동하려면 다음과 같은 작업을 수행해야합니다.
FREE=`df -k --output=avail "$PWD" | tail -n1` # df -k not df -h
if [[ $FREE -lt 10485760 ]]; then # 10G = 10*1024*1024k
# less than 10GBs free!
fi;
또한 설치 프로그램 df -k $INSTALL_TARGET_DIRECTORY
이보다 더 의미가있을 수 있습니다 df -k "$PWD"
. 마지막으로, --output
df / linux의 모든 버전에서이 플래그를 사용할 수있는 것은 아닙니다.
이것을 사용하여 서버의 마운트 지점을 모니터하려는 누군가를위한 완전한 예제. 이 예는 / var / spool이 5G 미만인지 확인하고 사람에게 이메일을 보냅니다.
#!/bin/bash
# -----------------------------------------------------------------------------------------
# SUMMARY: Check if MOUNT is under certain quota, mail us if this is the case
# DETAILS: If under 5G we have it alert us via email. blah blah
# -----------------------------------------------------------------------------------------
# CRON: 0 0,4,8,12,16 * * * /var/www/httpd-config/server_scripts/clear_root_spool_log.bash
MOUNTP=/var/spool # mount drive to check
LIMITSIZE=5485760 # 5G = 10*1024*1024k # limit size in GB (FLOOR QUOTA)
FREE=$(df -k --output=avail "$MOUNTP" | tail -n1) # df -k not df -h
LOG=/tmp/log-$(basename ${0}).log
MAILCMD=mail
EMAILIDS="dude@wheres.mycar"
MAILMESSAGE=/tmp/tmp-$(basename ${0})
# -----------------------------------------------------------------------------------------
function email_on_failure(){
sMess="$1"
echo "" >$MAILMESSAGE
echo "Hostname: $(hostname)" >>$MAILMESSAGE
echo "Date & Time: $(date)" >>$MAILMESSAGE
# Email letter formation here:
echo -e "\n[ $(date +%Y%m%d_%H%M%S%Z) ] Current Status:\n\n" >>$MAILMESSAGE
cat $sMess >>$MAILMESSAGE
echo "" >>$MAILMESSAGE
echo "*** This email generated by $(basename $0) shell script ***" >>$MAILMESSAGE
echo "*** Please don't reply this email, this is just notification email ***" >>$MAILMESSAGE
# sending email (need to have an email client set up or sendmail)
$MAILCMD -s "Urgent MAIL Alert For $(hostname) AWS Server" "$EMAILIDS" < $MAILMESSAGE
[[ -f $MAILMESSAGE ]] && rm -f $MAILMESSAGE
}
# -----------------------------------------------------------------------------------------
if [[ $FREE -lt $LIMITSIZE ]]; then
echo "Writing to $LOG"
echo "MAIL ERROR: Less than $((($FREE/1000))) MB free (QUOTA) on $MOUNTP!" | tee ${LOG}
echo -e "\nPotential Files To Delete:" | tee -a ${LOG}
find $MOUNTP -xdev -type f -size +500M -exec du -sh {} ';' | sort -rh | head -n20 | tee -a ${LOG}
email_on_failure ${LOG}
else
echo "Currently $(((($FREE-$LIMITSIZE)/1000))) MB of QUOTA available of on $MOUNTP. "
fi
df --output=avail -B 1 "$PWD" |tail -n 1
이 방법으로 바이트 단위의 크기를 얻습니다.
리눅스에서 GB 또는 TB에서 특정 디렉토리의 사용법을 알기 위해 명령은 다음과 같습니다.
df -h / dir / inner_dir /
or
df -sh / dir / inner_dir /
and to know the usage of the specific directory in bits in linux the command is,
df-k /dir/inner_dir/
To get the current free disk space in human readable format with letters. The following will output the free space for the current partition, drop the header and strip letters.
df --output=avail -h "$PWD" | sed '1d;s/[^0-9]//g'
Outputs 50G
free as 50
.
To make this part of a free disk space condition in bash script.
FREE=`df --output=avail -h "$PWD" | sed '1d;s/[^0-9]//g'`
if [[ $FREE -lt 10 ]]; then
# less than 10GBs free!
fi;
Type in the command shell:
df -h
or
df -m
or
df -k
It will show the list of free disk spaces for each mount point.
You can show/view single column also.
Type:
df -m |awk '{print $3}'
Note: Here 3 is the column number. You can choose which column you need.
참고URL : https://stackoverflow.com/questions/8110530/check-free-disk-space-for-current-partition-in-bash
'IT story' 카테고리의 다른 글
ASP.NET 양식에 필요한 확인란을 만들려면 어떻게합니까? (0) | 2020.08.05 |
---|---|
.tar.gz 파일을 만들 때 디렉토리 제외 (0) | 2020.08.05 |
MySQL 최대 메모리 사용량 (0) | 2020.08.05 |
한 목록의 모든 값을 다른 목록에서 제거 하시겠습니까? (0) | 2020.08.05 |
숭고한 텍스트처럼 Vim 멀티 라인 편집? (0) | 2020.08.05 |