1

I've also asked a sibling question for Windows.


In Linux (I just tested):

  • The size of an empty folder is given as 4 KB (regardless of its name)
  • The size of an empty file is given as 0 bytes

However, files and folders obviously have names, that must be stored somewhere.

  • Where, roughly, are they stored?
  • Is the maximum memory available to store these data a predefined number or is it dependent on the available space in disk?
  • Does an empty file with a short name take less space than an empty file with a larger name? (or perhaps the data structure in which these names are stored have a fixed byte count for each file, which perhaps fills the remaining bytes with \0?)
  • Does an empty folder with a short name take less space than an empty folder with a larger name?
  • Does an empty folder called foobar take less space, same space or more space than an empty file called foobar?
  • Does an empty file at /etc/empty.txt take less space, same space or more space than an empty file at /etc/long/nested/path/until/the/empty/file/is/reached/empty.txt?
2
  • So.. all of the questions you ask are dependent on the file system itself but there are some common patterns you can find within. Think of a "directory" as a text file holding file names with pointers to their first cluster.. and also "sub directories".. holding pointers to other directories files. the space for the name is held in the directory file itself for most file systems.. but there are no rules. Soft/hard links are additional entries to this list that are also referenced by other "directories". Someone smarter than me will hopefully refine my explanation. Commented Mar 11, 2021 at 4:16
  • So. I guess I didn't directly address your questions. In most cases, ALL of the space you are talking about is stored in the directory file. (Often called ".") ;) Again.. some filesystem guru might chime in. Commented Mar 11, 2021 at 4:19

2 Answers 2

3

File names are stored in the directory. A directory is composed of directory entries, and each directory entry contains a filename and the inode number for that file. The inode contains various metadata, such as the user id and group id owning the file, the time that the inode was last modified, etc. A file with a longer file name will take up more space in the directory. There can be multiple directory entries (in the same or different directories) that reference the same inode number. When an additional directory entry is created pointing to the same inode, it is referred to as creating hard link.

There is a fixed number of inodes in the inode table, so if you create a large number of inodes, you can eventually run out of space in the inode table. (You can see how many inodes are in use via "df -i". You can also create the file system with a large number of inodes, if you anticipate the average size of inodes in the file system to be smaller than the default.)

Directories are also composed of inodes, and work much like files, with the following exception. First, even an "empty" directory will have directory entries for '.' (the directory itself) and ".." (the parent directory), so even an "empty" directory will take up 4k of space. Secondly, a directory may not have hard links. That is (ignoring the '.' and '..' entries), there can only be directory entry referencing a directory inode. This means that directories form a tree, and not a generalized graph.

2
  • Hello! Sorry to take so long to give feedback. Thank you for this answer, upvoted. However, I still have a question. I've looked at nonempty folders, including folders with many files and subdirectories, and they also seem to take exactly 4 KiB of space. How is this possible? This seems to contradict what you said: "A file with a longer file name will take up more space in the directory."
    – Pedro A
    Commented Mar 29, 2021 at 2:38
  • If you create a large number of files with 200 byte filenames, the directory will size will grow in chunks of 4k. The same will be true if you create a large number of files that are only 4 characters long. But the rate at which the directory grows in chunks of 4k will be higher if you are creating filenames that are longer. That's because the file system is allocating blocks in 4k chunks, and you don't see the unused space in the directory. Commented Mar 30, 2021 at 16:37
1

ls command displays, among other things, the file size:

  1. If the file is a directory, the size displayed is the one allocated to it by the system (Per block of 4K: 4096 bits for ext4 file system) even if there is 0 byte in it.

  2. If the file is an ordinary one, the size displayed is the size of its content, not the size occupied by the file.

To know the size occupied by the file whether it is an ordinary file or a directory, use the command "du file_name"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .