17932

Reason of a directory size being zero

Question:

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.

Answer2:

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>).

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 <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).

Recommend

  • insert operation into a bigquery table
  • How to retrieve the size of all my Google Cloud Storage bucket?
  • RXTX gnu.io.PortInUseException: & WARNING: RXTX Version mismatch
  • manupulating a string to create directories in unix
  • How to make SASS put relative paths in its output
  • Using Netbeans with Cygwin and SDL, including SDL.h creates strange error
  • segue from landscape only to portrait only
  • Android mkdirs() sdcard do not work
  • Classic ASP URL Rewriting
  • Excel distinct count with conditions
  • Regex for nested values
  • WordPress > setting permalink option via script buggy?
  • Android custom URI scheme incorrectly encoded when type in browser
  • is there a way to update filter with async data
  • Primefaces lazy datascroller calling load twice
  • using html data-attributes as css-variable (i.e. text-shadow)
  • uml Composition relationships to RDF and OWL
  • Pycharm: Marking a folder as 'sources root' is not recursive for subfolders
  • Trying to get the char code of ENTER key
  • Using Sax parsing to edit and write XML in VB6
  • ASP.NET MVC Application won't update some controllers
  • Multicolored edittext hint
  • Redux Form - Not able to type anything in input
  • Debug.DrawLine not showing in the GameView
  • How can I sort a a table with VBA with given text condition?
  • Yii2: Config params vs. const/define
  • All Classes Conforming to Protocol Inherit Default Implementation
  • Is there a amazon webstore API for customers?
  • jqPlot EnhancedLegendRenderer plugin does not toggle series for Pie charts
  • How do I rollback to a specific git commit
  • Is there a mandatory requirement to switch app.yaml?
  • Comma separated Values
  • Error creating VM instance in Google Compute Engine
  • Hits per day in Google Big Query
  • how does django model after text[] in postgresql [duplicate]
  • Turn off referential integrity in Derby? is it possible?
  • Add sale price programmatically to product variations
  • Busy indicator not showing up in wpf window [duplicate]
  • Unable to use reactive element in my shiny app
  • How do I use LINQ to get all the Items that have a particular SubItem?