Pages

Folders in Linux

A Folder in Linux is also a File. It is a File with the list of other Files and sub-folders if any included in it. It contains a list of filenames and the corresponding inodes for each file.

This is how a folder looks like in Linux. Officially a Folder in Linux is called a Directory. If you look at the first character of the permission string in a full listing, a folder is denoted by a "d", that is a Directory. The same name is also used in the command names, such as cd for Change Directory.

It will be useful to remind ourselves the dictionary meaning of the word directory. As in a Telephone Directory. A list of items. This is exactly what a directory in Linux is. A list of files.

We will refer it as a Folder for our convenience.

You can create a new folder in Linux with this commandmkdir foldername

if you use ls -ali command and look at the information about this newly created folder, you will notice that

there is a "d" as the first character in the permissions string

the number of links to this folder are 2, whereas for a newly created file this number is 1

Now let us get inside this folder, for that we will use a command cd foldername

Now use the command ls -ali

This will list the contents of the newly created folder called foldername. You will notice that there are two strange names in it, they are a (.) dot and (..) dot dot. When you look at the first character in the permission string they are 'd', it means they both are directories/ folders.

Notice the inode numbers of these two entries and compare them with the inode numbers of the same folder and it's parent folder. You would notice that the inode number of (.) is same as that of the current folder, and inode number of (..) is same as that of it's parent folder.

This will also give us the answer to a question. What are the two links of a newly created Folder. One is that of it's name and another is the (.) folder that is inside it. As both of them point to the same inode number.

The (.) and (..) are called hidden files/folders and are not visible when you navigate a folder. You need to use the ls -a command to make them visible. Otherwise they are visible only to the system. They have a purpose for the operating system to keep track of the position of the folder.

Let us run a command cd on these folders. While still inside our newly created folder use this command cd .

When you run this command, you will notice that you are still inside the same folder. That is because the (.) has the same inode number as that of the current folder.

If you run cd .. you will notice that you have moved one level up in the parent folder. That is because the (..) folder has the inode number of the parent folder.

This is how the (.) and (..) names are put to use.

Now let us create a sub-folder in the current folder. Notice that we only have two links in this folder.

Let us create a sub-folder using a command mkdir subfolder1

If you look at the long listing, you should notice that the number of links of the parent folder "foldername" has incremented by one. Now it is 3. How did that happen.

To make the contrast more clear let us create a new file in the current folder. Use a command touch testfile1 and then see if it has made any change in the number of links in the parent folder "foldername". You will notice that after creating a new file the number of links of the folder are still the same. It means creating a new file does not add to the links to its parent folder, but creating a sub-folder adds 1 to the links count. Why is that?

We can find the answer to this question in the hidden (..) folder created automatically in the sub-folder. This folder (..) is created with the same inode number as that of the parent folder, this adding an extra hard link and thus increment the link count.

When is a folder Empty ?

A folder is considered in Linux as empty if it does not have any files and sub-folders except the two hidden folders (.) and (..)