Posted on 2011-03-10
Medium Priority
Last Modified: 2012-05-11
Hi all,
I was asked to write a directory traversal program in c (using unix). I know directory traversal is something like a recursive directory tree that finds the path and name, am i right? Can someone explain to me in more details as in what is this used for and what other system calls (other than opendir, readdir, chdir) should i include in my program?
Any help will be greatly appreciated.
Question by:crazy4s
  • 5
  • 4
  • 2
  • +2
LVL 14

Expert Comment

ID: 35099748
I would interpret "directory traversal" to mean to "visiting" a named directory and each sub-directory recursively and perform some function on each directory. The function may be to print the name of the directory but it could be something else. For example, consider the command "find DIR -type d -exec COMMAND {} \;" which will execute COMMAND against every directory.under DIR.

From this perspective, you have the routines that you need as long as the called "function" can operate on the data passed to it.

Author Comment

ID: 35103418
so do you meant that we can print in the tabbed format names of all files in specified directories all subdir tabbed by depth?
and i remembered that we should include getcwd in our program too, if we're to use this system call in my prog is this able to print the current directory and sub directories in it and also check whether the sub directories is a directory instead a file and if is a directory we'll continue to print out the subdir of the particular directory?
and one more thing what does the macro IS_DIR used in this prog? do we need this?
and in your given example, is that a command line, i'm not really understand?
sorry for asking so many questions but i'm really slow in catching up stuffs, hope you understand.
thank you.
LVL 33

Accepted Solution

phoffric earned 2000 total points
ID: 35103778
>> Can someone explain to me in more details as in what is this used for
The  ls -lR  command is an example command which lists all the entities in all folders under the current directory

What order are you expected to traverse the directory? If optional, you could consider emulating the  ls -R  command, but it may be easier for you to write a recursive function that produces a different ordering.

A directory has a number of entities in them, files, folders, links. You need to distinguish between a folder and a file. See Listing 5 in this link:

>> I know directory traversal is something like a recursive directory tree that finds the path and name, am i right?
   I think of path as a full path that includes all the folders down to the entity being examined. You could display this tree this way, but I would think that you could leave out the path and just indicate the entity name with the path being implicit from the parent node. Including an indicator at the end of the name if it represents a folder would be a nice touch.

   If you need to list more than folders, then your nodes probably should have an attribute indicating the type of entity (e.g., folder, file, symbolic link).
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

LVL 33

Expert Comment

ID: 35103855
>> and one more thing what does the macro IS_DIR used in this prog? do we need this?
    In the listing 5 is the macro S_ISDIR.

Printing the tree in the form of a tree can get complicated and should be asked in a different question. Printing the tree like ls -R prints it is a bit easier.

Author Comment

ID: 35103874
well at first i'm not sure what do but now i think he wants us to do recursive as i remembered he told us to put something like chdir("..") at the end of the loop, and that means back to the parent directory, am i correct?
LVL 33

Expert Comment

ID: 35104076
.. is the parent folder.

Author Comment

ID: 35104096
i read the link that you provided earlier, i roughly understand what it is about but i'm not quite sure for some of them.
what is a symbolic links, can you provide some example? and for the lstat, is this used to determine whether it is a dir or a reg file and hence collect the information from it and the print it out?

Author Comment

ID: 35104299
so a recursive directory traversal prog is an equivalent for the ls -R command,  they'll have the same output?
but i don't really understand why do we need to back to the parent folder (the chdir(".."))? Is it because the files in this directory might be a directory that's why we need to do that again and print out the files in this subdir?
LVL 33

Assisted Solution

phoffric earned 2000 total points
ID: 35104677
Firstly, if this question is related to security, as inferred by this link
then I misunderstood your assignment.

Assuming not related to security and you just want to be able to list all entities that exist under a folder, I'll continue.

If you do not know about symbolic links, then apparently it is not part of your assignment, so you can replace lstat with stat. Let's not try to build Rome in a day. Just test for folders vs. regular files.

You have been advised to go back up the to the parent folder using .. This suggests that if your tree looks like:
   B         C         D
E    F             G   H   I

Open in new window

then, after you complete processing the B tree, then you need to process the C and D trees. But to get to these two trees, you need to go back up to the parent folder A, and then drop into C and the up again to A and then down to D.

What I think you should take out of Listing 5 in this link:
is the usage of the S_ISDIR and S_ISREG macros. Bear in mind that your system may have other techniques for this functionality. You should start coding and see whether these macros are defined.


As you know, there are different ways to traverse and print the nodes in a tree.
This link uses binary trees as an example, which is useful for understanding orderings. Just apply the ideas to your needs.

There are three separate aspects to this question.
1) How do I recursively collect the data distinguishing between files and folders?
2) How do I save this data in a data structure?
3) How do I traverse the data structure to produce the output.

I am only dealing with question (1). Once you can collect the data, then try to figure out what kind of data structure you need knowing the end-goal is question (3).

Create a simple directory tree where each folder has three subfolders to, say, a max depth of three, and go through it by hand using what you know. Then try to write the program to do what you do by hand.

Expert Comment

ID: 35104948
Are you sure, the word directory in "Directory Traversal" refers to a directory or a folder?
I feel that, it could be something like searching a "Telephone Directory".
If so, then you should use the "Divide and Conquer" strategy.

Expert Comment

ID: 35106054
directory traversal means going thru each file/dir in the given directory, up to the leaf level. That is, if there is a directory within a directory, traverse recursively in that.

You should be doing a recursive program, with a template similar to this

function to go thru each file in the directory
    if this is a file, print its name (or anything else that you need to do)
    if this is a directory, call the same function again with the directory

The above will recursively go thru each file within the given directory and act as you require

Author Comment

ID: 35109034
i'm not too sure what's symbolic links but during his lecture he did used lstat to check the mode so i assumed that we've to use lstat instead of stat.
okay i get the recursive meaning here.
i'll start my codings now thanks for all the help!

Expert Comment

ID: 35125498
lstat() is identical to stat(), except that if path is a symbolic link, then the link itself is stat-ed, not the file that it refers to.

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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

Installing FreeBSD… FreeBSD is a darling of an operating system. The stability and usability make it a clear choice for servers and desktops (for the cunning). Savvy?  The Ports collection makes available every popular FOSS application and packag…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

569 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