- How to find the size of a directory in Linux
- Method-1: Get the size of a directory in Linux with du command
- Method-2: Find the size of a directory in Linux with ncdu command
- Method-3: Check a directory size in Linux with tree command
- Conclusion
- How To Find The Size Of A Directory In Linux
- Find the size of a directory in Linux
- Conclusion
- How to Get the Size of a Directory in Linux
- Option 1: Display the Size of a Directory Using the du Command
- Option 2: Get Size of Directory in Linux Using tree Command
- Option 3: Find the Size of a Linux Directory Using ncdu Command
How to find the size of a directory in Linux
It is very easy to check the size of directories and files in Linux using the GUI, but it is not always easy to get the size of a directory using the command line.
The ls command can be used to list the contents of a directory, but it does not display the exact directory size and always shows each directory size as 4096 bytes (4 KB), which is the size of space on the disk that is used to store the meta-information for the directory, not what it contains
You can get the actual size of a directory using the du command (Disk Usage), which is widely used by Linux administrators, but you can explore other commands for this purpose. Let’s explore them.
This guide shows you how to find the directory size in Linux using the below three commands:
- du command
- ncdu command
- tree command
Method-1: Get the size of a directory in Linux with du command
The du command refers to disk usage. It is a standard Unix program that is used to estimate disk space usage in the present working directory when no path is specified.
It recursively summarizes the disk usage to obtain a directory and its sub-directory sizes.
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 sub-directory as well as the total size.
Details:
- 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 following du command format to get the total size of a specific directory:
If you want to find out the size of the first-level sub-directories, including their sub-directories, for a given directory on Linux, use the following du command format:
Method-2: Find the size of a directory in Linux with ncdu command
The ncdu (NCurses Disk Usage) is a curses-based version of the well-known ‘du’ command, and provides a fast way to see which directories are consuming your disk space.
The ncdu command scans the given directory and displays their files and folder sizes recursively as shown below:
Method-3: Check a directory size in Linux with tree command
The pstree command displays directory contents recursively in a tree-like format, which is very convenient way to display the directory hierarchy and that improves the readability of the output.
As you can see, the tree command output is straight forward compared to the du & ncdu commands.
Conclusion
You have learnt the three different commands to find the size of a directory in Linux.
If you have questions, feel free to leave a comment below, and we will get back to you as soon as we can. Happy learning!
Источник
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 the size of files and directories in graphical mode is very 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 check the size of a directory from CLI mode as well.
Find the size of a directory in Linux
We can get the directory size using ‘du’ command in Linux and Unix-like operating systems. The du command will estimate and summarize file and directory space usage.
For those wondering, du stands for disk usage.
The typical syntax of du command is given below:
Now, allow me to show you how to find directory size in Linux from command line using du command with examples.
1. Display current directory size
Enter ‘du’ command without any options to display the size of the current directory and its sub-directories.
sample output:
Find the size of current directory in Linux
As you see in the above output, du command displays the disk usage of my current directory along with its sub-directories.
2. Display size of a specific directory
To display a particular directory’s size, for example ostechnix, run:
Sample output:
You can also display the size of multiple directories in one go like below:
3. Display directory size in human-readable format
By default, du displays the size in bytes . 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.
4. Display directory size in a specific format
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
5. Display grand total size of directories
We can display just the total human-readable size of the current working directory using -s and -h flags.
Here, -s flag indicates summary.
Sample output:
If you want to check the total disk space used by a particular directory, run:
We can also display the size of multiple directories at once as shown below.
To get the grand total of the combined directories in human-readable format, for example
/Music , add -c flag:
Here, -c refers the cumulative total.
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.
6. Display sizes of a directory and subdirectories, up to N levels deep
A directory may contain large number of sub-directories. You may want to list the sizes of a directory and any subdirectories, only up to N levels deep.
The following command displays the human-readable sizes of the given directory and its sub-directories, up to 2 level deep:
7. Sort directories based on size
To find out which sub-directories consume how much disk size and sort them by their 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.
8. Find the size of both files and directories
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 format.
Sample output:
Display disk usage of files and folders in human readable format in Linux
9. Exclude certian 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.
Tip: 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.
Suggested Read:
Conclusion
In this guide, we looked at how to find the total size of directory in Linux using du command with examples. As you can see, getting folder or directory size in Linux is not a big deal.
Источник
How to Get the Size of a Directory in Linux
Home » SysAdmin » How to Get the Size of a Directory in Linux
Many users run Linux from the command line. However, the command line — sometimes known as the terminal — doesn’t have an intuitive interface for checking disk space in Linux.
This guide shows you how to find the size of a specific directory in Linux from the command line.
- A system running Linux
- A command line / terminal window (available by clicking Search, then typing terminal)
- A user account with sudo or root privileges
Note: In Linux, a directory is the equivalent of a folder in Windows. A directory may have directories inside (called subdirectories), or it may only contain files.
Option 1: Display the Size of a Directory Using the du Command
The du command stands for disk usage. This command is included by default in most Linux distributions.
You can display the size of your current directory by typing du in the command line:
The system should display a list of the contents of your home directory, with a number to the left. That number is the size of the object in kilobytes.
You can add the -h option to make the output more readable:
Each entry will start with a number and a letter. The number is the amount of space used, and the letter (usually K, M, or G) indicates Kilobytes, Megabytes, or Gigabytes. For example:
To find the size of a specific directory different from your current working directory. The du command allows you to specify a directory to examine:
This displays the size of the contents of the /var directory. You may see some entries with an error, as in the image below.
This happens when your user account does not have permission to access a particular directory. Use the sudo or su command to get access privileges:
Note: Some versions of Linux don’t enable sudo by default. You can use the su command to switch to the root user account instead.
To display total disk usage of a particular directory, use the -c command:
Options can be combined. If you wanted to repeat the previous command in human-readable format, enter the following:
You can limit the scan to a certain level of subdirectory by using the max-depth option. For example, to scan only the size of the top directory, use —max-depth=0 :
If you wanted to list only the top directory and the first layer of subdirectories, change —max-depth=1 :
If you run into trouble or want to explore more options for the du command, enter the following command to display the help file:
Option 2: Get Size of Directory in Linux Using tree Command
By default, the tree command is not included in some versions of Linux. To install it, enter the following:
- For Debian / Ubuntu
- For CentOS / RedHat
The tree command displays a visual representation of your directories. It uses lines to indicate which subdirectories belong where, and it uses colors to indicate directories and files.
tree can also be used with options. To display a human-readable size of the current directory’s subdirectories, enter the following:
Like the du command, tree can target a specific directory:
This command takes a few moments since the /var directory has many entries.
The tree command also has a help file, which you can access by entering:
Option 3: Find the Size of a Linux Directory Using ncdu Command
The ncdu tool stands for NCurses Disk Usage. Like the tree command, it is not installed by default on some versions of Linux. To install it, enter the following:
- For Debian / Ubuntu
- For CentOS / RedHat
The ncdu utility is an interactive display of your disk usage. For example, enter the following:
In the upper left corner, it displays the current directory being scanned. A column on the left displays the numerical size, a graph of #- signs to indicate the relative size, and the file or directory.
Use the up and down arrows to select different lines. The right arrow will browse into a directory, and the left arrow will take you back.
ncdu can be used to target a specific directory, for example:
For help, press the ? key inside the ncdu interface. To quit, press the letter q .
Note: Learn how to move directories in Linux using the GUI or system commands.
You now have three different options to find the size of a directory in Linux operating systems.
If you want to learn more about directories in Linux, read our article how to rename directories in Linux.
Источник