PuTTYgen

Download PuTTYgen - Putty key generator

Du Command in Linux

Du Command in Linux

There are many commands on Linux that are simple but users leverage them frequently. One such is the du command which is one of the handiest commands on Linux. Du is an abbreviation for “Disk Usage” and is a command that calculates the space usage, the amount of space files and directories consume on the hard disk.

The expression is very useful as users can monitor and configure files or directories that are exhausting the disk space.

What is a du Command?

du command is a primary command-line tool to report disk space on Linux and Unix like operating systems.

While the du command does more than just estimate information of disk usage of files on a computer, it comes with various parameters that present results in various formats. One of the major advantages of using it is that it users can view the file and directory size recursively.

The du command is effective when a user wants to track large files that are occupying space on the disk. More so, as the tool is a standard GNU Command, it comes pre-installed on most machines so there is no hassle to install any packages.

du Command Syntax

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

du Command Options

The best part of du options, also known as ‘flags’, is that it comes with various parameters that make it easy for users to check multiple data easily. Below is a list of options that one can use with the du command:

  • -0, –null: one has to conclude each output line with NULL
  • -a, –all: it reports count of all files in addition to the directories
  • –apparent-size: instead of disk usage, the option will print apparent sizes.
  • -B, –block-size=SIZE: it will scale the sizes to SIZE prior to display on the console
  • -c, –total: prints the grand total
  • -d, –max-depth=N: it produces the total for directory for the files that are N or any level below it
  • -h, –human-readable: it reports the file sizes in a human-readable format such as megabyte, gigabyte, etc.
  • -S, -separate-dirs: it will not produce the size of subdirectories
  • -s, –summarize: the option will only produce the total for each directory and not the size of files within.
  • –time: the option displays the latest alteration of a file or directory
  • –exclude=PATTERN: the option will not consider files that match a particular pattern.

Examples of the du command

The du command is one of the most often used commands by Linux users. We will look at basic examples of how one can use it efficiently. It will cover basic to advanced examples where we can use multiple options with the du tool to get the required output.

Example 1:- Basic command

Let’s say you want to find the disk usage overview of the /home/putty directory tree and all include the sub-directories, then input the following expression:

du /home/putty

Example 2:- -h

The above command prints the result in block format which is a machine language that is not understandable to humans. So, to see the output in a human-readable format such as Bytes, Kilobytes, Megabytes, and Gigabytes, use the “-h” option.

du -h /home/putty

User can use several other parameters with the -h option that ensures the output will print in a human-readable format.

Example 3:- -a

Users can use the “-a parameter” to view the size of all files and directories. Enter the below command to do so

du -a -h /home/putty

One can choose to use the -a option without the “-h parameter” but only with the du command. Using the -a flag without the -h parameter will present the disk usage of all files and directories in computer-readable format.

Incase one only wants to view the total then they can leverage the “-c option”. Below is the expression to use -c option in conjunction with the du command: du -c -h /home/putty

Example 4:- -m and -k

Users can also choose to only print the disk usage of a directory along with its sub-directories in a specific unit such as kilobytes and megabytes. One can pair the parameter with -h option to get the result only in a human-readable format. Below are examples for both:

du -mh /home/putty

The above command will display data in Megabytes. Enter the following expression to view it in Kilobytes.

du -k /home/putty

Now let’s say you want to view files only above a certain size, rather viewing all files. Therefore, users can set a threshold to view only specific file size. For example, view the below example:

du -h -t 100M -a /

Example 5:- -s

The -s parameter comes in very handy as it will users to view the aggregate disk usage of a directory. User has to input the following command to do so:

du -sh /home/putty

Example 6:- du command with specific sizes

Programmers can use the -d option in conjunction with the du command to print sizes until a given level. You must enter the level number to view the files in that particular level. In simple words, using the -d parameter, you can specify the depth of how deep should the command check for files. By specifying level 1, it will analyze 1 directory deep.

Here is an example of the -d parameter:

du -d 1 /home/putty

No, instead of -d 1, enter -d 2.

du -d 1 /home/putty

You will notice that the output has more files than the previous command.

Example 7:- --time

The -time parameter is frequently used by programmers to view the timestamp of when the files was last modified.

du --time -h /home/putty

Example 8:- --exclude

The exclude parameter is vital for those that want to calculate disk usage while excluding some of the file types. For example, in the below command we will estimate the disk usage of a directory while leaving out all “.txt” files from the output.

du -ah --exclude=”*.txt” /home/putty

User can make use of the --exclude option for all file formats, making it easy to calculate disk usage of a directory without the file types you do not want to include.

Example 9:- -L

The -L flag is used to dereference all symbolic links. It helps as a symbolic link occupies space. Below is an example of the -L parameter:

du -L

Example 10:- disk space usage of a specific file

Let’s say you want to calculate the disk space occupied by a particular file. Here is the command to do so:

du home/putty/file. Txt

The du command in Linux is a very useful command that when used with utilities can make life easier. Users can use it with utilities such as -a, sort, -n, -r, head -n and many more. For example, below is how users can use the du to search and sort file systems.

du -a / | sort -n -r | head -n 20

The “sort -n -r” will sort results by size, while the “head -n 20” will show the top 20 results from the sorted files.

The above is a brief of a du command in Linux. When leveraged, users can get a complete overview of the disk space consumed by different files and directories. While standard du command will display tens of thousands of results, users can choose to trim the results by using many of the flags.