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
Solved

UNIX command rm -rf in C

Posted on 2007-03-18
3
1,568 Views
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?
0
Comment
Question by:chsalvia
  • 2
3 Comments
 
LVL 53

Expert Comment

by:Infinity08
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
            remove_file(file)
        end for
        for each dir in remove_dir
            remove_dir(dir)                          <----- recursive call
        end for
        remove_file(directory)
    end function
0
 
LVL 53

Accepted Solution

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

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

Assisted Solution

by:slyong
slyong earned 125 total points
ID: 18745099
You can have a read at the actual code for rm implementation here http://www.koders.com/c/fidD3021906AECAFB50B537CC4CEC66C335636DF6E1.aspx?s=bash
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Unix, date minus 1, 2 or 3 days (last working day) 11 67
centos linux 65 155
android secure ftp 3 50
How to mount nfs share on this CentOS server? 6 42
Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

856 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