使用bash在带有空格的文件夹中获取挂载设备上的可用空间 [英] Get available space on mounted device with folder with whitespaces using bash
问题描述
我尝试在挂载的磁盘上获得可用空间:
I try to get Available space on mounted disk:
df /tmp/mount/0dfShksftN | tail -1 | awk '{print $4}'
它可以正常工作,但在Filesystem参数的文件夹名称带有空格的情况下不能正常工作.
It works fine but not in cases when Filesystem param has folder name with spaces.
在这种情况下,我找到了获取文件系统和挂载点值的解决方案:
I found solution for getting Filesystem and Mount point values in this case:
df -P "/mnt/MOUNT WITH SPACES/path/to/file/filename.txt" | awk 'BEGIN {FS="[ ]*[0-9]+%?[ ]+"}; NR==2 {print $NF}'
但是找不到可用"字段值的解决方案.我可以自己提取整个字符串并进行解析,但是也许有一种使用bash进行此操作的方法,
But can't find solution for Available field value. I could take the entire string and parse by myself but maybe there is a way to do this using bash,
推荐答案
您可以使用 df
命令的-output
过滤器.
You can use --output
filter of df
command.
df "<file-system>" --output=avail
Avail
868215420
对于您的原始方法,您可能需要考虑向后计数列.
For your original approach, you may need to consider counting column backward.
在 df(1)
的手册页中:
FIELD_LIST是要包括的列的逗号分隔列表.有效的字段名称是:源","fstype",部分",已使用",'iavail','ipcent',大小",使用",可用",百分比",文件"和目标"(请参阅信息页).
FIELD_LIST is a comma-separated list of columns to be included. Valid field names are: 'source', 'fstype', 'itotal', 'iused', 'iavail', 'ipcent', 'size', 'used', 'avail', 'pcent', 'file' and 'target' (see info page).
这篇关于使用bash在带有空格的文件夹中获取挂载设备上的可用空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!