Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get free disk space with df to just display free space in kb?

I'm trying to output the amount of free disk space on the filesystem /example.

If I run the command df -k /example I can get good information about available disk space in kb but only by being human and actually looking at it.

I need to take this data and use it somewhere else in my shell script. I initially thought about using cut but then my script wont be portable to other disks as free disk space will vary and cut will not produce accurate results.

How can I get output of just the free disk-space of example in kb?

like image 526
Whoppa Avatar asked Oct 31 '13 10:10

Whoppa


People also ask

Which command displays the amount of free disk space df?

Use the df command to show the amount of free disk space on each mounted disk.

What command would you use to display in kilobytes the total amount of free disk space on your local drive?

The df command stands for disk free, and it shows you the amount of space taken up by different drives. By default, df displays values in 1-kilobyte blocks.

Which commands display information about the disk space used by each file?

The “df” command displays the information of device name, total blocks, total disk space, used disk space, available disk space, and mount points on a file system.

What is df H command?

df command examples To display all filesystems and their disk usage, use: df. To show all filesystems and their disk usage in human-readable form, use: df -h. To show the filesystem and its disk usage containing the given file or directory, use: df /path/to/directory_or_file.


2 Answers

To get the output of df to display the data in kb you just need to use the -k flag:

df -k 

Also, if you specify a filesystem to df, you will get the values for that specific, instead of all of them:

df -k /example 

Regarding the body of your question: you want to extract the amount of free disk space on a given filesystem. This will require some processing.

Given a normal df -k output:

$ df -k /tmp Filesystem     1K-blocks    Used Available Use% Mounted on /dev/sda1        7223800 4270396   2586456  63% / 

You can get the Available (4th column) for example with awk or cut (previously piping to tr to squeeze-repeats (-s) for spaces):

$ df -k /tmp | tail -1 | awk '{print $4}' 2586456 $ df -k /tmp | tail -1 | tr -s ' ' | cut -d' ' -f4 2586456 

As always, if you want to store the result in a variable, use the var=$(command) syntax like this:

$ myUsed=$(df -k /tmp | tail -1 | awk '{print $4}') $ echo "$myUsed" 2586456 

Also, from the comment by Tim Bunce you can handle long filesystem names using --direct to get a - instead, so that it does not print a line that breaks the engine:

$ df -k --direct /tmp Filesystem     1K-blocks    Used Available Use% Mounted on -                7223800 4270396   2586456  63% / 
like image 119
fedorqui 'SO stop harming' Avatar answered Oct 04 '22 10:10

fedorqui 'SO stop harming'


You can use stat(2) command to display free blocks and also to find out how large each block is, e.g.

stat -f --printf="%a %s\n" / 

will display number of free blocks (%a) on a given file system (/) followed by a block size (%s). To get size in kB, you can use bc(1) command as in the following example:

stat -f --printf="%a * %s / 1024\n" / | bc 

Finally, to put this into a variable is just a matter of using backtick substitution (or $() as in the first answer):

SOMEVAR=`stat -f --printf="%a * %s / 1024\n" / | bc` 
like image 23
sgros Avatar answered Oct 04 '22 12:10

sgros