LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 08-12-2007, 05:35 AM   #1
nc3b
Member
 
Registered: Aug 2005
Posts: 330

Rep: Reputation: 32
Remove a non-empty directory


Hello. I am trying to programmatically remove a folder (the program is in c/c++). I saw rmdir doesn't work for non-empty directories. Is deleting it's contents first the only solution? Thank you.
 
Old 08-12-2007, 05:44 AM   #2
Hammett
Senior Member
 
Registered: Aug 2003
Location: Barcelona, Catalunya
Distribution: Gentoo
Posts: 1,037

Rep: Reputation: 54
Use "rm -rf <directory>" instead

-r = recursive
-f = force (you won't be asked to delete files)

Hope it helps
 
Old 08-12-2007, 06:05 AM   #3
nc3b
Member
 
Registered: Aug 2005
Posts: 330

Original Poster
Rep: Reputation: 32
Yes, you are right. But I was hoping not to be forced to resort to external commands.. Thank you.

Last edited by nc3b; 08-12-2007 at 08:25 AM.
 
Old 08-12-2007, 06:09 AM   #4
carl0ski
Member
 
Registered: Sep 2004
Location: Melbourne, Victoria Australia
Distribution: Support those that support you :)
Posts: 872
Blog Entries: 12

Rep: Reputation: 30
Quote:
Originally Posted by nc3b
Hello. I am trying to programmatically remove a folder (the program is in c/c++). I saw rmdir doesn't work for non-empty directories. Is deleting it's contents first the only solution? Thank you.
rmdir --ignore-fail-on-non-empty <directory>

will delete a folder even if it is full
 
Old 08-12-2007, 06:44 AM   #5
nc3b
Member
 
Registered: Aug 2005
Posts: 330

Original Poster
Rep: Reputation: 32
) I really think you did not fully understand. I said I wanted to do it programmatically (from c), eg by calling a function. Not by using a command. And since I will have to use external programs, I will use rm..
 
Old 08-12-2007, 06:53 AM   #6
Mega Man X
Guru
 
Registered: Apr 2003
Location: ~
Distribution: Ubuntu, FreeBSD, Solaris, DSL
Posts: 5,339

Rep: Reputation: 63
I don't think most of us understands what you are trying to do. First time I read, you thought that the "rm" command would not work with empty directories. It does. When I read the second time, it looked like you are trying to create a c program that deletes directories. Re-reading everything, makes me think that you want to run a system command from a c program, but you couldn't because you believed that rm could not delete non-empty directories.

So what are you trying to do? If you want to call rm from a C program, something like this should help:

Code:
#include <iostream>
#include <unistd.h>

int main() {
    system("rm -rf <directory>/");
    return 0;
}
I can't compile this now, but I think it should work.

Last edited by Mega Man X; 08-12-2007 at 06:54 AM.
 
Old 08-12-2007, 08:24 AM   #7
nc3b
Member
 
Registered: Aug 2005
Posts: 330

Original Poster
Rep: Reputation: 32
To clear things up. Yes, at first i was looking for some way of deleting the folder, without resorting to external commands (rm, rmdir). When I said I should have liked to use rmdir, I was talking about the function. ( )
So, if possible, I wouldn't have used system, popen or such to call the external program, be it rm or rmdir.

And yes, I know that rm can delete empty directories, where did I state it can't?
 
Old 08-12-2007, 08:29 AM   #8
BlackRabbit
Member
 
Registered: Oct 2003
Distribution: SlackWare 12.0
Posts: 73

Rep: Reputation: 15
Long time since I did some C programming, but if you can't delete a non-empty directory, write a function that goes into the directory, deleting all files in it, and then delete the actual dir.

Should take to many lines to program.

Make it recursive, so dirs inside dirs are also emptied and deleted..
 
Old 08-12-2007, 01:26 PM   #9
nc3b
Member
 
Registered: Aug 2005
Posts: 330

Original Poster
Rep: Reputation: 32
Yes BlackRabbit, I will try to write it in the future, although the performance could be an issue.. That folder could contain many many folders which in their turn.. You get what I mean. Thank you for your help.
 
Old 08-12-2007, 03:03 PM   #10
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,524

Rep: Reputation: 93
Yes, if you want to code "rm -r" yourself in C, you really need to dive into the directory-tree, and delete all files at the bottom, then crawl up and remove the just-emptied directories.

For a code example to do this recursively see this forum thread

Or have a look at the srouces of "rm" of course.

P.S. IMHO the question was crystal clear from the first post.

Last edited by Hko; 08-12-2007 at 03:04 PM.
 
Old 08-13-2007, 04:14 AM   #11
nc3b
Member
 
Registered: Aug 2005
Posts: 330

Original Poster
Rep: Reputation: 32
Thumbs up

Yup Hko, your code in the other thread looks pretty good . Guess I should have searched in the forum before asking the question
Thank you.
 
Old 08-13-2007, 04:32 PM   #12
BlackRabbit
Member
 
Registered: Oct 2003
Distribution: SlackWare 12.0
Posts: 73

Rep: Reputation: 15
Quote:
Originally Posted by nc3b View Post
Yes BlackRabbit, I will try to write it in the future, although the performance could be an issue.. That folder could contain many many folders which in their turn.. You get what I mean. Thank you for your help.
Performance shouldn't be an issue really.
 
Old 08-14-2007, 12:57 AM   #13
JoeyAdams
Member
 
Registered: Jun 2006
Distribution: Kubuntu Hardy
Posts: 94

Rep: Reputation: 15
The problem with using this sort of thing:

Code:
#include <iostream>
#include <unistd.h>

int main() {
    system("rm -rf <directory>/");
    return 0;
}
is the huge possibility of an injection exploit. What if someone tries to delete a folder (in the local directory) called "" ? You could change it to system("rm -rf ./<directory>/"); to prevent it from deleting the main directory, but it's still pretty easy to exploit (suppose <directory> is set to ' `rm -rf /` ' ). Just a thought.
 
Old 08-14-2007, 03:34 AM   #14
nc3b
Member
 
Registered: Aug 2005
Posts: 330

Original Poster
Rep: Reputation: 32
I knew there was something fundamentally wrong with using external commands. Reading from other posts, I have always learned to prefer a "c" way instead of an external program.

Now I know what to say when people advise me to use external commands (it so happens that in my program the string with the directory to delete comes from a client through the network, so the possibility of an exploit would be even greater).
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
rm: cannot remove directory `ff': Directory not empty blancs Linux - General 17 08-29-2011 01:47 PM
remove a non empty directory in shell acidjuice Slackware 10 06-28-2011 12:02 PM
Command to remove a non-empty directory ? wlaw Linux - General 6 10-23-2006 10:02 AM
How do you remove a non empty directory aaronruss Linux - Newbie 3 06-05-2004 08:26 PM
RMDIR - how to remove a non-empty directory?? jacksmash Linux - Newbie 15 11-23-2003 12:51 PM


All times are GMT -5. The time now is 05:12 AM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration