eSecureData Inc.

×
×

List and Check File/Folder Size in Linux

Back

Problem
Find out disk size of files and folders in Linux.

tl;dr
List all files (with size), and folders in current directory:

ls -lh

List only folders and show their total size in current directory:

du -h --max-depth=1

Solution

There are several ways to get the required information.

Current directory

If you want to list all files in a directory, along with their sizes, you can use this command:

ls -lh

Unfortunately, it also lists folders in alphabetical order along with the rest of the files, so you might not be able to tell straight away if something is a file or a folder. However, you can add --group-directories-first to show folders listed first (like the default on Windows).

ls -lh --group-directories-first

Example output:

drwxr-xr-x 4 root root 4.0K Nov 30 08:18
drwxr-xr-x 5 root root 4.0K Nov 30 02:29 folder1
drwxr-xr-x 18 1000 1000 4.0K Dec 18 03:46 folder2
-rw-------. 1 root root 1.4K Oct 12 06:28 configfile.cfg
-rwxr-xr-x 1 root root 15K Dec 18 04:55 howtodeleteavirus.txt
-rwxr-xr-x 1 root root 496 Dec 21 07:13 program.py
-rwxr-xr-x 1 root root 222 Dec 18 04:59 program2.py
-rwxr-xr-x 1 root root 874 Dec 18 04:55 totallynotavirus.py

Please note that it does not show the total size of a folder and its contents! ls command is useless that way.

Folders only

What if, instead, you need to see the total size of folders?. For example, one of my production servers recently started glitching. Reason? It ran out of physical space. This begged the question - what took up the most space? To find out, I went to the root directory (/), and ran this command:

du -h --max-depth=1

It shows total folder size of all folders in the current directory. Example showing my user directory:

# du -h --max-depth=1
8.0K ./.pki
118M ./folder1
292K ./folder2
26M ./stuff
512K ./
155M .

This tells me that I have 118 MB in folder1, 292 KB in folder2, 26 MB in folder named stuff, and 155 MB in total in the current folder.

You can also change the argument --max-depth=1 to show the size of subfolders as well. Example:

# du -h --max-depth=2
26M ./stuff
924K ./stuff/derp
2.3M ./stuff/funny
21M ./stuff/build
1.5M ./stuff/ihavenolife
12K ./stuff/coolteapots

Was this article helpful?
Dislike0 Like2
Views: 14534