Check free disk space for current partition in bash

check hard disk space ubuntu command line
how to check total disk space in linux
linux check disk space in gb
shell script to check available disk space
ubuntu check disk space terminal
how to check which file is using more space in linux
linux total disk size
linux check disk space subdirectory

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 -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" .)))

Check free disk space for current partition in bash, 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)  You can get this information in a graphical view using the Disks (gnome-disk-utility) in the GNOME desktop. Launch it to see all disks detected by your computer, and click a partition to see details about it, including space used and space remaining.

  1. df command : Report file system disk space usage
  2. du command : Estimate file space usage

Type df -h or df -k to list free disk space:

 $ df -h


 $ df -k

du shows how much space one ore more files or directories is using.

 $ du -sh

-s option summarize the space a directory is using and -h option provides Human-readable output.

How to check free disk space in Linux, Launch it to see all disks detected by your computer, and click a partition to see details about it, including space used and space remaining. This tutorial discusses the ways to check disk space on Linux using the command line or graphical tools. As a system administrator, you always want to make sure that you have enough space for your hosts to run. Luckily for you, there are plenty of commands that you can use in order to check disk space on Linux.

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

  df --output=avail -h "$PWD" | sed '1d;s/[^0-9]//g'

has a substantial flaw: Yes, it will output 50G free as 50 -- but it will also output 5.0M free as 50 or 3.4G free as 34 or 15K 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:

FREE=`df -k --output=avail "$PWD" | tail -n1`   # df -k not df -h
if [[ $FREE -lt 10485760 ]]; then               # 10G = 10*1024*1024k
     # less than 10GBs free!

Also for an installer to df -k $INSTALL_TARGET_DIRECTORY might make more sense than df -k "$PWD". Finally, please note that the --output flag is not available in every version of df / linux.

Get the free space available in current directory in Bash, The output can be made a bit easier to parse by using the -P option which will ensure that: The information about each file system is always printed on exactly  With "free space" you mean "not yet allocated to a partition" aka "unpartitioned space", do you? The v command of fdisk verifies the partition table and reports such space as well, e.g. Remaining 239 unallocated 512-byte sectors in my case.

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 :

  # -----------------------------------------------------------------------------------------
  # SUMMARY: Check if MOUNT is under certain quota, mail us if this is the case
  # DETAILS: If under 5G we have it alert us via email. blah blah  
  # -----------------------------------------------------------------------------------------
  # CRON: 0 0,4,8,12,16 * * * /var/www/httpd-config/server_scripts/clear_root_spool_log.bash

  MOUNTP=/var/spool  # mount drive to check
  LIMITSIZE=5485760 # 5G = 10*1024*1024k   # limit size in GB   (FLOOR QUOTA)
  FREE=$(df -k --output=avail "$MOUNTP" | tail -n1) # df -k not df -h
  LOG=/tmp/log-$(basename ${0}).log
  MAILMESSAGE=/tmp/tmp-$(basename ${0})

  # -----------------------------------------------------------------------------------------

  function email_on_failure(){

          echo "" >$MAILMESSAGE
          echo "Hostname: $(hostname)" >>$MAILMESSAGE
          echo "Date & Time: $(date)" >>$MAILMESSAGE

          # Email letter formation here:
          echo -e "\n[ $(date +%Y%m%d_%H%M%S%Z) ] Current Status:\n\n" >>$MAILMESSAGE
          cat $sMess >>$MAILMESSAGE

          echo "" >>$MAILMESSAGE
          echo "*** This email generated by $(basename $0) shell script ***" >>$MAILMESSAGE
          echo "*** Please don't reply this email, this is just notification email ***" >>$MAILMESSAGE

          # sending email (need to have an email client set up or sendmail)
          $MAILCMD -s "Urgent MAIL Alert For $(hostname) AWS Server" "$EMAILIDS" < $MAILMESSAGE

          [[ -f $MAILMESSAGE ]] && rm -f $MAILMESSAGE


  # -----------------------------------------------------------------------------------------

  if [[ $FREE -lt $LIMITSIZE ]]; then
          echo "Writing to $LOG"
          echo "MAIL ERROR: Less than $((($FREE/1000))) MB free (QUOTA) on $MOUNTP!" | tee ${LOG}
          echo -e "\nPotential Files To Delete:" | tee -a ${LOG}
          find $MOUNTP -xdev -type f -size +500M -exec du -sh {} ';' | sort -rh | head -n20 | tee -a ${LOG}
          email_on_failure ${LOG}
          echo "Currently $(((($FREE-$LIMITSIZE)/1000))) MB of QUOTA available of on $MOUNTP. "

Linux 101: Check Disk Space Command, Linux command to check disk space. df command – Shows the amount of disk space used and available on Linux file systems. du command  Why is space not being freed from disk after deleting a file in Linux? Even though the file was successfully deleted, the disk space was not freed. How to free disk space? My case is when a user uploads huge files to the web server and there are a lot of temporary files (I configured in the /tmp dire

df --output=avail -B 1 "$PWD" |tail -n 1

you get size in bytes this way.

Linux Check Disk Space Command To View System Disk Usage , Explains Linux check disk space command that report file system df command – Shows the amount of disk space used and available on For example, the following command provides information only for the partition /dev/sda: For example, to see the size of each png file in the current directory, enter: 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 as 50. To make this part of a free disk space condition in bash script.

Linux / Unix - Checking Free Disk Space, Explains how to checking free disk space and used disk space in Linux or Unix using the df, du, and other command line options. df command examples – to check free disk space. Type df -h or df -k to list free disk space: Outputs: The df utility displays statistics about the amount of free disk space on the specified file system or on the file system of which file is a part. Values are displayed in 512-byte per block counts.

Finding Unallocated and Free Disk Space in Linux, The partitions section always displays the current partition df Command Examples to Check Disk Space in Linux 

Df Command in Linux (Check Disk Space), On Linux based systems, you can use the df command to get a detailed report Is there enough free disk space to download a large file or install a new application? Here is an example showing how to list all ext4 partitions:

  • Use the -P flag to df, you'll get it all on one line.
  • related du -hs . to see the disk space in use for current directory.
  • As @Mat said, you will want to use -P if you go with your solution. In the past I've encountered oddly formatted or long named partitions that caused a script to fail because the -P flag was missing.
  • Thanks for that! stat looks a bit mind-blowing to me. Reading the man page suggests I don't want -k on my version of df, but putting the path in did help.
  • Ahh - now I see why you use -k, makes the calculations easier!
  • The thing is a lot of systems will default to 512 byte blocks if you don't specify -k. (coreutils df defaults to 1k blocks, so you're pretty safe on Linux though - but if you have coreutils, you have stat, and that's even safer - no parsing required).
  • Now I understand what stat is doing, I do like it more.
  • In my Red-Hat something, the format string should be %a*%s instead of %a*%S. I'm not sure if this is a difference or a typo.
  • "but I am not allowed to comment (not enough rep)" I added that comment.
  • thanks @Mateusz, ... I really should have done that after this post earned me so much rep :-)
  • Not all distros have --output=avail
  • Good observation. That's precisely what the last sentence of the answer states. @YzmirRamirez ;-)
  • Not all distros have --output=avail
  • Please quote $PWD or this will fail if current directory contains spaces.