For this, you will need a Linux system, a command-line interface for running the command, and the right user access for checking the space on the Linux system.
We will be looking at various ways to check the file or folder size available on the Linux system.
Using du Command
The du in the “du” command specifies the disk usage, which is available in all Linux distributions by default. Run the below command for checking the disk usage for your Linux system.
You will get the output displaying your home directory contents and some numbers on the left showing the object’s size in KB.
If you want the output in a more readable format, use the “-h” option and the “du” command, as shown below.
In the above output, you will see some numbers ending with characters. The number specifies the amount of space used, and the character will specify the (usually K, G, or M) kilobyte, gigabyte, or megabyte.
If you want to check the size of the directory that is different from the current directory, use the following format for the “du” command.
The above command will help in displaying the size of the /var directory contents. You will notice an error in the below output as the user does not have the right permission for accessing that file. If you want the right privileges, use the sudo or su command as mentioned below.
If you want to display the total disk usage for a particular directory, use the “-c” command as shown below.
To get the output in a human-readable format, combine the “-c” option and the “-h” option, as shown below.
Well, you can even put the limit for displaying a certain level of the subdirectory with the help of the “max-depth” option. Consider the below example for displaying only the top level of the directory.
But if you want to specify the details of the top directory level along with the first layer of the subdirectory, run the below command with the max_depth value as “1,” as shown below.
Using Tree Command
Some Linux versions do not have the tree command; you can explicitly install it as follows.
- For Linux distribution- Debian / Ubuntu
- For Linux distribution- CentOS / RedHat
Using the tree command will display the visual representation of your directories. You can use the tree command along with several options for various functionalities. For getting the output in human-readable format, use the following command with the “-h” option.
You can also use the tree command if you want to target the specific directory.
If the provided directory has several contents, the above command will take time to generate the output.
Using ncdu Command
The ncdu is also known as NCurses Disk Usage, which is also not available in all the Linux distributions by default. You can explicitly install it using the below commands for various Linux distributions.
- For Linux distribution- Debian / Ubuntu
- For Linux distribution- CentOS / RedHat
Use the ncdu command for getting the interactive display of the disk usage.
You can see the current directory being scanned in the upper right corner. In the left column, you will get the numerical size along with a graph of #- signs specifying the relative size.
For selecting the different lines, you can use the up and down arrows. With the right arrow, you will be able to browse into the directory and using the left arrow, you can go back.
You can also use the ncdu command for targeting the specific directory.
For any help with the ncdu command, you can press the “?” key inside the ncdu interface. If you want to quit, you can press the “q” letter.
Conclusion
While working on a Linux system, you must know the usage of a disk and the size of your files or directories. You can use various commands for the purpose that we have mentioned above. You can even create a bash script for emptying the file if the size exceeds a certain limit using the disk usage commands. Once you start working on a Linux system, you will check disk space commands.