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
$ du test 4 test/temp 8 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”
$ du -a test 0 test/file2 0 test/file3 0 test/file1 4 test/temp 8 test
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
$ du -h test 4.0K test/temp 8.0K test
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”
$ du -s test 8.0K test
We can also pass file names to find the size of a file instead of a folder.
$ du -h test/file1 0 test/file1
Thus we can easily find the space occupied by any file or folder on the system using the du command