Build a Simple File Explorer in C (Step-by-Step)
                    When learning C programming, many beginners stick to small examples like calculators or guessing games. But if you really want to understand the power of C, you need to dive into system-level programming.
In this tutorial, we’ll build a File Explorer in C — a console-based program that lets you:
List files and directories
Identify whether an item is a file or folder
Navigate inside directories (
cd)Exit when you’re done
By the end, you’ll not only have a working project but also a deeper understanding of C system programming and how operating systems handle files.
🔹 Why Build a File Explorer in C?
Before jumping into code, let’s understand why this project is useful.
System Programming Practice – You’ll work directly with the file system using C libraries.
Learn Important APIs – Functions like
opendir,readdir,stat, andchdirare used in real-world system tools.Mini Shell Foundation – File explorers are the foundation of command-line shells like Bash and Zsh.
Real-World Skills – Developers working on Linux, embedded systems, or OS development often rely on these techniques.
This makes it a perfect C programming project for students and beginners looking to go beyond theory.
🔹 Prerequisites
To follow along, you should:
Know the basics of C (variables, loops, functions).
Be using Linux or macOS (Windows can work but may require
dirent.halternatives likewindows.h).Have a working C compiler (GCC or Clang).
🔹 Understanding the Libraries
We’ll use a few key libraries in this project:
<dirent.h>→ Allows reading directories (opendir,readdir,closedir).<sys/stat.h>→ Provides file information (stat) to check if something is a file or directory.<unistd.h>→ Lets us change directories (chdir) and get the current path (getcwd).<stdio.h>&<string.h>→ For input/output and string handling.
These libraries give us direct access to the operating system’s file system.
🔹 Step-by-Step Code
Here’s the complete implementation of our File Explorer in C:
#include <stdio.h>
#include <dirent.h>
#include <sys/stat.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
void listFiles(const char *path) {
    struct dirent *entry;
    struct stat fileStat;
    DIR *dir = opendir(path);
    if (!dir) {
        perror("opendir");
        return;
    }
    printf("\nContents of %s:\n", path);
    while ((entry = readdir(dir)) != NULL) {
        char fullPath[512];
        snprintf(fullPath, sizeof(fullPath), "%s/%s", path, entry->d_name);
        if (stat(fullPath, &fileStat) == 0) {
            if (S_ISDIR(fileStat.st_mode)) {
                printf("[DIR]  %s\n", entry->d_name);
            } else {
                printf("[FILE] %s\n", entry->d_name);
            }
        }
    }
    closedir(dir);
}
int main() {
    char path[512] = ".";
    char input[256];
    while (1) {
        listFiles(path);
        printf("\nOptions:\n");
        printf("cd <dir>  - Change directory\n");
        printf("exit      - Quit program\n");
        printf("Enter command: ");
        fgets(input, sizeof(input), stdin);
        input[strcspn(input, "\n")] = 0;
        if (strncmp(input, "cd ", 3) == 0) {
            char newPath[512];
            snprintf(newPath, sizeof(newPath), "%s/%s", path, input + 3);
            if (chdir(newPath) == 0) {
                getcwd(path, sizeof(path));
            } else {
                perror("chdir");
            }
        } else if (strcmp(input, "exit") == 0) {
            printf("👋 Exiting File Explorer.\n");
            break;
        } else {
            printf("❌ Unknown command!\n");
        }
    }
    return 0;
}
        🔹 Code Explanation
Let’s break it down:
listFiles()FunctionUses
opendir()to open a directory.Reads entries using
readdir().Uses
stat()to check if an entry is a file or directory.Prints
[DIR]or[FILE]accordingly.
Main Loop
Starts at the current directory (
".").Lists files in the current folder.
Waits for user input.
Supported Commands
cd <dirname>→ Moves into the given folder.exit→ Closes the program.
Navigation
Uses
chdir()to switch directories.Updates the path with
getcwd().
This is a basic shell-like program where you can explore your file system from the terminal.
🔹 Sample Output
Contents of .:
[DIR]  .
[DIR]  ..
[FILE] main.c
[DIR]  src
[FILE] README.md
Options:
cd <dir>  - Change directory
exit      - Quit program
Enter command: cd src
Contents of ./src:
[DIR]  .
[DIR]  ..
[FILE] utils.c
[FILE] utils.h