Find the answer to your Linux question:
Results 1 to 5 of 5
Hi, I am writing a program in C++/Linux where I need to print the information of files in a directory. I need to show info in this way: <filename> : ...
  1. #1
    Just Joined!
    Join Date
    Oct 2008
    Posts
    13

    Question traversing a directory recursively

    Hi,

    I am writing a program in C++/Linux where I need to print the information of files in a directory. I need to show info in this way:

    <filename> : <last-modified-time>

    I want to show all the filenames (only files, no directories and links). I found an API scandir() but it doesn't do a recursive search. It just gives the info of all the elements in the present directory (irrespective of directory/file/link).

    In brief, I need the info as we get when we do "ls -R" from command line.

    Please help me if u can.

    Thanks and Regards,
    krishna

  2. #2
    Linux Newbie
    Join Date
    Jul 2008
    Posts
    181
    Why don't you look at the source code of "ls"?

  3. #3
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    It seems to me that scandir() might be too complex for your needs. Try these:
    Code:
    opendir()
    readdir()
    closedir()
    stat()
    Those, plus your basic skills in string manipulation (for adding to your "current" directory) and recursion, ought to do the trick.

    Hope this helps.
    --
    Bill

    Old age and treachery will overcome youth and skill.

  4. #4
    Just Joined!
    Join Date
    Oct 2008
    Posts
    13

    Thumbs up

    Thanks!

    I got to know about one more function to do the purpose.

    ftw()/nftw()

    Thanks and Regards,
    krishna

  5. #5
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    Code:
    ftw()/nftw()
    Cool! I hadn't even been aware of these.

    The only reason you might want to avoid them is if you want to sort the names of the files in a directory before listing them; and, more importantly, if you want to sort the names of the directories in a directory before descending into those directories and listing their files.

    You want to do such sorting if you want to mimic
    Code:
    ls -R
    If you run this shell script, you'll see an example of how ftw() and nftw() don't sort:
    Code:
    #!/bin/sh
    
    rm -rf tuesdaydir tuesdayprog*
    
    mkdir tuesdaydir
    mkdir tuesdaydir/1
    mkdir tuesdaydir/3
    mkdir tuesdaydir/2
    touch tuesdaydir/1/alpha
    touch tuesdaydir/1/beta
    touch tuesdaydir/2/gamma
    touch tuesdaydir/2/delta
    touch tuesdaydir/3/epsilon
    touch tuesdaydir/3/zeta
    
    cat > tuesdayprog.c <<EOD
    #define _XOPEN_SOURCE 500
    #include <ftw.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    
    int ftwsub(const char        *fpath,
               const struct stat *sb,
               int                typeflag
              )
    {
      printf("%s\n",fpath);
    
      return 0;
    
    } /* ftwsub() */
    
    int nftwsub(const char        *fpath,
                const struct stat *sb,
                int                typeflag,
                struct FTW        *ftwbuf
               )
    {
      printf("%s\n",fpath);
    
      return 0;
    
    } /* nftwsub() */
    
    int main(void)
    {
      char   *version_string_content;
    
      size_t  version_string_length;
    
      version_string_length=confstr(_CS_GNU_LIBC_VERSION,NULL,0);
    
      if(version_string_length<1)
      {
        fprintf(stderr,"first confstr() failure\n");
    
        return 1;
      }
    
      version_string_content=malloc(version_string_length);
    
      if(version_string_content==NULL)
      {
        fprintf(stderr,"malloc() failed\n");
    
        return 1;
      }
    
      if(confstr(_CS_GNU_LIBC_VERSION,version_string_content,version_string_length)
         !=version_string_length
        )
      {
        fprintf(stderr,"second confstr() failure\n");
    
        return 1;
      }
    
      printf("%s\n",version_string_content);
    
      printf("*** with ftw() ***\n");
    
      ftw("tuesdaydir",ftwsub,5);
    
      printf("*** with nftw() ***\n");
    
      nftw("tuesdaydir",nftwsub,5,0);
    
      return 0;
    
    } /* main() */
    EOD
    
    gcc -Wall tuesdayprog.c -o tuesdayprog
    
    tuesdayprog
    This program visits directory tuesdaydir/3 before it visits directory tuesdaydir/2. Here's the output:
    Code:
    glibc 2.7
    *** with ftw() ***
    tuesdaydir
    tuesdaydir/1
    tuesdaydir/1/alpha
    tuesdaydir/1/beta
    tuesdaydir/3
    tuesdaydir/3/epsilon
    tuesdaydir/3/zeta
    tuesdaydir/2
    tuesdaydir/2/gamma
    tuesdaydir/2/delta
    *** with nftw() ***
    tuesdaydir
    tuesdaydir/1
    tuesdaydir/1/alpha
    tuesdaydir/1/beta
    tuesdaydir/3
    tuesdaydir/3/epsilon
    tuesdaydir/3/zeta
    tuesdaydir/2
    tuesdaydir/2/gamma
    tuesdaydir/2/delta
    --
    Bill

    Old age and treachery will overcome youth and skill.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...