1

I understand that the sizes that ls -l reports for directories reflects the space taken up by the "metadata" for those directories. Also, I imagine that this metadata exists in binary/non-human-readable form.

Is there a Unix command to display this metadata in human-readable form?

EDIT: if the answer to the question above is highly dependent on the filesystem, what keywords should I use to search for the answer for a particular filesystem? (I'm interested in the answer for many types of filesystems, including nfs, ext3, ext4, vfat, gpfs, tmpfs, and lustre). When I search for, e.g., "how to display directory data/information/contents", etc. I find only information on how to list the files in a directory, which is not quite what I'm looking for.

  • 1
    Other than ls? That would depend on the type of file-system, and likely only used for debugging purposes by developers of device-drivers, etc. – Thomas Dickey Jun 22 '16 at 22:59
  • @ThomasDickey: thanks; I've updated my question in response to your comment. – kjo Jun 22 '16 at 23:12
2

ls displays that information because the data is stored in the size field of the inode for the directory. This is filesystem dependent. A given filesystem could hold other information there. As an example, ZFS reports the number of directory entries in that field.

If that's the level you're interested in, then reading the data is simple. ls and stat are tools to display the inode information in different ways. You can examine them to see how to pull the data and display it in human-readable form. Take a look at stat(2) for the system call to query the info.

If you just want to gather that number directly, consider stat -c "%s"

% ls -ld /
drwxr-xr-x 44 root root 4096 Jun 10 17:11 /
% stat -c "%s" /
4096
1

Yes, there is: it's called ls

The size reported by ls for a directory is the size of the directory contents. It isn't metadata of the directory, it's metadata of the files in the directory. Most of that is listed with ls -la.

On some Unix variants, you can display the binary form of that data by calling cat (or od, etc.) on the directory. This is not the case on Linux, and on systems where it's possible, the interpretation of that data may depend on the filesystem type or may be normalized (to be compatible with historical filesystem types). See When did directories stop being readable as files? and octal dump of directory

The size reported by ls is more than what it takes to store the data you see with ls for several reasons:

  • There may be metadata that ls doesn't display, such as block lists for the file contents.
  • There may be padding, e.g. unused bits here and there, alignment to machine word boundaries, file names stored in fixed-size fields, etc.
  • There's metadata to organize the list of file names, e.g. a B-tree or search tree structure.
  • Typically files are organized in blocks, and a file uses a whole number of blocks, with the last block being only partially filled unless the file size is exactly a multiple of the block size.
  • On some filesystems, such as ext4, directories don't give back unused blocks after they've grown a lot (because they contained a lot of files) then shrunk a lot (because a lot of files were deleted).

If you want to know about things that ls won't show you, you need to go and look directly at the directory contents. Either run od or similar on the directory, if your Unix variant allows it, or use a low-level tool such as a hex viewer on the partition or a filesystem debugger such as debugfs for ext2/ext3/ext4. The information you'll see in this way doesn't qualify as “human-readable” except to the very few people who are intimately familiar with the filesystem format.

  • No, it can't be ls. ls does not tell me why two directories with exactly the same contents (one is an rsync copy of the other) but residing in different file systems have sub-directory entries whose sizes (according to ls -ls) vary widely (~10x) between the two filesystems. – kjo Jun 23 '16 at 1:08
  • @kjo, that's because the size of the directory (as kept in the inode) is not directly related to either the amount of data in subdirectories or the file structrure. The file structure can influence it, but not guarantee anything. Copying a directory (via cp or rsync) can often shrink it by removing stale directory entries. – BowlOfRed Jun 23 '16 at 2:55
  • @kjo Yes, it is ls. As I explain in my answer, there's space that contains metadata that you can only see with a filesystem debugger (and isn't human-readable unless you're deeply familiar with the filesystem format), and space that isn't used. If the sizes vary wildly, it can be because the filesystems store data in different formats, or because one of the directories contains more unused space. – Gilles Jun 23 '16 at 9:42
  • @BowlOfRed: garbage-collectable items such as "stale directory entries" are precisely the kind of thing I want to look at. – kjo Jun 23 '16 at 10:22
  • 1
    @kjo, that data isn't public (or relevant in some cases). Instead you need to go and walk the private structures. Look at debugfs. You can use that to dump a directory node and see the raw data inside. Example: cs.montana.edu/courses/309/topics/4-disks/debugfs_example.html – BowlOfRed Jun 23 '16 at 15:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.