linuxbash

Check free disk space for current partition in Bash


I am writing an installer in Bash. The user will go to the target directory and run 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


Solution

  • Yes:

    df -k .
    

    for the current directory.

    df -k /some/dir
    

    if you want to check a specific directory.

    You might also want to check out the stat(1) command if your system has it. You can specify output formats to make it easier for your script to parse. Here's a little example:

    $ echo $(($(stat -f --format="%a*%S" .)))