Linux 在bash中检查当前分区的可用磁盘空间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8110530/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 07:10:47  来源:igfitidea点击:

Check free disk space for current partition in bash

linuxbash

提问by Greg Reynolds

I am writing an installer in bash. The user will go to the target directory and runs the install script, so the first action should be to check that there is enough space. I know that df will report all file systems, but I was wondering if there was a way to get the free space just for the partition that the target directory is on.

我正在用 bash 编写一个安装程序。用户将转到目标目录并运行安装脚本,因此第一个操作应该是检查是否有足够的空间。我知道 df 会报告所有文件系统,但我想知道是否有办法为目标目录所在的分区获取可用空间。

Edit - the answer I came up with

编辑 - 我想出的答案

df $PWD | awk '/[0-9]%/{print $(NF-2)}'

Slightly odd because df seems to format its output to fit the terminal, so with a long mount point name the output is shifted down a line

有点奇怪,因为 df 似乎格式化其输出以适合终端,因此使用长挂载点名称将输出向下移动一行

采纳答案by Mat

Yes:

是的:

df -k .

for the current directory.

对于当前目录。

df -k /some/dir

if you want to check a specific directory.

如果要检查特定目录。

You might also want to check out the stat(1)command if your system has it. You can specify output formats to make it easier for your script to parse. Here's a little example:

stat(1)如果您的系统有该命令,您可能还想查看该 命令。您可以指定输出格式,以便更轻松地解析脚本。这是一个小例子:

$ echo $(($(stat -f --format="%a*%S" .)))

回答by Girish

  1. dfcommand : Report file system disk space usage
  2. ducommand : Estimate file space usage
  1. df命令:报告文件系统磁盘空间使用情况
  2. du命令:估计文件空间使用情况

Type df -hor df -kto list free disk space:

键入df -hdf -k以列出可用磁盘空间:

 $ df -h

OR

或者

 $ df -k

dushows how much space one or more files or directories is using:

du显示一个或多个文件或目录正在使用多少空间:

 $ du -sh

The -soption summarizes the space a directory is using and -hoption provides Human-readableoutput.

-s选项总结了目录正在使用的空间,-h选项提供了人类可读的输出。

回答by Reactgular

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 50Gfree as 50.

50G免费输出为50.

To make this part of a free disk space condition in bash script.

在 bash 脚本中使这部分成为可用磁盘空间条件。

FREE=`df --output=avail -h "$PWD" | sed '1d;s/[^0-9]//g'`
if [[ $FREE -lt 10 ]]; then
         # less than 10GBs free!
fi;

回答by trs

I think this should be a comment or an edit to ThinkingMedia's answer on this very question (Check free disk space for current partition in bash), but I am not allowed to comment (not enough rep) and my edit has been rejected (reason: "this should be a comment or an answer"). So please, powers of the SO universe, don't damn me for repeating and fixing someone else's "answer". But someone on the internet was wrong!? and they wouldn't let me fix it.

我认为这应该是对 ThinkingMedia 对这个问题的回答的评论或编辑(检查 bash 中当前分区的可用磁盘空间),但我不允许发表评论(没有足够的代表)并且我的编辑被拒绝(原因: “这应该是评论或答案”)。所以,SO 宇宙的力量,请不要因为我重复和修正别人的“答案”而诅咒我。但是网上有人错了!?他们不让我修。

The code

编码

  df --output=avail -h "$PWD" | sed '1d;s/[^0-9]//g'

has a substantial flaw: Yes, it will output 50Gfree as 50 -- but it will also output 5.0Mfree as 50 or 3.4Gfree as 34 or 15Kfree as 15.

有一个很大的缺陷:是的,它会输出50G50 的免费 - 但它也会输出5.0M50 或3.4G免费的 34 或15K免费的 15。

To create a script with the purpose of checking for a certain amount of free disk space you have to know the unit you're checking against. Remove it (as seddoes in the example above) the numbers don't make sense anymore.

要创建一个用于检查一定数量的可用磁盘空间的脚本,您必须知道要检查的单位。删除它(如上sed例所示),数字不再有意义。

If you actually want it to work, you will have to do something like:

如果您确实希望它工作,则必须执行以下操作:

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;

Also for an installer to df -k $INSTALL_TARGET_DIRECTORYmight make more sense than df -k "$PWD". Finally, please note that the --outputflag is not available in every version of df / linux.

同样对于安装程序来说df -k $INSTALL_TARGET_DIRECTORY可能比df -k "$PWD". 最后,请注意该--output标志并非在 df / linux 的每个版本中都可用。

回答by ganesh hegde

To know the usage of the specific directory in GB's or TB's in linux the command is,

要了解 Linux 中 GB 或 TB 中特定目录的使用情况,命令是,

df -h /dir/inner_dir/

df -h /dir/inner_dir/

 or

df -sh /dir/inner_dir/

df -sh /dir/inner_dir/

and to know the usage of the specific directory in bits in linux the command is,

并以位为单位了解特定目录在 linux 中的用法,命令是,

df-k /dir/inner_dir/

df-k /dir/inner_dir/

回答by Mike Q

A complete example for someone who may want to use this to monitor a mount point on a server. This example will check if /var/spool is under 5G and email the person :

对于可能想要使用它来监视服务器上的安装点的人的完整示例。此示例将检查 /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 
df --output=avail -B 1 "$PWD" |tail -n 1
).log MAILCMD=mail EMAILIDS="[email protected]" MAILMESSAGE=/tmp/tmp-$(basename
 df -h 
) # ----------------------------------------------------------------------------------------- function email_on_failure(){ sMess="" 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
df -m |awk '{print }'
) 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

回答by xerostomus

##代码##

you get size in bytes this way.

您可以通过这种方式获得大小(以字节为单位)。

回答by Masud Sarker

Type in the command shell:

在命令行输入:

##代码##

or

或者

df -m

df -m

or

或者

df -k

df -k

It will show the list of free disk spaces for each mount point.

它将显示每个挂载点的可用磁盘空间列表。

You can show/view single column also.

您也可以显示/查看单列。

Type:

类型:

##代码##

Note: Here 3 is the column number. You can choose which column you need.

注意:这里 3 是列号。您可以选择您需要的列。