How do I get the size of a directory on the command line?
I tried to obtain the size of a directory (containing directories and sub directories) by using the ls command with option l . It seems to work for files ( ls -l file name ), but if I try to get the size of a directory (for instance, ls -l /home ), I get only 4096 bytes, although altogether it is much bigger.
16 Answers 16
Explanation
- du (disc usage) command estimates file_path space usage
The options -sh are (from man du ):
To check more than one directory and see the total, use du -sch :
Just use the du command:
will give you the cumulative disk usage of all non-hidden directories, files etc in the current directory in human-readable format.
You can use the df command to know the free space in the filesystem containing the directory:
du is your friend. If you just want to know the total size of a directory then jump into it and run:
If you also would like to know which sub-folders take up how much disk space?! You could extend this command to:
which will give you the size of all sub-folders (level 1). The output will be sorted (largest folder on top).
du can be complicated to use since you have to seemingly pass 100 arguments to get decent output. And figuring out the size of hidden folders is even tougher.
Make your life easy and use ncdu .
You get per folder summaries that are easily browsable.
Others have mentioned du , but I would also like to mention Ncdu — which is an ncurses version of du and provides interactivity: You can explore the directory hierarchy directly and see the sizes of subdirectories.
The du command shows the disk usage of the file.
The -h option shows results in human-readable form (e.g., 4k, 5M, 3G).
All of the above examples will tell you the size of the data on disk (i.e. the amount of disk space a particular file is using, which is usually larger than the actual file size). There are some situations where these will not give you an accurate report, if the data is not actually stored on this particular disk and only inode references exist.
In your example, you have used ls -l on a single file, which will have returned the file’s actual size, NOT its size on disk.
If you want to know the actual file sizes, add the -b option to du.
personally I think this is best, if you don’t want to use ncdu
This shows how much disk space you have left on the current drive and then tells you how much every file/directory takes up. e.g.,
Here is a function for your .bash_aliases
find all files under current directory recursively and sum up their size:
Note that du prints the space that a directory occupy on the media which is usually bigger than just the total size of all files in the directory, because du takes into account the size of all auxiliary information that is stored on the media to organize the directory in compliance with file system format.
If the file system is compressible, then du may output even smaller number than the total size of all files, because files may be internally compressed by the file system and so they take less space on the media than just uncompressed information they contain. Same if there are sparse files.
if there are hard links in the directory, then du may print smaller value as well because several different files in the directory refer the same data on the media.
To get the straightforward total size of all files in the directory, the following one-line shell expression can be used (assuming a GNU system):
It just sums sizes of all non-directory files in the directory (and its subdirectories recursively) one by one. Note that for symlinks, it reports the size of the symlink (not of the file the symlink points to).
How To Find The Size Of A Directory In Linux
This brief tutorial explains how to find the size of a directory in Linux operating systems. Finding size of files and directories in GUI mode is easy! All we have to do is just right click on the file or directory, and choose the properties option from the context menu. However, it is equally important to know how to find the size of a directory from CLI mode as well. Let me explain how to find directory size from command line.
Find the size of a directory in Linux
To find out the size of a directory, we will use ‘du’ command. du stands for disk usage.
The typical syntax of du command is given below:
Let us type the ‘du’ command in the Terminal and see what it displays.
sample output:
As you see above, du command displays the disk usage of the directories along with its sub-directories in the current directory.
To display a particular directory’s size, for example ostechnix, run:
Sample output:
We can also display the size in «human readable format» (i.e. auto-selecting the appropriate unit for each size), rather than the standard block size.
To do so, add -h tag with du command as shown below.
Sample output:
Now you see the size of the directories in Kilobytes, Megabytes and Gigabytes, which is very clear and easy to understand.
We can also display the disk usage size only in KB, or MB, or GB.
To do so, use -k for kilobytes, -m for megabytes
To find out which sub-directories consume how much disk size, use this command:
The largest sub-directories will be displayed on the top. You can increase the directory depth level by increasing the value of —max-depth parameter.
As you may noticed in the all above outputs, du command only displayed the disk usage of directories. But, what about the files? To display the disk usage of all items including files and directories, use -a flag.
Now, you will see the disk usage of all files and folders in human readable form.
Sample output:
We can also display the size of multiple directories at once as shown below.
If you want to check the total disk space used by a particular directory, use the -s flag.
Here, -s flag indicates summary.
Sample output:
Similarly, to display the total disk space used by multiple directories, for example ostechnix and /etc, run:
Sample output:
To display the grand total of directories, add -c flag with du -sh command.
Sample output:
To display only the grand total of the given directory including all the sub-directories, use ‘grep’ command with ‘du’ command like below.
You might want to exclude certain type of files. The following command will display the size of the current directory including its sub-directories, but it will exclude the size of all .mp4 files.
Can we find the biggest or smallest directories/files? Of course, yes! Check the following guide.
For more details about ‘du’ command, check the man pages.
3 Simple Ways to Get the Size of Directories in Linux
by Magesh Maruthamuthu · Last Updated: November 5, 2019
Two days ago I got a mail from our regular reader, who asked me how to get a summary of directories in Linux.
I know this can be achieved by the disk usage (Do) command, which I didn’t even find on my first attempt.
So I used the possible combination by digging into the Du Command Man page and finally got the best result.
Whenever a Linux administrator receives such a request, they can immediately assume that this is achieved using the du command (Disk Usage) and df (Disk FileSystem) command.
But alternatively, you can use the ncdu command or the tree command to achieve the same results.
By default the du command displays the size of the current directory files, which does not display the directory and its sub-directory size.
Read the article below to quickly summarize the size of each directory and their sub-directory.
Method-1: How to Get the Size of a Directory in Linux Using the Disk Usage (du) Command
The du command refers to disk usage. It is a standard Unix program that is used to estimate file space usage in the present working directory.
It recursively summarizes the disk usage to obtain a directory and its subdirectory size.
As I said at the beginning of the article, we are going to use the Disk Usage (Do) command with some options to achieve this. So use the following disk usage command combination to get the summary of folders and their sub-folders.
Use the below du command format to get the total size of each directory, including sub-directories.
The above command will print the size of each file and the actual size of each directory, including their subdirectory and total size.
Details of the above command:
- du: It’s a command
- -h: Print sizes in human readable format (e.g., 1K, 234M, 2G)
- -c: Produce a grand total
- /home/daygeek/Documents/: The path of directory
- sort -rh: Sort the results with numerical value
- head -20: Output the first 20 lines result
Use the below du command format to get the total size of a given directory.
If you want to get the size of the first-level sub-directories, including their sub-directories, for a given directory on Linux, use the du command format below.
Method-2: How to Get the Size of a Directory in Linux Using the ncdu (NCurses Disk Usage) Command
The ncdu (NCurses Disk Usage) is a curses-based version of the well-known ‘du’, and provides a fast way to see what directories are using your disk space.
The ncdu command scans the given directory and displays the files and folder size recursively.
Method-3: How to Get the Size of a Directory in Linux Using the tree Command
The tree command is a recursive directory listing program that produces a depth indented listing of files and directories in a tree-like format.
The two commands above show directory summary, but the tree command will tell you every file size inside the directory and their subdirectory, and print the summary of the directory.