3

1> for a empty directory, linux command du will show 0 size, that means it does not use disk space. right? but File.length() in java will not show zero, instead it show the empty directory used some bytes.

If it is true that everything in Unix is a file and should take up disk space, then Java is right here, why du show its 0 blocks.

If it should be 0 blocks then why Java show up some bytes used by an empty directory?

$ mkdir empty_directory
$ du -h empty_directory/
  0B    empty_directory/
$ du  empty_directory
  0     empty_directory


File f = new File("/test/empty_directory");
if (f.exists() && f.isDirectory()) {
    System.out.println(f.length());
}

68

2> for a same no empty file, the size show by linux command du is still not same as that of File.length() in java.
Is the reason only from the difference units: the blocks used by du and the bytes used by File.length()?

$ du -h oneline.txt 
4.0K    oneline.txt
$ du   oneline.txt 
8    oneline.txt
$ ls -s oneline.txt
8    oneline.txt

-s Display the number of file system blocks actually used by each file, in units of 512 bytes, where partial units are rounded up to the next integer value. If the output is to a terminal, a total sum for all the file sizes is output on a line before the listing. The environment variable BLOCKSIZE overrides the unit size of 512 bytes.

File f = new File("/test/oneline.txt");
if (f.exists() && f.isFile()) {
    System.out.println(f.length());
}

26

confused. will you please give some help on this?

Bruce Zu
  • 507
  • 6
  • 17
  • Which version of java do you use? – STaefi Jan 29 '16 at 20:49
  • 3
    from javadoc `Returns the length of the file denoted by this abstract pathname. The return value is unspecified if this pathname denotes a directory.` – Iłya Bursov Jan 29 '16 at 20:52
  • For your first question, the javadoc says: *The return value is unspecified if this pathname denotes a directory*. – JB Nizet Jan 29 '16 at 20:52
  • Possible duplicate of [Get size of folder or file](http://stackoverflow.com/questions/2149785/get-size-of-folder-or-file) – Iłya Bursov Jan 29 '16 at 20:52
  • I saw the javadoc already before I post this question. I am just not clear about the meaning of the "value is unspecified". – Bruce Zu Jan 29 '16 at 21:42

4 Answers4

8

Because the du command has a different purpose, as its man page shows. It shows how much disk space is used by the file, including partially empty blocks. That's different from the size of of the file, which is how much data it contains.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • Disk space == number of disk blocks used. Because space is allocate in disk blocks ... not bytes. – Stephen C Jan 30 '16 at 02:57
  • What part of 'including partially empty blocks' don't you understand? – user207421 Jan 30 '16 at 04:57
  • Thanks @Stephen C and EJP! got it now with the answer of BRasmussen. But what I am still not clear is the first question, the empty directory is also a file in Unix, it also should take up disk space, while du show it takes 0 blocks – Bruce Zu Jan 30 '16 at 05:09
4

On Linux / Unix systems, there is a concept of a "block size". This block size is the minimum amount of space the OS can hand a file. du reports the block size, rather than the exact file size. This is how much space the file is taking up on the OS, not the size of the file.

The du utility displays the file system block usage...

For clarification, let's say my block size = 4 KB (which is fairly common.) If I create a text file that is 2 KB, then the size of the file is 2 KB, but the amount of space the file takes up on the system is 4 KB because that is the minimum amount the OS can hand out.

Similarly, if I made a file that was 11 KB, then the file size is 11 KB, but it actually takes up 12 KB. This is because we need 3 blocks to be able to hold the file.

:Edit: stat -f will show you the block size on a drive in bytes. For example,

stat -f /dev/sda1
...
File: "/dev/sda1"
Block size: 4096
...

Then you can pair that with ls -ls to give a directory listing that includes the number of blocks a file uses. The blocks are the left-most column.

ls -ls
1 -rw-r--r--. 1 brasmussen someGroup        0 Jan 29 16:24 f1

You can see here that even thought file "f1" has a size of 0 bytes, it still takes up one block. Therefore, its OS size is 4KB, and its file size is 0KB.

BRasmussen
  • 350
  • 2
  • 15
  • Thanks a lot for your detail feedback. I am clear about the second question now. for the first question, as every thing is file in Unix, I think an empty directory should also take up disk space, but du command will show the number of blocks usage for a empty directory is 0 – Bruce Zu Jan 30 '16 at 02:12
  • Plus a bit of context: 4k is commonly used as a block size because in traditional hard disk drives (you know, the ones with spinning disk platters and read-heads that move around and stuff), 4k is the size of a sector. A sector is the smallest unit of data that a hard drive can read off of a spinning disk at a time. – nasukkin Aug 01 '16 at 22:51
1

Directories are just special cases of files and both are specializations of an inode, they take up disk space even if they do not contain anything. There has to be something on the disk to denote the existence of the directory, its permissions and other book keeping. So directories are files that use up disk but have no length because they contain nothing.

You are comparing apples and oranges with programs that are not expected to return the same results because they calculate and report back different things.

Community
  • 1
  • 1
  • Thank you for your help. As empty directory take up disk space, what confused me is du command will show the number of blocks usage of a empty directory is 0 – Bruce Zu Jan 30 '16 at 02:06
0

Block size is a feature common to both Linux and Windows. It is caused by the way file systems work and is not Java specific. Because of the way space is handled a block can be occupied by no more than one file.

Create a new text document.txt, write a single character in it and check the file size. It will show size of 1 byte and a size on disk 4 Kb.

sixtytrees
  • 1,156
  • 1
  • 10
  • 25