Oct 20, 2023
How to Check Disk Space Usage in Linux
As a system administrator, learning Linux commands helps improve your server management efficiency. For instance, you can use them to easily check your virtual private server’s disk space usage.
In this article, we will explain how to check disk space in Linux using the dr and du commands. The df command is used to check storage space, while du is for monitoring disk usage.
We will also explain how to combine these commands to manage your VPS files more effectively. Moreover, you will learn how to check disk space and optimize storage with Hostinger using our built-in features.
How to Analyze the Hard Drive in Linux
Since the df and du commands have slightly different purposes, we will explain them in separate sections. Before proceeding, connect to your remote server using Terminal or an SSH client like PuTTY.
How to Check Disk Space in Linux Using the df Command
The df command, or Disk Filesystem, is used to check disk space. It displays the amount of used and available storage space in your machine.
When executing this command, you will see the default columns: Filesystem, Size, Used, Available, Use%, and Mounted On. It should look something like this:
- Filesystem – provides the file system name.
- Size – provides the total size of the specified file system.
- Used – shows disk space usage in the particular file system.
- Available – specifies the remaining disk space in the file system.
- Use% – displays the amount of used disk space in percentage.
- Mounted On – tells us a particular file system’s mount point.
Add an option to the df command for detailed disk space checking in Linux. Popular options include:
- df -h – shows the result in a human-readable format.
- df -m – displays file system usage information in MB.
- df -k – tells users the file system usage in KB.
- df -T – specifies the file system type in a new column.
- df /home – allows you to view information about a specific file system in a readable format. In this example, it’s the /home file system.
- df – help – lists other options to use and their descriptions.
How to Check Disk Usage in Linux Using the du Command
The du or Disk Usage command will show files and directories’ disk usage on your Linux system. This SSH command requires you to specify which folder or file to check. Here’s the syntax:
du <options> <location of directory or file>
Consider these du command usage examples with the /Desktop directory:
- du /home/user/Desktop – allows users to see the disk usage of their Desktop folders and files, including subdirectories.
- du -h /home/user/Desktop – displays information in a human-readable format, similar to the -h option for the df command.
- du -sh /home/user/Desktop – gives us the total size of a specified folder, namely Desktop.
- du -m /home/user/Desktop – provides us with folder and file sizes in MB. Replace -m with -k to see the information in KB.
- du -h – time /home/user/Desktop – retrieves the last modification date of the displayed folders and files.
- df –help – displays a list of available options with their usage.
How to Optimize Disk Space in Hostinger VPS
Hostinger VPS hosting plans offer various features that help users efficiently manage their server’s disk space. For example, our custom hosting control panel hPanel has a built-in analytics tool showing your VPS storage space usage.
This feature is useful for a quick analysis since you can access it directly from your browser. It also offers a graphical user interface (GUI) that simplifies the task for non-technical users.
To access it, open hPanel and click VPS on the top menu. Select the VPS in question and navigate to Server Usage on the sidebar. You can also select different periods to check disk usage over time.
If you prefer to use commands to check and optimize your remote server’s disk space, use our built-in Browser terminal. This feature allows you to execute bash commands without Terminal or an SSH client.
To access it, click Browser terminal on your VPS overview menu. Then, log in using the root account and start executing the commands.
Check out our tutorial to learn how to use AI prompts for VPS management in Hostinger AI assistant to simplify server administration tasks.
Moreover, hPanel has a Disk Quota feature for setting VPS storage space usage limits to avoid performance issues. Follow these steps to activate it:
- On hPanel, click the VPS menu.
- Select the VPS in question → Settings on the sidebar.
- Click the Disk Quota toggle. Wait until the change is applied.
- Navigate to Settings on the sidebar → Panel Access
- Click the Panel access URL and log in to your account.
- Set the Disk Quota limit. The steps and setting location differ depending on the control panel.
How to Combine Commands and Clean Disk Space
You can combine the df and du commands with other arguments for more detailed information. It gives you more insights into which files you can delete to free up disk space.
Start with the df command to determine which file system needs an immediate cleanup. Then, run these combinations.
Important! We will use the /home/user/Desktop directory path for this tutorial. To check another directory, change it accordingly.
How to Sort Files By Size
To sort files by size, run the du command to list files and folders on /Desktop in a readable format. Then, pipe the result to the sort command with the -rn option.
The script will sort all the files and folders by size in descending order to check the disk space usage in your Linux system. The combination should look like this:
du -h /home/user/Desktop | sort -rn
Remember that you shouldn’t delete files solely based on their size. If not careful, you might delete essential files that will break your project or virtual private server.
How to Exclude by File Size
If you want to see all files beyond a certain size, the most effective way is to use the command below:
du -h /home/user/Desktop | grep '^\s*[0-9\.]\+G'
The grep command lets us search files based on a specified pattern. In this example, the script will output all files bigger than 1 GB. If you want to list files over 1 MB, replace G with M.
How to Exclude File Types
If you want to exclude a particular file format from the search results, use the following combination:
du -h /home/user/Desktop/ --exclude="*.txt"
The –exclude=”.txt” argument tells the du command to display all file formats except .txt documents.
The df and du commands are important file management utilities in a Linux system. The df command is used to check the disk usage of a file system, while du is for analyzing files or directories storage consumption.
These commands take various options to refine the output based on your needs, like -h, -m, and -k. You can also combine them with other commands, such as sort, grep, and exclude for a more specific output.
While running these commands requires Terminal or an SSH, Hostinger VPS plans’ Browser terminal lets you run them directly via a web browser. Moreover, you can use hPanel to quickly see the disk usage and set a quota to optimize it.
Discover Other Linux Commands for Server Management
How to Transfer Data With Curl Command
How to Calculate Process Execution With Time Command
How to Transfer Files Using Scp Command
How to Monitor Changes With Watch Command
How to Shutdown and Restart the Server
How to Transfer and Synchronize Data With Rsync
How to Manage and List Services in Linux
How to Install and Use Linux Screen