Pages

du: Finding the file/folder size from command line in linux

To find the amount of space occupied by any file or folder in the system we can use the command du.

Let us say we have a folder test which contains the files file1,file2,file3,temp

To find the size of the folder test



The first column Indicates the size of the folder and the second column gives the name of the folder. By default du also lists the sizes of only the sub folders and not of files.
In case we want to have a look at the size of the files too we can use the option "-a"



The size column if displayed with no suffix can be difficult to read for bigger sized files, to make the reading easier we can use the option "-h" which sill convert it into more human readable form



The suffix K indicating the size is in Kilo bytes. For Mega bytes it will be M for giga G etc.
If we just want the size of the folder and not its sub folders we can use the option "-s"



We can also pass file names to find the size of a file instead of a folder.



Thus we can easily find the space occupied by any file or folder on the system using the du command

3 comments:

  1. another useful addition I can share is the use of 'du' to find big files/folders.
    Let's say you're in a situation where the system is telling you the /var partition is 90% full and you want to know who's taking up all that space
    So you can use the du command to find out and to "dig" down the tree to find the hog
    so in /var you would do:
    du -ks *
    Now, if you want to narrow down the results to, let's say, folders that are bigger than 1 giga, you could do the following:
    du -ks * | grep [0-9+]G
    This tells grep to display only results that contain a one digit or more, followed by the letter G, if you were to find folders even bigger, with two digits, then it would be
    du -ks * | grep [0-9][0-9]G and so on
    You can also sort the results and then, drill down into the big folders and perform the same commands again

    ReplyDelete
  2. https://www.linuxteck.com/basic-du-command-in-linux-with-examples/

    ReplyDelete