How to recursively list directories in C on Linux?

How to Recursively List Directories in C on Linux

To recursively list directories in C on Linux, you can use the POSIX API, specifically the functions provided by the dirent.h library for directory traversal. The approach involves reading a directory, identifying subdirectories, and recursively processing each subdirectory.

Here’s a complete guide:

Steps

  1. Include Required Libraries: Use dirent.h for directory operations and sys/stat.h for file type checks.
  2. Open the Directory: Use opendir() to open a directory.
  3. Read Entries: Use readdir() to iterate over directory entries.
  4. Check Entry Types: Use stat() or DT_DIR to identify subdirectories.
  5. Recursive Call: For each subdirectory (excluding . and ..), call the function recursively.
  6. Handle Edge Cases: Exclude hidden directories or handle symbolic links if necessary.

Code Implementation

#include <stdio.h> #include <stdlib.h> #include <dirent.h> #include <sys/stat.h> #include <string.h> // Function to recursively list directories void listDirectories(const char *basePath) { struct dirent *entry; DIR *dir = opendir(basePath); if (dir == NULL) { perror("Unable to open directory"); return; } while ((entry = readdir(dir)) != NULL) { // Skip "." and ".." if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) { continue; } // Construct full path char path[1024]; snprintf(path, sizeof(path), "%s/%s", basePath, entry->d_name); // Check if the entry is a directory struct stat statbuf; if (stat(path, &statbuf) == 0 && S_ISDIR(statbuf.st_mode)) { printf("Directory: %s\n", path); // Recursive call listDirectories(path); } } closedir(dir); } int main(int argc, char *argv[]) { if (argc != 2) { fprintf(stderr, "Usage: %s <directory_path>\n", argv[0]); exit(EXIT_FAILURE); } listDirectories(argv[1]); return 0; }

Explanation

  1. Opening the Directory: The function opendir(basePath) opens the specified directory for reading. If the directory cannot be opened, opendir() returns NULL, and we handle the error using perror.

  2. Reading Entries: The readdir() function reads each entry in the directory, returning a struct dirent pointer.

  3. Checking File Type:

    • stat(): Fetches metadata about the file. The S_ISDIR() macro checks if the file is a directory.
    • Alternatively, you can use entry->d_type for simpler checks (DT_DIR for directories). However, this field may not always be reliable on certain filesystems.
  4. Skipping . and ..: These special entries represent the current directory and parent directory, so they must be ignored to avoid infinite recursion.

  5. Recursive Call: For each subdirectory found, listDirectories(path) is called recursively.

  6. Closing the Directory: After processing all entries, the directory is closed using closedir().

Output Example

For a directory structure like this:

/home/user/test
    ├── dir1
    │   └── dir2
    │       └── file.txt
    └── dir3

Running ./listDirectories /home/user/test produces:

Directory: /home/user/test/dir1
Directory: /home/user/test/dir1/dir2
Directory: /home/user/test/dir3

Considerations

  1. Path Length: Ensure the buffer (path[1024]) is large enough for the full path. Use PATH_MAX if available.
  2. Symbolic Links: Use lstat() instead of stat() if you want to handle symbolic links separately.
  3. Error Handling: Add checks for permission errors or inaccessible directories.
  4. Hidden Files/Directories: To include hidden directories (starting with .), ensure no additional filtering is applied.

Enhanced Version with File Listing

If you also want to list files within directories, include them in the output:

void listContents(const char *basePath) { struct dirent *entry; DIR *dir = opendir(basePath); if (dir == NULL) { perror("Unable to open directory"); return; } while ((entry = readdir(dir)) != NULL) { if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) { continue; } char path[1024]; snprintf(path, sizeof(path), "%s/%s", basePath, entry->d_name); struct stat statbuf; if (stat(path, &statbuf) == 0) { if (S_ISDIR(statbuf.st_mode)) { printf("Directory: %s\n", path); listContents(path); // Recursive call } else { printf("File: %s\n", path); } } } closedir(dir); }

Example Output

For the same directory structure:

Directory: /home/user/test/dir1
File: /home/user/test/dir1/file1.txt
Directory: /home/user/test/dir1/dir2
File: /home/user/test/dir1/dir2/file.txt
Directory: /home/user/test/dir3

This version differentiates files from directories while maintaining recursive traversal.

TAGS
Coding Interview
CONTRIBUTOR
Arslan Ahmad
Arslan Ahmad
ex-FAANG engineering manager and author or Grokking series.
-

GET YOUR FREE

Coding Questions Catalog

Design Gurus Newsletter - Latest from our Blog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
Who is the head of HR at Amazon?
What is a PM in hiring?
Why work for Shopify?
Which company interview is the hardest?
What is the eligibility for Microsoft interview?
what is Alibaba hiring process?
Related Courses
New
Grokking the AI System Design Interview course cover
Grokking the AI System Design Interview
Learn to design AI systems the way interviewers expect: classic ML products, LLM and RAG architectures, and agentic systems, all through the lens of the system design interview.
4.8
(1,192 learners)
Discounted price for Your Region

$123

Grokking the Coding Interview: Patterns for Coding Questions course cover
Grokking the Coding Interview: Patterns for Coding Questions
The 24 essential patterns behind every coding interview question. Available in Java, Python, JavaScript, C++, C#, and Go. The most comprehensive coding interview course with 543 lessons. A smarter alternative to grinding LeetCode.
4.6
Discounted price for Your Region

$197

Grokking Modern AI Fundamentals course cover
Grokking Modern AI Fundamentals
Master the fundamentals of AI today to lead the tech revolution of tomorrow.
4.1
Discounted price for Your Region

$72

Design Gurus logo
One-Stop Portal For Tech Interviews.
Copyright © 2026 Design Gurus, LLC. All rights reserved.