DEV Community

Cover image for Key Linux Commands for Disk Space Analysis
Stanlisberg
Stanlisberg

Posted on

Key Linux Commands for Disk Space Analysis

Disk storage is an integral component of the Linux operating system. Knowing the status of your Linux disk space is essential for monitoring its performance, analyzing disk usage management, and ensuring productivity.
In this article, we'll explore some commands that are used to check available disk space on Linux. These commands provide a range of options, also known as flags to enhance the operation of the command. We'll cover three commonly used commands: df, du, and ls.

2. The df Command
The df command, also known as disk free, provides a comprehensive outline of disk space information on the Linux system.

$ df                                                                                
Filesystem  1k-blocks      used   available use%    Mounted on                              
udev          3865423         0     3562764   0%    /dev                                    
tmpfs          758372      2054      798435   1%    /run                                    
/dev/sda2/  276387653  43499328   165690087  23%    /                                       
tmpfs         3954382     57353     3728663   2%    /dev/shm 
Enter fullscreen mode Exit fullscreen mode

This is a detailed overview of the disk space usage information showing the filesystem, available space in kilobytes, disk usage, available free space, usage in percentage, and mount point path. The command is mainly used to track the storage space in the system.

2.1. df -h
The -h flag, when used with the df command basically displays the information in human-readable format, presenting sizes in megabytes and gigabytes which is easier to read. Compared to the df command as a stand-alone, the information is displayed in kilobytes, which is a bit difficult to understand.

$ df -h                                         
filesystem    size    Used   Avail     Use%    Mounted on       
udev          3.8G       0    3.8G       0%    /dev             
tmpfs         774M    2.1M    772M       1%    /run             
/dev/sda2     219G     47G    162G      23%    /                
tmpfs         3.8G     55M    3.8G       2%    /dev/shm
Enter fullscreen mode Exit fullscreen mode

Here, the data are now displayed in megabytes and gigabytes, hence making it easier for users to comprehend.

2.2. df -t
The -t flag displays the disk space information for all mounted filesystems when used with the df command. It also introduces a new column that showcases the filesystem types and their disk usage.

$ df -t                                                                           
Filesystem   Type 1k-blocks     Used  Available Use%  Mounted on      
udev     devtmpfs   3986539        0   34552676   0%  /dev            
tmpfs       tmpfs    806773     3588  677442662   1%  /run            
/dev/sda2    ext4 248756852 55226653   66354422  26%  /               
tmpfs       tmpfs  40943674        4  273732441  14%  /dev/shm  
Enter fullscreen mode Exit fullscreen mode

The datasets above are a representation of filesystem information. A new column is integrated into the data that lists all the file types for the filesystem.

3. The du Command
The du command also referred to as disk usage displays the amount of disk space used by a specific directory and its subdirectories. These sizes are displayed in blocks(bytes), which is not user-friendly.

$ du                                                             
7       ./.config/gtk-3.0                                              
24     ./.config/xfce4/desktop                                        
8       ./.config/xfce4/panel/launcher-7                               
9       ./.config/xfce4/panel/launcher-6                               
73     ./.config/xfce4/xfconf/xfce-perchannel-xml
Enter fullscreen mode Exit fullscreen mode

The dataset is an overview of the disk space consumption of files and directories.

3.1. du -sh
The du -sh command provides the total disk usage size for a specified directory. The -s flag will display the data in a block-size format, while the inclusion of the -h flag converts the data into a human-readable format.

$ du -sh /home                                    
37612M    home                                      
37612M    total
Enter fullscreen mode Exit fullscreen mode

From the output above, the total disk usage of the home directory is 37612MB.

3.2. du -a
The du -a command is used to print the disk usage for files including the directories and sub-directories. It allows us to check for the most sizeable files within a designated path and also remove unused files to free up significant space on the server.

$ du -a                                                                            
2  ./abc1                                                                                   
4  ./.config/gtk-3.0                                                                    
4  ./.config/xfce4/desktop/icons.screen0-813x823.rc                                    
4  ./.config/xfce4/desktop/icons.screen0-1904x910.rc                                   
4  ./.config/xfce4/desktop/icons.screen0-1904x1033.rc
Enter fullscreen mode Exit fullscreen mode

This output lists the disk usage for all files including the directories.

4. The ls Command
The ls command simply lists all the files of a working directory. it's also a practical way to determine the terminal's current directory.

$ ls                                                                                 
camera         pc        downloads git                      insta         
picture        desktop   workspace g0l.13.3.linux-amd64.gz  local         
vs-code        dev       emacs.d   index.php                .m2           
Bash-history   documents en-GB     info-page-conte          host
Enter fullscreen mode Exit fullscreen mode

Here, the datasets show the files in the current working directory.

4.1. ls -a
The -a flag when used with the ls command displays a comprehensive list of both hidden files and directories within the current directory. This flag is mostly used with the ls command when trying to access files that are hidden from view.

$ ls -a                                                                            
.       .brave   downloads gol.13.3.linux-amd64.gz    music           
..      .bse     .eclipse  .gphoto                    .mysql_comp  
camera  .bsedata .eclipse  .gradle                    .node-gyp       
picture .bundle  workspace index.php                  .npm            
vs.code .cache   emacs.d   info-page-content          .nvm 
Enter fullscreen mode Exit fullscreen mode

The output shows all hidden files and directories within the directory. The hidden files are those files that begin with the dot “.” format.

4.2. ls -lh
Using the -lh flag with the ls command accurately displays the entire information about the file or directory name in human-readable format.

$ ls -lh                                                                                                                
drwxrwxr-x  4 ubuntu ubuntu  4.0k  sep  6   12:17   phone_images      
drwxrwxr-x  2 ubuntu ubuntu  16k   jan  9   10:22   assessment        
drwxrwxr-x  1 ubuntu ubuntu  3.0k  jan  2   02:55   bin               
drwxrwxr-x  6 ubuntu ubuntu  4.3k  may  7   08:32   bse
Enter fullscreen mode Exit fullscreen mode

As you can see from the output, it shows the entire information of directory names. Additionally, the sizes of files are displayed in kilobytes hence, making it easier for users to read.

5. Summary
In this article, we looked at several commands that give insights into effective disk space management. The df, du, and ls commands are the most commonly used within the Linux environment to analyze disk storage information such as checking for available disk space and monitoring the disk usage of a directory. With these commands, users can easily examine their system disk usage and optimize storage efficiency.

However, it's important to monitor disk space usage often. Doing so prevents common performance issues, enhances smooth operation, and efficient storage management.

If you find this article interesting, like, share, and comment your thoughts in the comment section below.

Top comments (0)