Last updated on March 11th, 2021 at 09:33 am
Sometimes you may need to check disk usage or get disk space in Linux / Unix to find which file is using more space in Linux. Here’s how to get disk space in Linux. You can use it to check disk space usage in Ubuntu, CentOS, Redhat, Debian, Fedora and other systems.
How to Get Disk Space in Linux / Unix
You can check disk space in Linux / Unix using du (disk usage) or df (disk free) commands. Both support various options to find disk usage and show disk usage in different ways.
Here’s the syntax of du and df commands
du [options] [locations/devices]
df [options] [locations/devices]
We will look at different ways to use du and df commands
Bonus Read : How to Install Zip and Unzip in Linux
How to Check Disk Space from terminal
Here’s how to check disk usage from terminal.
$ du
The above command will list space occupied by all files & folders in your present working directory (pwd). Since there can be many files & directories in a file system, we pass the output of du command to head command and select only the first 10 results. You can change it as per your requirements
$ du | head -n 10
Here’s a sample output
ubuntu@ip-172-30-0-194:~$ du | head -n 10 4 ./.qws/share/data/Ofi Labs/PhantomJS 8 ./.qws/share/data/Ofi Labs 12 ./.qws/share/data 16 ./.qws/share 20 ./.qws 4 ./.gnupg/private-keys-v1.d 8 ./.gnupg 52 ./conf/.git/hooks 8 ./conf/.git/info 4 ./conf/.git/branches
If you don’t want Linux/Unix to display disk usage of subfolders and files in folders, then you can use max-depth option. The following command will give disk usage of only files and folders located at your current directory. It will not recursively calculate disk usage of all subfolders and files in folders.
$ du --max-depth=1
Here’s sample output
ubuntu@ip-172-30-0-194:~$ du --max-depth=1 20 ./.qws 8 ./.gnupg 44704 ./conf 125548 ./fedingo 9592 ./.cache 8 ./.vim 8 ./.ssh 49176 ./.local 259896 .
The above command lists all files & folders in your present working directory along with their size in bytes.
You can get more details about du command using
$ man du
How to get disk usage for specific folder / directory
Here’s the linux command to get disk usage for specific directory / folder
$ du /path/to/dir
Here’s an example of du command for specific folder (e.g /etc/)
$ du /etc/
How to get disk usage in GB and MB
You can use the following command to get disk usage in human readable format (Mb and Gb)
$ du -h
ubuntu@ip-172-30-0-194:~$ du -h --max-depth=1 20K ./.qws 8.0K ./.gnupg 44M ./conf 123M ./fedingo 9.4M ./.cache 8.0K ./.vim 8.0K ./.ssh 49M ./.local 255M .
How to get disk usage by folder / directory
You can get disk space in linux for all files and directories using -a option. We will use its output to get disk usage by directory / folder.
Here’s the command to get top 5 biggest subfolders on your disk
$ sudo du -a | sort -n -r | head -n 5
Here’s the command to get top 5 biggest subfolders in a specific folder/directory
$ sudo du -a /path/to/dir | sort -n -r | head -n 5
We will use the above command to get top 5 biggest folders in /etc folder
$ sudo du -a /etc/ | sort -n -r | head -n 5 7432 /etc/ 1076 /etc/apparmor.d 684 /etc/apache2 640 /etc/ssl 616 /etc/ssl/certs
In the above command we get the disk usage of all files and folders at /etc using du -a command. We sort this output using sort command in descending order of bytes. Finally, we select only top 5 records.
Similarly, you can use df command to get disk space in linux for a user across various disks where the user has read access.
Here’s the command to check disk space using df command, and get disk usage in human readable format (Mb and Gb)
$ df -h Filesystem Size Used Avail Use% Mounted on udev 476M 0 476M 0% /dev tmpfs 98M 784K 98M 1% /run /dev/xvda1 7.7G 4.7G 3.1G 61% / tmpfs 490M 0 490M 0% /dev/shm tmpfs 5.0M 0 5.0M 0% /run/lock tmpfs 490M 0 490M 0% /sys/fs/cgroup /dev/loop0 18M 18M 0 100% /snap/amazon-ssm-agent/1566 /dev/loop3 29M 29M 0 100% /snap/amazon-ssm-agent/2012 /dev/loop4 97M 97M 0 100% /snap/core/9665 /dev/loop2 97M 97M 0 100% /snap/core/9804 tmpfs 98M 0 98M 0% /run/user/1000
The above command lists all file systems the user has access to, one per row. The first row of output contains column names such as filesystem, total disk space, disk space used, disk space available, percentage disk space used, and location of mount. You can use it to get disk size of various file systems you have access to.
Hopefully, the above commands will help you get disk space in linux for any folder / directory in human readable format.
Ubiq makes it easy to visualize data in minutes, and monitor in real-time dashboards. Try it Today!
Sreeram Sreenivasan is the Founder of Ubiq. He has helped many Fortune 500 companies in the areas of BI & software development.