1

My intention is to read every directory and file rooted at a directory given as input in a depth-first manner and towards it I had written a portion(very very initial) of code as shown below.

int main()
{

 DIR *fd_dir;
 struct dirent *s_dirent;
 struct stat buff;
 char str[100];

 fd_dir = opendir("/home/juggler");
 if(fd_dir == 0)
    printf("Error opening directory");

 while((s_dirent = readdir(fd_dir)) != NULL)
 {
    printf("\n Name %s",s_dirent->d_name);

 } 
 closedir(fd_dir);
}

Now, the directory juggler has 3 directories say A, B and C but an output to this program not only gives these three directories but also .mozilla .zshrc .gvfs .local .bash_history etc which i do not see when opening juggler normally.

What are these extra things inside juggler and how do I not read them

Thanks

Lipika Deka
  • 3,774
  • 6
  • 43
  • 56

1 Answers1

2

In the Unix world, to hide files, you make the first character a .. So when you simply ls in a directory, you don't see them. You have to use ls -a or ls -A to see them.

You can't "ignore them". You can check in your loop to see if the first character is a . and continue.

if ('.' == s_dirent->d_name[0]) {
    continue;
}

But keep in mind that they are all equal citizens. So there is no reason to skip them. What you might want to skip are the special files . (current directory) and .. (parent directory).

cnicutar
  • 178,505
  • 25
  • 365
  • 392
  • @cnicular Thanks...but what are these files – Lipika Deka Jul 11 '11 at 05:11
  • @Juggler You can `vim` / `cd` into most of them. They are generally configuration files that most users don't want to see. For instance, `.bash_history` records the history of your commands. `.zshrc` is sourced by interactive `zsh` shells, `.mozilla` is probably a directory used by firefox et all etc. So they are **useful, needed, real** files. It's just that you don't generally operate directly on them, so they're "out of sight". – cnicutar Jul 11 '11 at 05:14