Using du command in Linux


In a Unix-like system, du is a command which stands for “disk usage” is used to determine the space occupied by the files and directories on a disk. This command is very useful in analyzing which file or directory is using how much space on a disk. On the basis of this information, you can optimize the disk space on your system.

There are several options available to use with the du command so that the output can be customized. In this article, we will discuss the usage of the du command with some examples.

The syntax of the du command

The syntax of the du command for using on a Unix based system is given below –

du [options] file

You can see a detailed list of options that can be used with the du command on its man page.

Display the disk usage summary of the current working directory

By default when no file or directory is mentioned with the du command, it will display the disk usage summary of the current working directory. Using option -h will print the size of the file in human-readable form i.e. in KB, MB, or GB, etc.

Now use the following command to see the space usage by your current working directory –

du -h

Display the disk usage summary of a specific directory

Now if you want to see the disk usage summary of a specific directory or file for example – To view the disk usage summary of /home/lalit/dir use –

du -h /home/lalit/dir

As you can see the output of the command in the given image it displays file and directories inside a directory.

How to display the total size of file and directories inside a directory

If you want to view the grand total size of files and directories inside a directory, use the option -s with the du command –

du -sh /home/lalit/Downloads

Display the list of all files and directories with disk usage in a directory

To display all the files and directories inside a directory use -a option with du command –

du -ah /home/lalit/Downloads

How to view the summary of disk usage with last modification time of files and directories

You can use the following command in your terminal if you want to view the list of files and directories with the last modification time –

du -ah --time /home/lalit/Downloads

If you want to know more about the du command then see its manual page by using –

man du

Conclusion

Now you know how to use the du command to check disk usage in Linux. If you have a query then write us in the comments below.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.