bash 我怎样才能*只*获得bash中磁盘上可用的字节数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33249591/
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
How can I *only* get the number of bytes available on a disk in bash?
提问by Ryan
df
does a great job for an overview. But what if I want to set a variable in a shell script to the number of bytes available on a disk?
df
概述做得很好。但是,如果我想将 shell 脚本中的变量设置为磁盘上可用的字节数怎么办?
Example:
例子:
$ df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda 1111111111 2222222 33333333 10% /
tmpfs 44444444 555 66666666 1% /dev/shm
But I just want to return 33333333
(bytes available on /
), not the whole df
output.
但我只想返回33333333
(可用字节数/
),而不是整个df
输出。
采纳答案by Gilles 'SO- stop being evil'
Portably:
便携:
df -P /dev/sda1 | awk 'NR==2 {print }'
The -P
option ensures that df
will print output in the expected format, and will in particular not break the line after the device name even if it's long. Passing the device name as an argument to df
removes any danger from parsing, such as getting information for /dev/sda10
when you're querying /dev/sda1
. df -P
just prints two lines, the header line (which you ignore) and the one data line where you print the desired column.
该-P
选项可确保df
以预期格式打印输出,特别是即使设备名称很长也不会断开设备名称后面的行。将设备名称作为参数传递以df
消除解析过程中的任何危险,例如获取/dev/sda10
查询时的信息/dev/sda1
。df -P
只打印两行,标题行(您忽略)和打印所需列的一行数据。
There is a risk that df
will display a device name containing spaces, for example if the volume is mounted by name and the name contain spaces, or for an NFS volume whose remote mount point contains spaces. In this case, there's no fully portable way to parse the output of df
. If you're confident that df
will display the exact device name you pass to it (this isn't always the case), you can strip it:
存在df
显示包含空格的设备名称的风险,例如,如果卷是按名称挂载并且名称包含空格,或者对于远程挂载点包含空格的 NFS 卷。在这种情况下,没有完全可移植的方式来解析df
. 如果您确信它df
会显示您传递给它的确切设备名称(并非总是如此),您可以将其剥离:
df -P -- "$device" | awk -vn=${#device} 'NR==2 {df | awk '=="/dev/sda"{print }'
= substr(df | grep sda | awk '{print }'
, n+1); print }'
回答by Avinash Raj
You may use awk,
你可以使用awk,
df --output=avail
回答by ergonaut
You can use an awk
你可以使用 awk
stat -f -c '%f' /
回答by Alex Mantaut
Only in Linux
仅在 Linux 中
echo $((`stat -f -c '%f*%S' /`))
回答by Zouppen
You can query disk status with stat
as well. To query free blocks on filesystem mounted at /
:
您也可以查询磁盘状态stat
。要查询挂载于 的文件系统上的空闲块/
:
df -P --total | grep 'total' | awk '{print }'
To get the result in bytes instead of blocks, you can use shell's arithmetic:
要获得以字节而不是块为单位的结果,您可以使用 shell 的算法:
##代码##回答by Odondon Labama
You may get exact number of bytes with df:
您可以使用 df 获得确切的字节数:
df -B1 /
Filesystem 1B-blocks Used Available Use% Mounted on
/dev/mapper/cl-root 32893632512 13080072192 18119061504 42% /
df -B1 /
Filesystem 1B-blocks Used Available Use% Mounted on
/dev/mapper/cl-root 32893632512 13080072192 18119061504 42% /
回答by SpicestMemeLord
Very similar answers to the ones shown, but if you don't know the name of filesystem and are just looking for the total available space on the partition.
与显示的答案非常相似,但如果您不知道文件系统的名称并且只是在寻找分区上的总可用空间。
##代码##