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. <strong>Logs:</strong>
[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. <strong>Logs:</strong>
[root@gd911-linux-host1 random]# du -sk 4 . [root@gd911-linux-host1 random]#
Please let me know what is the reason of this.Answer1:
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.
Stricto sensu, <strong>a <a href="http://en.wikipedia.org/wiki/Directory_%28computing%29" rel="nofollow">directory</a> cannot be <em>completely</em> empty</strong> (on Linux and POSIX systems). It is <em>required</em> to have the two entries for
. (the directory itself) and
.. (the parent directory). Use
ls -als to list all entries in some directory. See <a href="http://man7.org/linux/man-pages/man1/ls.1.html" rel="nofollow">ls(1)</a> & <a href="http://man7.org/linux/man-pages/man1/stat.1.html" rel="nofollow">stat(1)</a>. If using
* be aware of <a href="http://en.wikipedia.org/wiki/Glob_%28programming%29" rel="nofollow">globbing</a> by the shell (the shell's globbing for
* is skipping file names starting with
., see <a href="http://man7.org/linux/man-pages/man7/glob.7.html" rel="nofollow">glob(7)</a>)
In particular <a href="http://man7.org/linux/man-pages/man3/opendir.3.html" rel="nofollow">opendir(3)</a> & <a href="http://man7.org/linux/man-pages/man3/readdir.3.html" rel="nofollow">readdir(3)</a> will give at least these two
.. entries (except on failure).
Hence, a directory is <em>always</em> taking some place for those two mandatory entries and for additional metadata (i.e. <a href="http://en.wikipedia.org/wiki/Inode" rel="nofollow">inode</a>).
/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 <a href="http://man7.org/linux/man-pages/man1/chmod.1.html" rel="nofollow">chmod(1)</a>; you could use as <em>root</em> the <a href="http://man7.org/linux/man-pages/man1/chown.1.html" rel="nofollow">chown(1)</a> command to change ownership).