linux du command

How to run the du command in Linux?

du command in Linux – Introduction

The du command in Linux is used to estimate and display the disk usage of files and directories. It provides information about the size of files and directories in a specified location, helping you identify which files or directories are consuming the most disk space.

Basic Syntax

The basic syntax of the du command in linux is as follows:

du [OPTIONS] [DIRECTORY or FILE]

Here, ‘[OPTIONS]’ represents various command options that you can use to modify the behavior of the ‘du’ command. And ‘[DIRECTORY or FILE]’ specifies the path to the directory or file for which you want to calculate the disk usage. If you don’t specify a directory or file, ‘du’ will default to the current directory.

Commonly Used Options of ‘du’ Command

Now, let’s explore some commonly used options with the du command:

  • -h or –human-readable: This option displays the sizes in a human-readable format, using units like “K” (kilobytes), “M” (megabytes), “G” (gigabytes), etc., to make the output more understandable.
  • -s or –summarize: With this option, du will display only the total disk usage for the specified directory or file, rather than providing a detailed breakdown of each file and subdirectory.
  • -c or –total: This option produces a grand total of the disk usage, including the total for all the specified directories or files.
  • -a or –all: When used in conjunction with a directory, this option displays disk usage information for all files and directories, including hidden ones (files/directories whose names start with a dot).
  • -d N or –max-depth=N: This option limits the depth of the directory hierarchy shown in the output to the specified level N. For example, using -d 1 will display disk usage for only the immediate subdirectories and files.

Examples of using ‘du’ commands

Here are a few examples of using the ‘du’ command:

  1. To find the disk usage of a specific directory and sort the output in descending order:

du -h /path/to/directory | sort -rh

This command combines du with the sort command using a pipe (|) to sort the output based on the disk usage in a human-readable format (-h). The -r option in sort specifies a reverse sort order, showing the largest directories or files first.

  1. To display the total disk usage of multiple directories and show a grand total:

du -h -c /path/to/directory1 /path/to/directory2

By providing multiple directory paths, du calculates and displays the disk usage for each directory individually, followed by a grand total using the -c option.

  1. To exclude specific directories or files from the disk usage calculation:

du -h –exclude=/path/to/exclude /path/to/directory

Using the –exclude option, you can specify directories or files that you want to exclude from the disk usage calculation. This is useful if you want to focus on specific parts of a directory tree.

  1. To limit the depth of the output and show only the disk usage summary for each directory:

du -h -d 1 –max-depth=2 /path/to/directory

In this example, the -d option limits the depth of the output to 1 level below the specified directory, while –max-depth allows you to specify a custom depth limit. This provides a summary of disk usage for each subdirectory up to the specified depth.

  1. To redirect the output of du to a file for further analysis:

du -h /path/to/directory > disk_usage.txt

Here, the > operator redirects the output of du to a file called disk_usage.txt instead of displaying it in the terminal. You can later open this file to analyze the disk usage data.

  1. To display the disk usage of all subdirectories individually within a directory:

du -h –max-depth=1 /path/to/directory

This command will show the disk usage of each immediate subdirectory within the specified directory while limiting the output to a maximum depth of 1. It provides a quick overview of the disk space consumption of individual subdirectories.

  1. To find the largest files or directories within a specific directory:

du -ah /path/to/directory | sort -rh | head -n 10

This command combines du, sort, and head to identify the top 10 largest files or directories within the specified directory. It lists all files and directories (-a), sorts them in descending order of disk usage (-rh), and displays only the first 10 lines of output using head -n 10.

  1. To display the disk usage of a directory excluding its subdirectories:

du -h –max-depth=0 /path/to/directory

By setting the –max-depth option to 0, the command will only calculate the disk usage of the specified directory itself, excluding any subdirectories.

  1. To monitor the disk usage of a directory in real-time:

watch -n 1 du -h /path/to/directory

Using the watch command, this example continuously updates the disk usage of the specified directory every 1 second. It provides a live view of any changes in disk usage, which can help monitor disk space consumption during specific activities or processes.

  1. To estimate the total size of a file or directory before copying it:

du -sh /path/to/file_or_directory

By using the -s option, this command provides a summarized output (-h) of the estimated size of a file or directory. It can help you get an idea of the space required before initiating a copy operation.

  1. To display the disk usage of directories in a tree-like format:

du -h –max-depth=2 –time /path/to/directory

This command shows the disk usage of directories up to a depth of 2 levels (–max-depth=2). The –time option also includes the last modification time of each directory, providing additional information about the files and directories.

  1. To display the disk usage in a machine-readable format, useful for scripting or parsing the output:

du -k /path/to/directory

By using the -k option, this command shows the disk usage in kilobytes, making it easier to process the output programmatically.

  1. To limit the display of disk usage to a specific threshold, such as only showing directories or files larger than a certain size:

du -h –threshold=1M /path/to/directory

This command sets the threshold to 1 megabyte (1M). Only directories or files larger than the specified threshold will be displayed, helping you quickly identify the items consuming substantial disk space.

  1. To find files or directories that have not been accessed within a specified time:

find /path/to/directory -type f -atime +30 -exec du -ch {} +

This command combines find and du to search for files (-type f) within the specified directory that have not been accessed in the last 30 days (-atime +30). It then calculates the disk usage (du) of those files and displays a cumulative total (-ch).

  1. To calculate the disk usage of multiple directories but exclude certain subdirectories:

du -h –exclude=dir1 –exclude=dir2 /path/to/directory

This command excludes specific subdirectories (dir1 and dir2) within the main directory (/path/to/directory) from the disk usage calculation. It allows you to focus on the disk usage of the remaining directories.

  1. Display the disk usage in a long format with ownership and permissions:

du -al /path/to/directory

The -l option shows the disk usage in a long format, including ownership, permissions, and other details of files and directories.

  1. Find and display directories with no subdirectories:

find /path/to/directory -maxdepth 1 -type d -empty

This command uses the find command to locate directories (-type d) within the specified directory that have no subdirectories (-empty).

  1. Monitor the disk usage in real-time:

watch -n 1 du -sh /path/to/directory

Using the watch command, this example continuously updates the disk usage of the specified directory every 1 second, providing a live view of changes.

  1. Estimate the total size of a directory and its contents:

du -sb /path/to/directory | awk ‘{print $1}’

Using awk, this command extracts the total size ($1) of the directory and its contents, providing an estimated size in bytes.

  1. Calculate and display the disk usage of a remote directory over SSH:

ssh user@host “du -sh /path/to/directory”

This command remotely executes the du command on a remote server using SSH, displaying the disk usage of the specified directory.

 

Final Thoughts

Now you’ve learnt how to run the du command in Linux. On a UNIX or Linux system, the ‘du’ commands are typically used to evaluate a file’s space usage. We’ve gone over several command options and understandable examples of outputs for disk memory usage to express and communicate the value of a directory size. Additionally, we examined the methods for determining the n-largest files and directories on a drive. These numerous command arguments gave us a good understanding of how the du command functions.

Only information on the input file’s internal system disk space use can be provided by the df program. Nevertheless, the du command shows how much disk memory the selected directories or files are projected to be using. On the terminal of our Linux system, we can obtain a list of all potential command parameters by typing the command man du.

You can learn about linux more deeply by clicking the link below

https://linuxiron.com/what-is-linux-a-whole-introduction/

Learn about the other linux commands by clicking the links below

https://linuxiron.com/echo-command-in-linux/

https://linuxiron.com/how-to-use-nice-renice-commands-in-linux/

https://linuxiron.com/how-to-use-kill-commands-in-linux/

https://linuxiron.com/a-beginners-guide-to-htop-for-process-management/

https://linuxiron.com/15-useful-yum-commands-in-linux/

https://linuxiron.com/how-to-use-the-top-command-in-linux/

https://linuxiron.com/17-ps-command-to-monitor-linux-process-with-examples-linuxiron/

https://linuxiron.com/12-cat-commands-in-linux-with-examples/

https://linuxiron.com/archiving-and-compressing-files-and-directories-in-linux/

Q. What does Linux’s du command accomplish?

The disk space utilized by files or directories is calculated using the du (disk usage) program. The current directory and all its subdirectories are measured by default, and totals for each are printed in blocks at the bottom along with a grand total.

Q. How can I use the Unix du command?

The du command shows how many blocks are being used for files. All files included in a directory are reported on if the File parameter is genuinely a directory. The du command uses the files in the current directory if no File option is given.

Q. How does Linux remove directories?

Use the rm command along with the recursive option, -r, to delete a directory along with all of its contents, including any subdirectories and files. Neither directories nor the contents of directories that have been deleted with the rmdir or rm -r commands may be retrieved.

Q. What distinguishes DF and du from one another?

The simplest way to put the (extremely complex) response into words is as follows: The df tool gives you a broad estimate of how much space is being used up on your disk overall. A significantly more precise snapshot of a specified directory or subdirectory is provided by the du command.

Q. Why do we employ the command du?

The du command is a common Linux/Unix command that enables a user to easily obtain disk utilization data. It works best when used with certain directories and offers a wide range of customization options so you may tailor the output to your needs. Like with other commands, the user has access to a wide range of options and flags.

Leave a Comment

Your email address will not be published. Required fields are marked *