Results 1 to 2 of 2

Thread: Count Files in C code and break when more than X nodes

  1. #1
    Join Date
    Feb 2010
    Beans
    4

    Question Count Files in C code and break when more than X nodes

    We sometimes have directories with more than 800,000 files. Is there a command or way to read the the entries, and without blocking, stop reading when count of files is above a certain number?

    Or even a direct way to read the file count in a directory, without using ls ?

    Looking for the most performant way to count files. And the ability to stop counting (if we need to call an API or com mand) when a certain limit is reached. like say

    Code:
    ls -max 20000

  2. #2
    Join Date
    Mar 2010
    Location
    Squidbilly-Land
    Beans
    Hidden!
    Distro
    Ubuntu

    Re: Count Files in C code and break when more than X nodes

    Having even 1K files in a single directory is a poor design choice. Split them up into chunks.
    You can use the readdir() function in C.
    https://stackoverflow.com/questions/...ectory-using-c
    Can't imagine any faster way. As you can see from the link, there isn't any way to stop reading, once started.
    Manpage confirms this:
    Code:
    READDIR(3)                 Linux Programmer's Manual                READDIR(3)
    
    NAME
           readdir, readdir_r - read a directory
    
    SYNOPSIS
           #include <dirent.h>
    
           struct dirent *readdir(DIR *dirp);
    
           int readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result);
    I only went with C, because you posted to the Development & Programming subforum.

Tags for this Thread

Bookmarks

Posting Permissions

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