- Check free disk space for current partition in bash
- 8 Answers 8
- 5 Linux commands to check free disk space
- Linux df command
- Linux du command
- Linux ls -al command
- Linux stat command
- Linux fdisk -l command
- Check used disk space on Linux with du
- Use df to check free disk space on Linux
- How to Check Disk Space in Linux
- Check Linux Disk Space Using df Command
- Display Usage in Megabytes and Gigabytes
- Understanding the Output Format
- Display a Specific File System
- Display File Systems by Type
- Display Size in 1000 Instead of 1024
- Check Disk Space in Linux With du Command
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 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
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
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.
8 Answers 8
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:
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.
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).
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.
@RnMss: It’s supposed to be %S (capital) according to the man page on RedHat. Can’t find a filesystem where %s != %S on the few RedHats I have access too though, so can’t check 🙁
- df command : Report file system disk space usage
- du command : Estimate file space usage
Type df -h or df -k to list free disk space:
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.
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! fi;
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.
df --output=avail -B 1 "$PWD" |tail -n 1
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 :
#!/bin/bash # ----------------------------------------------------------------------------------------- # 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 $).log MAILCMD=mail EMAILIDS="dude@wheres.mycar" MAILMESSAGE=/tmp/tmp-$(basename $) # ----------------------------------------------------------------------------------------- function email_on_failure()< sMess="$1" 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 $ echo -e "\nPotential Files To Delete:" | tee -a $ find $MOUNTP -xdev -type f -size +500M -exec du -sh <> ';' | sort -rh | head -n20 | tee -a $ email_on_failure $ else echo "Currently $(((($FREE-$LIMITSIZE)/1000))) MB of QUOTA available of on $MOUNTP. " fi
5 Linux commands to check free disk space
Keeping track of disk utilization information is on system administrators’ (and others’) daily to-do list. Linux has a few built-in utilities that help provide that information.
Linux df command
The df command stands for «disk-free,» and shows available and used disk space on the Linux system.
df -h shows disk space in human-readable format
df -a shows the file system’s complete disk usage even if the Available field is 0
df -T shows the disk usage along with each block’s filesystem type (e.g., xfs, ext2, ext3, btrfs, etc.)
df -i shows used and free inodes
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.
Linux du command
du shows the disk usage of files, folders, etc. in the default kilobyte size
du -h shows disk usage in human-readable format for all directories and subdirectories
du -a shows disk usage for all files
du -s provides total disk space used by a particular file or directory
The following commands will check your total space and your utilized space.
This information can be represented visually in GNOME with the Disk Usage application, or with Filelight in the KDE Plasma desktop. In both applications, disk usage is mapped to concentric circles, with the middle being the base folder (usually your /home directory, but it’s up to you) with each outer ring representing one directory level deeper. Roll your mouse over any segment for detailed information about what’s taking up space.
Linux ls -al command
ls -al lists the entire contents, along with their size, of a particular directory
Linux stat command
stat displays the size and other stats of a file/directory or a filesystem.
Linux fdisk -l command
fdisk -l shows disk size along with disk partitioning information
These are most of the built-in utilities for checking file space in Linux. There are many similar tools, like Disks (GUI), Ncdu, etc., that also show disk space utilization. Do you have a favorite tool that’s not on this list? Please share in the comments.
This article was originally published in July 2018 and has been updated to include additional information.
Check used disk space on Linux with du
Find out how much disk space you’re using with the Linux du command.
Use df to check free disk space on Linux
Find out how much Linux disk space you have left with the df command.
How to Check Disk Space in Linux
Managing disk space on a Linux server is an important task. For example, package manager applications notify you how much disk space will be required for an installation. For that information to be meaningful, you should know how much space your system has available.
In this tutorial, learn how to use the df command to check disk space in Linux and the du command to display file system disk space usage.
- A Linux-based system
- A terminal window / command line
- A user account with sudo or root privileges
Check Linux Disk Space Using df Command
You can check your disk space simply by opening a terminal window and entering the following:
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.
Display Usage in Megabytes and Gigabytes
You can display disk usage in a more human-readable format by adding the -h option:
This displays the size in kilobytes (K), megabytes (M), and gigabytes (G).
Understanding the Output Format
The df command lists several columns:
Filesystem Size Used Avail Use% Mounted on udev 210M 0 210M 0% /dev tmpfs 49M 1004K 48M 3% /run /dev/sda2 7.9G 4.3G 3.2G 58% /
Your output may have more entries. The columns should be self-explanatory:
- Filesystem – This is the name of each particular drive. This includes physical hard drives, logical (partitioned) drives, and virtual or temporary drives.
- Size– The size of the filesystem.
- Used – Amount of space used on each filesystem.
- Avail – The amount of unused (free) space on the filesystem.
- Use% – Shows the percent of the disk used.
- Mounted on – This is the directory where the file system is located. This is also sometimes called a mount point.
The list of filesystems includes your physical hard drive, as well as virtual hard drives:
- /dev/sda2 – This is your physical hard drive. It may be listed as /sda1, /sda0, or you may even have more than one. /dev stands for device.
- udev– This is a virtual directory for the /dev directory. This is part of the Linux operating system.
- tmpfs – You may have several of these. These are used by /run and other Linux processes as temporary filesystems for running the operating system. For example, the tmpfs /run/lock is used to create lockfiles. These are the files that prevent multiple users from changing the same file at the same time.
Display a Specific File System
The df command can be used to display a specific file system:
You can also use a backslash:
This displays the usage on your primary hard drive. Use the mount point (in the Mounted on column) to specify the drive you want to check.
Note: The df command only targets a full filesystem. Even if you specify an individual directory, df will read the space of the whole drive.
Display File Systems by Type
To list all file systems by type, use the command:
This lists drives with the ext4 type, in human-readable format.
Display Size in 1000 Instead of 1024
You can display disk usage in units of 1000 instead of 1024:
This can address a point of confusion in storage technology. Hard drive manufacturers sell hard drives in sizes based on 1000 bytes = 1 kilobyte.
However, operating systems divide that space up so that 1024 bytes = 1 kilobyte. Because of this, a 1000-gigabyte hard drive ends up with roughly 930 gigabytes of usable storage.
Note: Check out our overview of the Linux free command used to check total, used, shared, free, and available memory and swap space.
Check Disk Space in Linux With du Command
The du command displays disk usage. This tool can display disk usage for individual directories in Linux, giving you a finer-grained view of your disk usage. Use it to display the amount of space used by your current directory:
Like the df command, you can make du human-readable:
It displays a list of the contents of the current directory, and how much space they’re using. You can simplify the display with the -s option:
This shows how much space the current directory uses.
To specify the directory or file, check use the following options:
With the second command, you may have noticed a permission denied error message. This means the current user doesn’t have the privileges to access certain directories. Use the sudo command to elevate your privileges:
Note: If you’re working on CentOS Linux, you may need to use the su command to switch to the root user to access protected directories.
You should now understand how to use df and du commands to check disk space on your Linux system. Remember, to display a complete list of options, use either df —help or du —help .
Check out our article on how to use fsck command to run a filesystem check as preventive maintenance or when there is an issue with your system.