I am writing an installer in bash. The user will go to the target directory and runs the install script, so the first action should be to check that there is enough space. I know that df will report all file systems, but I was wondering if there was a way to get the free space just for the partition that the target directory is on.
Edit - the answer I came up with
df $PWD | awk '/[0-9]%/{print $(NF-2)}'
Slightly odd because df seems to format its output to fit the terminal, so with a long mount point name the output is shifted down a line
df
command : Report file system disk space usagedu
command : Estimate file space usageType
df -h
ordf -k
to list free disk space:OR
du
shows how much space one or more files or directories is using:The
-s
option summarizes the space a directory is using and-h
option provides Human-readable output.I think this should be a comment or an edit to ThinkingMedia's answer on this very question (Check free disk space for current partition in bash), but I am not allowed to comment (not enough rep) and my edit has been rejected (reason: "this should be a comment or an answer"). So please, powers of the SO universe, don't damn me for repeating and fixing someone else's "answer". But someone on the internet was wrong!™ and they wouldn't let me fix it.
The code
has a substantial flaw: Yes, it will output
50G
free as 50 -- but it will also output5.0M
free as 50 or3.4G
free as 34 or15K
free as 15.To create a script with the purpose of checking for a certain amount of free disk space you have to know the unit you're checking against. Remove it (as
sed
does in the example above) the numbers don't make sense anymore.If you actually want it to work, you will have to do something like:
Also for an installer to
df -k $INSTALL_TARGET_DIRECTORY
might make more sense thandf -k "$PWD"
. Finally, please note that the--output
flag is not available in every version of df / linux.