获取免费的磁盘空间使用df只显示KB的可用空间? [英] Get free disk space with df to just display free space in kb?

查看:174
本文介绍了获取免费的磁盘空间使用df只显示KB的可用空间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想输出的可用磁盘空间量上的文件系统 /例如

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

如果我运行命令 DF -k /例子,我可以,但只能由人类本性的,实际上看它获取有关kb的可用磁盘空间,良好的信息。

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

我需要这些数据,并在我的shell脚本用在别的地方。我最初想过用剪切但后来我的剧本不会移植到其他磁盘作为可用磁盘空间会有所不同,切不会产生准确的结果。

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

如何才能例子仅仅是可用磁盘空间的输出KB?

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

推荐答案

由于正常 DF -k 输出:

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

您可以在可用(第4列),例如: AWK (previously管路 TR 挤压式重复 -s )的空格):

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

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

一如往常,如果你想要的结果存储在一个变量,使用 VAR = $(命令)语法如下:

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

从<一个href=\"http://stackoverflow.com/questions/19703621/get-free-disk-space-with-df-to-just-display-free-space-in-kb/19703644#comment43896106_19703644\">comment由Tim邦斯你可以使用处理长文件系统的名称 - 直接获得 - 代替,所以它不打印,打破了发动机的一行:

From the comment by Tim Bunce you can handle long filesystem names using --direct to get a - instead, so that it does not print a line that breaks the engine:

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

这篇关于获取免费的磁盘空间使用df只显示KB的可用空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆