Reading and Deleting Directories

This lesson goes into details of how to read and delete directories.

We'll cover the following

Reading directories

Now that we’ve created a directory, we might wish to read one too. Indeed, that is exactly what the program ls does. Let’s write our own little tool like ls and see how it is done.

Instead of just opening a directory as if it were a file, we instead use a new set of calls. Below is an example program that prints the contents of a directory. The program uses three calls, opendir(), readdir(), and closedir(), to get the job done, and you can see how simple the interface is. We just use a simple loop to read one directory entry at a time, and print out the name and inode number of each file in the directory.

Get hands-on with 1400+ tech skills courses.