File System

Posted on 1999-07-01
Medium Priority
Last Modified: 2013-12-06
In unix file system there is a block called "Directory block" which points to the list of objects containing inode and the file name. How can i get the contents in the directory block ?
Question by:jituj
  • 2
LVL 51

Expert Comment

ID: 2011327
opendir() ?
LVL 14

Accepted Solution

chris_calabrese earned 60 total points
ID: 2011328
A "Directory" in the Unix filesystem is a mapping between names and corresponding inode-numbers.  It's not any kind of fixed block, but actually pretty similar to a regular file.  As an aside, because the directories only reference the inodes, not contain them, it is therefore possible that the same file (inode) can be pointed at by different directories under different names.

Directory information can be read with the opendir() and readdir() C-language functions.  Here's an example from the HP-UX man page for opendir():
     The following code searches the current directory for an entry name:

           DIR *dirp;
           struct dirent *dp;

           dirp = opendir(".");
           while ((dp = readdir(dirp)) != NULL) {
                if (strcmp(dp->d_name, name) == 0) {
                     (void) closedir(dirp);
                     return FOUND;
           (void) closedir(dirp);
           return NOT_FOUND;

You can get information about the file itself with the stat() system call.

Author Comment

ID: 2011329
i also found the same solution for the problem but this is not very efficient bec. everytime u have to check whether the name u are getting from readdir is a directory or some other file , i want handle to the list of directories which again points to the list of objects having inode number and the filename associated with the inode number.
LVL 14

Expert Comment

ID: 2011330
The Unix filesystem semantics are structured in such a way that you _must_ read the directory with readdir() and then stat() the entries to tell if they, in turn, are directories.  Sorry, there is no way around this.  Meanwhile, it actually is pretty efficient because the kernel keeps caches for the name/inode translations and the inode contents, so you're usually looking in the cache and not going repeatedly to the disk.

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

I have been running these systems for a few years now and I am just very happy with them.   I just wanted to share the manual that I have created for upgrades and other things.  Oooh yes! FreeBSD makes me happy (as a server), no maintenance and I al…
Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…

624 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question