How to Check File and Folder Sizes in Linux

When disk usage exceeds standard thresholds, an alert is typically triggered. Knowing how to use the df and du commands effectively is a smart move in such situations.

    df allows you to view the size of top-level folders, usage ratios, filesystems, and their mount points, but it cannot handle individual files.
    du allows you to view the size of both files and folders.

    Used together, they are highly effective. For example, you can use df to identify which top-level directory is too large, then use du to drill down into specific folders or files to quickly pinpoint the root cause.

    A brief introduction to each is provided below.

    The df command displays the available space and usage of all current filesystems. See the following example:

 

 

The following is a code snippet:

[yayug@yayu ~]$ df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda1             3.9G  300M  3.4G   8% /
/dev/sda7             100G  188M   95G   1% /data0
/dev/sdb1             133G   80G   47G  64% /data1
/dev/sda6             7.8G  218M  7.2G   3% /var
/dev/sda5             7.8G  166M  7.2G   3% /tmp
/dev/sda3             9.7G  2.5G  6.8G  27% /usr
tmpfs                 2.0G     0  2.0G   0% /dev/shm
The -h parameter specifies “Human-readable” output, meaning it displays filesystem sizes in easy-to-read formats like GB and MB.

    In the output above, the first field (Filesystem) and the last field (Mounted on) represent the filesystem and its mount point, respectively. We can see that the /dev/sda1 partition is mounted on the root directory.

    The next four fields 鈥?Size, Used, Avail, and Use% 鈥?represent the partition’s capacity, used space, available space, and usage percentage, respectively. Under FreeBSD, when a hard disk is full, you might see a usage percentage exceeding 100%. This is because FreeBSD reserves some space for root, allowing the root user to write to the filesystem for management purposes even when it is full.

    du: Querying disk space usage for files or folders

    If there are many files and folders in the current directory, running the du command without parameters will recursively list the space used by all files and folders. This is not helpful for quickly identifying what is taking up too much space. Therefore, you need to specify the depth of directory traversal using the parameter –max-depth=, which is an extremely useful parameter! As shown below, note that using “*” allows you to get the space usage of individual files.

    Note: In FreeBSD, whose commands are often more complex than Linux, the du command specifies directory depth with a simpler flag: -d.

du sums up individual file sizes
    df  reports on data block usage

    If a process is holding open a large file, and that large file is directly removed (rm) or moved (mv), du will update its statistics, but df will not. It will still consider the space as unreleased until the process holding the large file is killed.

    As a result, when periodically deleting files under /var/spool/clientmqueue, if the relevant process is not killed, the space will never be released.

    After killing the process using the following command, the system recovered.
    fuser -u /var/spool/clientmqueue
 

How to Check the Size of Linux Directories and File Counts

    Calculate total size

    du -sh xmldb/

    du -sm * | sort -n // Calculate the size of the current directory and sort by size

    du -sk * | sort -n

    du -sk * | grep guojf // Check the size belonging to a specific user

    du -m | cut -d “/” -f 2 // Display the text before the second “/” character

    View how many files are in this folder /*/*/*

    du xmldb/

    du xmldb/*/*/* |wc -l

    40752

    Explanation:

    wc [-lmw]

    Parameter descriptions:

    -l : Number of lines

    -m: Number of characters

    -w: Number of words

Linux: Using ls to View File Sizes in K, M, G Units

#man ls

鈥︹€?br />
-h, –human-readable

                print sizes in human readable format (e.g., 1K 234M 2G)

鈥︹€?br />
# ls

cuss.war    nohup.out

# ls -l

total 30372

-rw-r–r–    1 root root 31051909 May 24 10:07 cuss.war

-rw——-    1 root root          0 Mar 20 13:52 nohup.out

# ls -lh

total 30M

-rw-r–r–    1 root root 30M May 24 10:07 cuss.war

-rw——-    1 root root     0 Mar 20 13:52 nohup.out

# ll -h

total 30M

-rw-r–r–    1 root root 30M May 24 10:07 cuss.war

-rw——-    1 root root     0 Mar 20 13:52 nohup.out

 

 

 

Leave a Comment

Your email address will not be published.