Friday, July 22, 2022

10 Examples Of du Command in Linux

 Here, we shall be looking at another command in Linux. The” du” command. Before giving the examples you can use with it I will be explaining the du itself, What is being used for and When to use it, and How to use it

So Firstly, What is DU or What does the command mean?

The full meaning of du is Disk usage. This helps in planning and maintaining the file’s space. The command is used to keep track of any files or directories that are taking up too many spaces in the hard disk drive. Now we shall be taking the examples one after the other.

1. You can use the du command in printing the files in human-readable format with the -h option

Now see what we have:

“du -h /home”



What this command does is that it prints the files in the home directory or folder in a human-readable format. Either Kilobyte, Megabyte, or Gigabyte (use option -h)

2. You can use the du command to print the total size with option -c, Here is what we have:

“du -c -h /home”



Now, let us see what we have here critically there are two options which are -c and -h. Option -c is what is making the printing of the total size possible, while option -h handles it by presenting it in a human-readable format. We dealt with that in number 1. Since we are printing the total size the list is long and I can't provide all, I just cut some parts which is seen above.

3. Another thing that the du command could be used with is printing all files and directories. With the option -a.

Now we have :

“du -a -h /home”



The command here above prints all files, including all the directories, with the help of option -a. the second option which you can find there shows that all the files and directories should be printed in a human-readable format.

4. You can use the du command to check the last time you modified a file. 

With the option --time that is possible.

See what we have:

“du --time -h /home/webmaster/.java”



To explain what I did with the commands. The major thing here is that this gives us the last modified time with the option --time. We know what option -h does by now, it only presents whatever you are trying to do in a human-readable format. So What I was trying to do is that inside the home directory, there is a folder called webmaster and inside the folder, all files with the extension .java should be displayed with the timestamp or the last time modified.

5. To exclude a particular kind of file while displaying others’ disk size

“du -h --exclude="*.php" ”

There could be times you may need to exclude some files, which means the disk space of all files will be displayed, it will only exclude files with the .php extension in our case. That Is PHP files shall be excluded.

6. To display the file in a specific unit e.g megabyte, kilobyte or gigabyte

Now, I want this displayed in megabyte

“du -mh /home”

7. To display file disk space in a specific unit, this time around kilobyte.

Simply do :

“du - kh / home”



The option -k makes that work and the h has always been a human-readable format

8. To view disk usage of a directory

“du -sh/ home”



There could be times you need to view the aggregate disk usage of a directory. The parameter -s is what you will use and you would see the disk usage

9. To change the default block size output to either kilobyte, megabyte, or gigabyte

“du -BK /home”



This command changes the default block size to kilobyte. So you are going to see all the sizes in kilobytes.

10. To change the  block size output to  megabyte

To change the default size to a megabyte we have the below command

“du -BM /home”



Nothing so serious, same as above but the slight difference is the ‘M’ which stands for the megabyte.

If needs arise and you have to do for a gigabyte, you do the same thing. But this time around with the option G. And here is the picture of it.



Conclusion:

The “du” command is a command you will not do without unless you won’t have anything to do with the disk usage which is almost impossible. This article provided the meaning of du and what it does, as well as different options or parameters you could use with du to give you a different result

 

No comments:

Post a Comment

Feel free to comment, ask questions if you have any doubt.