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
Get free disk space with df to just display free space in kb?
提问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 /example
I 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 cut
but 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 df
to display the data in kb you just need to use the -k
flag:
要获得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 -k
output:
给定正常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 awk
or cut
(previously piping to tr
to squeeze-repeats
(-s
) for spaces):
例如,您可以Available
使用awk
或cut
(以前管道tr
到squeeze-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 --direct
to 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