Introduction du Command in Linux
The du command is known as the “disk usage” command gives the estimation of the total amount of the disk memory space used by the input files and stored files in memory directories. It is used to measure and identify the memory usage of specific files and directories that take up a large sum of the disk memory usage. When we give a file name that is in the directory, the du will give the estimation of the memory usage of the disk in that directory.
In this topic, we are going to learn about du Command in Linux.
du command Usage & Syntax:
The Basic syntax for the du command is:
du [OPTIONS]... [FILE]...
When there is no FILE which has been specified, then du will automatically report the usage of the disk memory in the current directory that we are working. The memory usage of the disk is given in bytes of the given file or the directories memory usage where the file is located and also the subdirectories of that file when we execute the command without option.
We can give multiple file names and directory names as input to the du command:
du ~/Documents ~/Pictures ~/.zsg
du doesn’t work on the file or directory which we don’t possess the authorization for. The command will throw an error since the du command would not be able to read the directory. For such cases, we need to add a keyword ‘sudo’ at the beginning of the command to run on such files and directories.
The frequently used du options are,
The -a is an option that instructs to du to show the disk memory usage of individual files in that particular directory.
du -a ~/Documents
The -h option is used to get the space usage of a directory in a readable format in simple terms.
For instance, if we want the size of the /Doc/lib and its subdirectories, the following command is run:
sudo du -h /var
Sudo is used since that directory is not readable to the users.
10.0K / Doc/lib/apt/mirrors
There is another option named -s which is used to give us the estimation of a specific directory.
To exclude the subdirectories memory usage in the output, use the -s option, which will only give the total memory size of the given directory,
sudo du -sg /var
When we want to know the combined size of multiple directories we can use -c option which will give the total sum of the size of the given directories.
sudo du -csg /Doc/log /Doc/lib
An option called max-depth which is used to get the memory usage space of different levels of the sub-directories. We can give the level to the command to get the disk space usage of a specific level of the subdirectory.
Example of du Command in Linux
Below is the example mentioned below:
sudo du -h --max-depth=2 /Doc/lib
An option called as –apparent-size which can be used to get the estimate of how much space or data is existing inside a particular file.
sudo du -sg --apparent-size /var/lib
du is a command which can be used to get all the directories that start with a particular letter or word. It is used to identify the pattern of the shell and provide information according to it.
sudo du -csg ~/P*
The du command could be used to combine with other commands with the help of pipes. Pipes can be used to join the du command with other commands in Linux.
To take an example, if we want to print top 4 large directories inside the system we can give the du output for sort command to sort directories concerning their sizes which we gave and we use pipe to get output of that command and we will give it to the top command where only the top 4 directories will be printed.
sudo du -sg /var/ | sort -rsg | head -4
The du commands in a UNIX system or Linux system is primarily used for evaluating the space usage of a file. We have discussed the different command options and examples of disk memory usage outputs, which is given in a readable format, representing and giving the value of a directory size and also, we saw the commands used for estimating the n-largest files and folders on a disk. Form these various command options we got good knowledge and understanding of how to work with the du command.
df command can only give them information about the disk space usage of the input file inside a system, but du command gives the actual estimated amount of the disk memory space used up by the given directories or files. We can get the different command options that are available by typing the command man du at the terminal of our Linux system.
This is a guide to du Command in Linux. Here we discuss the examples of du Command in Linux along with the usage and basic syntax. You may also have a look at the following articles to learn more –