How to get the current free disk space in Bash?

时光毁灭记忆、已成空白 提交于 2019-12-04 13:30:06

First, the available disk space depends on the partition/filesystem you are working on. The following command will print the available disk space in the current folder:

TARGET_PATH="."
df -h "$TARGET_PATH"  | awk 'NR==2{print $4}'

TARGET_PATH is the folder you are about writing to. df automatically detects the filesystem the folder belongs to.

This was the only portable way (Linux and Mac OS) in which I was able to get the amount of free disk space:

df -Pk . | sed 1d | grep -v used | awk '{ print $4 "\t" }'

Be aware that df from Linux is different than the one from Mac OS (OS X) and they share only a limited amount of options.

This returns the amount of free disk space in kilobytes. Don't try to use a different measure because they options are not portable.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!