Cyberithub

12 Best Linux du command examples to Check File Space Usage

In this article, we will look into 12 Best Linux du command examples to Check File Space Usage. du is also known as disk usage in Linux. It is one of the most popular tool available to check and track the files and directories usage in the Server. We will go through the usage of du command in detail with the help of examples.

SYNOPSIS

du [OPTION]... [FILE]...
du [OPTION]... --files0-from=F

15 Best Linux du command examples to Check File Space Usage

Linux du command examples to Check File Space Usage

Also Read: 12 Practical and Useful free command examples in Linux(RHEL/CentOS 7/8)

Example 1: How to Check du command version

To check du command version you need to use du --version command as shown below. As you can see from below output, current version is 8.22.

[root@localhost ~]# du --version
du (GNU coreutils) 8.22
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Torbjörn Granlund, David MacKenzie, Paul Eggert,
and Jim Meyering.

Example 2: How to Show write counts for all Files and Directories in Linux

If you want to check the write counts for all files and directories then you need to use -a option du command as shown in below illustration.

[root@localhost ~]# du -a
4 ./hello.rs
2844 ./hello
0 ./file
2848 .

-a : write counts for all files, not just directories. More on du command Man Page.

Example 3: How to Show the Total Size of all the Files

If you are looking to check the total size of all the files and directories then you need to use -c option with du command as shown in below example.

Advertisements
[root@localhost ~]# du -c
2848 .
2848 total

-c : produce a grand total.

Example 4: How to Show Total Size in specified size unit

If you want to check show the total size of a file in units of 1,048,576 bytes then you need to use -BM option with du command as shown below.

[root@localhost ~]# du -BM hello.rs
1M hello.rs

-B : scale sizes by SIZE before printing them. e.g., '-BM' prints sizes in units of 1,048,576 bytes.

Example 5: How to check the size of each file along with last modified time

If you are looking to check the last modified time of all the files along with the file size then you need to use --time option with du -sh command as shown below.

[root@localhost ~]# du -sh * --time
0 2020-05-21 01:07 file
2.8M 2020-06-28 14:02 hello
4.0K 2020-06-28 14:02 hello.rs

-s :  display only a total for each argument.

-h : print sizes in human readable format.

Example 6: How to Check the size of each file along with last access time

If you want to check the size of each file along with the last file access time then you need to use --time=atime argument with du -sh command as shown below.

[root@localhost ~]# du -sh * --time=atime
0 2021-01-17 10:31 file
2.8M 2020-06-28 14:02 hello
4.0K 2020-06-28 14:02 hello.rs

Example 7: How to Check the size of each file along with the creation time

If you are interested in checking the size of each file along with the creation time then you need to use --time=ctime argument with du -sh command as shown below.

[root@localhost ~]# du -sh * --time=ctime
0 2020-08-19 18:51 file
2.8M 2020-08-19 18:53 hello
4.0K 2020-08-19 18:53 hello.rs

Example 8: How to Only Check the Files with Size in GB

If you want to find all the files with size in GB then you need to filter those files from du -sh * output using grep G command as shown below.

[root@localhost ~]# du -sh * | grep G
1.1G file.tar.gz
6.1G another-file.tar.gz
1.6G jorge

Example 9: How to Only Check the files with Size in MB

Just like previous example, if you want to filter all the files of size in MB then you need to use grep M instead of grep G in the output of du -sh * command as shown below.

[root@localhost ~]# du -sh * | grep M
111M aws
32M awscliv2.zip
140M bkp
6.6M Brian
140M CentOS.ISO

Example 10: How to Check the Size of Files till a Specific MAX Depth

If you want to check the size of files and directories till a specific MAX Depth then you need to specify the depth using -d option with du command as shown below. In this example we are specifying the depth to 1 so it will show the size of all the files and directories lying one Level below the command line argument.

[root@localhost ~]# du -d 1 example/
77040 example/test
77616 example/

-d : print the total for a directory (or file, with --all) only if it is N or fewer levels below the command line argument

Example 11: How to Check Man Page of du command in Linux

If you want to check the man page of du command in Linux then you need to use man du command as shown below.

[root@localhost ~]# man du
DU(1) User Commands DU(1)

NAME
du - estimate file space usage

SYNOPSIS
du [OPTION]... [FILE]...
du [OPTION]... --files0-from=F

DESCRIPTION
Summarize disk usage of each FILE, recursively for directories.

Mandatory arguments to long options are mandatory for short options too.

-0, --null
end each output line with 0 byte rather than newline

-a, --all
write counts for all files, not just directories

--apparent-size
print apparent sizes, rather than disk usage; although the apparent size is usually smaller, it may be larger due to holes in ('sparse')
files, internal fragmentation, indirect blocks, and the like

-B, --block-size=SIZE
scale sizes by SIZE before printing them; e.g., '-BM' prints sizes in units of 1,048,576 bytes; see SIZE format below

-b, --bytes
equivalent to '--apparent-size --block-size=1'

-c, --total
produce a grand total

-D, --dereference-args
dereference only symlinks that are listed on the command line

Example 12: How to Check all the available options of du command in Linux

You can also check all the available options of du command using --help as shown below.

[root@localhost ~]# du --help
Usage: du [OPTION]... [FILE]...
or: du [OPTION]... --files0-from=F
Summarize disk usage of each FILE, recursively for directories.

Mandatory arguments to long options are mandatory for short options too.
-0, --null end each output line with 0 byte rather than newline
-a, --all write counts for all files, not just directories
--apparent-size print apparent sizes, rather than disk usage; although
the apparent size is usually smaller, it may be
larger due to holes in ('sparse') files, internal
fragmentation, indirect blocks, and the like
-B, --block-size=SIZE scale sizes by SIZE before printing them; e.g.,
'-BM' prints sizes in units of 1,048,576 bytes;
see SIZE format below
-b, --bytes equivalent to '--apparent-size --block-size=1'
-c, --total produce a grand total
-D, --dereference-args dereference only symlinks that are listed on the
command line
-d, --max-depth=N print the total for a directory (or file, with --all)
only if it is N or fewer levels below the command
line argument; --max-depth=0 is the same as
--summarize
Advertisements

Leave a Comment