priya
priya

Reputation: 91

Reason of a directory size being zero

Was doing some copy operation on a linux machine to the director /misc. and it did not perform well. After digging came to know that size of /misc is 0. Logs:

[root@gd911-linux-host1 misc]# ls -lrt
total 0
[root@gd911-linux-host1 misc]#

But created a directory myself which empty only & checked its size, showing 4 bytes. Logs:

[root@gd911-linux-host1 random]# du -sk
4       .
[root@gd911-linux-host1 random]#

Please let me know what is the reason of this.

Upvotes: 2

Views: 4538

Answers (2)

Stricto sensu, a directory cannot be completely empty (on Linux and POSIX systems). It is required to have the two entries for . (the directory itself) and .. (the parent directory). Use ls -als to list all entries in some directory. See ls(1) & stat(1). If using * be aware of globbing by the shell (the shell's globbing for * is skipping file names starting with ., see glob(7))

In particular opendir(3) & readdir(3) will give at least these two . & .. entries (except on failure).

Hence, a directory is always taking some place for those two mandatory entries and for additional metadata (i.e. inode).

Your /misc might have not been filled by previous cp commands perhaps for lack of permissions. Be sure that its owner is appropriate (the one used by cp commands). Check with stat /misc or with ls -ald /misc. Then chmod u+rwx /misc at least (see chmod(1); you could use as root the chown(1) command to change ownership).

Upvotes: 1

David C. Rankin
David C. Rankin

Reputation: 84561

Both commands are functioning 100% correctly. When you create a directory, for example:

mkdir -p misc

The directory is empty. If you use ls to list the files in the directory, it reports as expected:

$ ls -lrt misc
total 0

Because there are 0 files in the directory. Now when you look at du the disk usage taken by the directory itself, it correctly notes the size of the file (inode, links, etc.) that represents the directory on disk. Example:

$ du -sk misc
4       misc

Rest assured both are working correctly on your system. ls reporting the number of files contained within the directory, and du reporting the actual size that the directory itself takes on disk.

Upvotes: 3

Related Questions