How to recursively list directories in C on Linux?

Free Coding Questions Catalog
Boost your coding skills with our essential coding questions catalog. Take a step towards a better tech career now!

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
Design Gurus Team
-

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
What is networking in software engineering?
Which software course is the highest salary?
Iterating solutions using constructive feedback from mentors
Related Courses
Image
Grokking the Coding Interview: Patterns for Coding Questions
Grokking the Coding Interview Patterns in Java, Python, JS, C++, C#, and Go. The most comprehensive course with 476 Lessons.
Image
Grokking Data Structures & Algorithms for Coding Interviews
Unlock Coding Interview Success: Dive Deep into Data Structures and Algorithms.
Image
Grokking Advanced Coding Patterns for Interviews
Master advanced coding patterns for interviews: Unlock the key to acing MAANG-level coding questions.
Image
One-Stop Portal For Tech Interviews.
Copyright © 2025 Design Gurus, LLC. All rights reserved.