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
you get size in bytes this way.
A complete example for someone who may want to use this to monitor a mount point on a server. This example will check if /var/spool is under 5G and email the person :
Yes:
for the current directory.
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:To know the usage of the specific directory in GB's or TB's in linux the command is,
df -h /dir/inner_dir/
df -sh /dir/inner_dir/
and to know the usage of the specific directory in bits in linux the command is,
df-k /dir/inner_dir/
To get the current free disk space in human readable format with letters. The following will output the free space for the current partition, drop the header and strip letters.
Outputs
50G
free as50
.To make this part of a free disk space condition in bash script.
Type in the command shell:
or
or
It will show the list of free disk spaces for each mount point.
You can show/view single column also.
Type:
Note: Here 3 is the column number. You can choose which column you need.