Linux 使用 df 获取可用磁盘空间以仅以 kb 为单位显示可用空间?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19703621/
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-07 01:14:26  来源:igfitidea点击:

Get free disk space with df to just display free space in kb?

linuxbashunixcutdf

提问by Whoppa

I'm trying to output the amount of free disk space on the filesystem /example.

我正在尝试输出文件系统上的可用磁盘空间量/example

If I run the command df -k /exampleI can get good information about available disk space in kb but only by being human and actually looking at it.

如果我运行该命令,df -k /example我可以获得有关可用磁盘空间(以 kb 为单位)的良好信息,但只能通过人工操作并实际查看它。

I need to take this data and use it somewhere else in my shell script. I initially thought about using cutbut then my script wont be portable to other disks as free disk space will vary and cut will not produce accurate results.

我需要获取这些数据并在我的 shell 脚本中的其他地方使用它。我最初考虑使用cut但后来我的脚本无法移植到其他磁盘,因为可用磁盘空间会有所不同,并且剪切不会产生准确的结果。

How can I get output of just the free disk-space of example in kb?

如何仅以 kb 为单位获得示例的可用磁盘空间的输出?

采纳答案by fedorqui 'SO stop harming'

To get the output of dfto display the data in kb you just need to use the -kflag:

要获得df以 kb 显示数据的输出,您只需要使用-k标志:

df -k

Also, if you specify a filesystem to df, you will get the values for that specific, instead of all of them:

此外,如果您将文件系统指定为df,您将获得该特定的值,而不是所有值:

df -k /example


Regarding the body of your question: you want to extract the amount of free disk space on a given filesystem. This will require some processing.

关于您的问题的主体:您想提取给定文件系统上的可用磁盘空间量。这将需要一些处理。

Given a normal df -koutput:

给定正常df -k输出:

$ df -k /tmp
Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/sda1        7223800 4270396   2586456  63% /

You can get the Available(4th column) for example with awkor cut(previously piping to trto squeeze-repeats(-s) for spaces):

例如,您可以Available使用awkcut(以前管道trsqueeze-repeats( -s) 以获取空格)获得(第 4 列):

$ df -k /tmp | tail -1 | awk '{print }'
2586456
$ df -k /tmp | tail -1 | tr -s ' ' | cut -d' ' -f4
2586456

As always, if you want to store the result in a variable, use the var=$(command)syntax like this:

与往常一样,如果要将结果存储在变量中,请使用如下var=$(command)语法:

$ myUsed=$(df -k /tmp | tail -1 | awk '{print }')
$ echo "$myUsed"
2586456

Also, from the comment by Tim Bunceyou can handle long filesystem names using --directto get a -instead, so that it does not print a line that breaks the engine:

此外,从Tim Bunce评论中,您可以使用--direct来处理长文件系统名称来获取 a -,这样它就不会打印破坏引擎的行:

$ df -k --direct /tmp
Filesystem     1K-blocks    Used Available Use% Mounted on
-                7223800 4270396   2586456  63% /

回答by sgros

You can use stat(2) command to display free blocks and also to find out how large each block is, e.g.

您可以使用 stat(2) 命令来显示空闲块并找出每个块的大小,例如

stat -f --printf="%a %s\n" /

will display number of free blocks (%a) on a given file system (/) followed by a block size (%s). To get size in kB, you can use bc(1) command as in the following example:

将显示给定文件系统 (/) 上的空闲块数 (%a),后跟块大小 (%s)。要以 kB 为单位获取大小,您可以使用 bc(1) 命令,如下例所示:

stat -f --printf="%a * %s / 1024\n" / | bc

Finally, to put this into a variable is just a matter of using backtick substitution (or $() as in the first answer):

最后,将其放入变量只是使用反引号替换(或第一个答案中的 $() ):

SOMEVAR=`stat -f --printf="%a * %s / 1024\n" / | bc`

回答by Diego Andrés Díaz Espinoza

Show interesting columns only

仅显示有趣的列

 df /example --total -k -h  --output=source,avail
  • --total = grand total at the end
  • -k = block size 1K
  • -h = human readable
  • --output=[FIELD_LIST] column list to show separated by ","
  • --total = 最后的总计
  • -k = 块大小 1K
  • -h = 人类可读
  • --output=[FIELD_LIST] 列列表以“,”分隔显示

Not totally standard (I have seen --output just in Ubuntu man pages), in this case Awk and others just to remove columns are not necessary.

不完全标准(我只在 Ubuntu 手册页中看到过 --output ),在这种情况下,Awk 和其他人只是为了删除列是没有必要的。

回答by Alberto Rojas

This is another solution:

这是另一种解决方案:

df --output=avail -m /example | tail -1

output:

输出:

6415

6415