UNIX command rm -rf in C

Posted on 2007-03-18
Last Modified: 2010-05-18
How can you implement the standard UNIX command "rm -rf" in C, in order to remove an entire directory tree and all files inside the tree?

I realize I could just do:

system("rm -rf somedir");

But usually there are corresponding C lib functions for every UNIX command.  For example, there is an mkdir() function in C.  But the rmdir() function in C only takes a single argument, (the path), and fails if the specified directory is not empty.  So is there a way to get the functionality of rm -rf using the C library?
Question by:chsalvia
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 53

Expert Comment

ID: 18744996
Well, basically, you just remove all files within the directory, and then remove the directory. All of that recursively. So, pseudo code :

    function remove_dir(directory)
        for each file in remove_dir
        end for
        for each dir in remove_dir
            remove_dir(dir)                          <----- recursive call
        end for
    end function
LVL 53

Accepted Solution

Infinity08 earned 125 total points
ID: 18744997
Should have been this of course :

    function remove_dir(directory)
        for each file in directory
        end for
        for each dir in directory
            remove_dir(dir)                          <----- recursive call
        end for
    end function
LVL 24

Assisted Solution

slyong earned 125 total points
ID: 18745099
You can have a read at the actual code for rm implementation here

Featured Post

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
trouble on installing syslog-ng on CentOS 7 7 101
C qsort compare function issue 6 44
plsql job on oracle 18 78
Advice on ESXi 5.1 Health / Storage 1 48
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode ( They will have you believe that Unicode requires you to use…
Join Greg Farro and Ethan Banks from Packet Pushers ( and Greg Ross from Paessler ( for a discussion about smart network …
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question