Why The Size of an Empty Directory in Linux Is 4Kb

Why the size of an empty directory in Linux is 4KB?

I'm going to break this question down into 3 parts, 2 of which I can answer...

Part 1: why isn't an empty directory size 0?

Because it contains . and .. so it's not really empty.

Part 2: Why is 4K the minimum?

Because that's the filesystem's block size. You can set it smaller when you create the filesystem, but there is overhead. The filesystem must remember a free-or-in-use flag for every block, so smaller blocks = more blocks = more overhead. (In the early days of ext2, the default block size was 1K. Disks were small enough that the space saved by not allocating a multiple of 4K for every file was more important than the space used for the free block map.)

Block sizes over 4K aren't possible because 4K is the page size (the smallest unit of virtual memory) on most processors, and Linux wasn't designed to deal with filesystem blocks bigger than memory pages.

Part 3: When you ls -l a regular file, you get the actual number of bytes used but when you ls -ld a directory, you get the number of bytes allocated. Why?

This part I don't know. For regular files, there is an allocation size you can view with ls -s, and the two sizes actually tell you different things. But on directories, the -l size is like a redundant copy of the -s size. Presumably the kernel could report a size that indicates how much of the 4K block is actually used, but it doesn't. I don't know why.

why the size of File.length() in java is not same with that of linux du command

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.

Get the size of a directory (not its content)

https://en.cppreference.com/w/cpp/filesystem/file_size

For a regular file p, returns the size determined as if by reading the
st_size member of the structure obtained by POSIX stat (symlinks are
followed)

The result of attempting to determine the size of a directory (as well
as any other file that is not a regular file or a symlink) is
implementation-defined.

file_size on a directory may actually work in some implementations, but I guess that yours doesn't. There doesn't appear to be a pure C++ alterantive, but you can call the POSIX stat function yourself. That does work on directories and reports the number you want.

Summing total file sizes of directory is different by a large margin: Ruby -e, du -ach, ls -al total

If du is showing you a lot of 4K and 8K files, this is because it is showing you the block size. For performance, storage on disk is made up of blocks. A typical block these days is 4K. Even a single byte will take a full block.

$ echo '1' > this

$ hexdump this
0000000 31 0a
0000002

$ ls -l this
-rw-r--r-- 1 schwern staff 2 Dec 5 15:16 this

$ du -h this
4.0K this

$ du --apparent-size -h this
2 this

$ ruby -e 'puts File.size(ARGV[0])' this
2

The file in question has 2 bytes of content. ls -l and File.size report the content of two bytes.

du, by default, reports the block size of the file. This is because it is a Disk Usage tool and you want to know the true amount of disk taken up. Those 2 bytes take up 4K of disk. 1000 2 byte files will take 4000K, not 2000 bytes.

For this reason, many programs will avoid having many tiny files and instead save disk space by packing them together into a single image file. A simple example is Git packfiles.

Check if directory has any files

Check if this would work for you:

[ "$(find /E/Nuvem/Músicas -maxdepth 1 -type f)" ] && echo "Not Empty" || echo "Empty"

EDIT: If you would like to check subfolders for files, just ignore the -maxdepth 1 option:

[ "$(find /E/Nuvem/Músicas -type f)" ] && echo "Not Empty" || echo "Empty"

linux command to get size of files and directories present in a particular folder?

Use ls command for files and du command for directories.

Checking File Sizes

ls -l filename   #Displays Size of the specified file
ls -l * #Displays Size of All the files in the current directory
ls -al * #Displays Size of All the files including hidden files in the current directory
ls -al dir/ #Displays Size of All the files including hidden files in the 'dir' directory

ls command will not list the actual size of directories(why?). Therefore, we use du for this purpose.

Checking Directory sizes

du -sh directory_name    #Gives you the summarized(-s) size of the directory in human readable(-h) format
du -bsh * #Gives you the apparent(-b) summarized(-s) size of all the files and directories in the current directory in human readable(-h) format

Including -h option in any of the above commands (for Ex: ls -lh * or du -sh) will give you size in human readable format (kb, mb,gb, ...)

For more information see man ls and man du

Why does Page Directory entries need 20 bits to point 2^10 Page Tables?

A page has a size of 4096 bytes, i.e., 2^12 bytes.

This means that pages are aligned to a multiple of 4 KB, and that the address of a page is xxxxxxxxxxxxxxxxxxxx000000000000.
In other words, a page address needs 12 bits less than the address bus size.
For 32-bit addresses, this ends up being 20 bits.

A page directory entry has 32 bits, so 2^10 of them fit into a 4 KB page.

du -ahx shows 15Gb in folder but there are only 32M in files on it

4KB is the size of a directory and from what I see of du output your /tmp directory is full of directories (named for instance schemajs-2.1.1-linux-x86_64.tar.bz2-extract-151708939813) . Inside those directories are the file occupying disk space. It is always the same file of 66 MB named
schemajs-2.1.1-linux-x86_64



Related Topics



Leave a reply



Submit