|
Home > Archive > Unix Programming > October 2007 > Removing all files inside a directory in Linux using an API call from a C Code
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Removing all files inside a directory in Linux using an API call from a C Code
|
|
| Aditya 2007-10-29, 7:32 am |
| Hi
I have several files inside a directory. I am trying to remove files
through a C program. I want to remove all those, including the
directory. I tried using remove() function call, but that doesn't help
because the parameter needs to be the absolute file path including
filename. And I want to delete all the files, the number of which
varies everytime that directory is created.
Please, help me out with this.
Thanks
Aditya
| |
| Joachim Schmitz 2007-10-29, 7:32 am |
| "Aditya" <adityagupta.18@gmail.com> schrieb im Newsbeitrag
news:1193648196.858207.94110@i38g2000prf.googlegroups.com...
> Hi
> I have several files inside a directory. I am trying to remove files
> through a C program. I want to remove all those, including the
> directory. I tried using remove() function call, but that doesn't help
> because the parameter needs to be the absolute file path including
> filename. And I want to delete all the files, the number of which
> varies everytime that directory is created.
> Please, help me out with this.
Get yourself a copy of the source code for rm and check what it does for
'rm -r',
or use something along the lines of
sprintf(cmd, "rm -rf %s", targ);
system(cmd);
Bye, Jojo
| |
| Maurizio Loreti 2007-10-29, 7:32 am |
| - open and read the directory (man opendir, man readdir);
- remove all files one by one (man unlink);
- remove all subdirectories recursing the procedure;
- close and remove the directory (man closedir, man rmdir).
--
| Maurizio Loreti -- ROT13:ybergv@cq.vasa.vg | (@_
| Un. of Padova, Dept. of Physics, Padova, Italy | //\
| http://www.pd.infn.it/~loreti/mlo.html | V_/_
| |
| santosh 2007-10-29, 7:32 am |
| Joachim Schmitz wrote:
> "Aditya" <adityagupta.18@gmail.com> schrieb im Newsbeitrag
> news:1193648196.858207.94110@i38g2000prf.googlegroups.com...
> Get yourself a copy of the source code for rm and check what it does
> for 'rm -r',
> or use something along the lines of
>
> sprintf(cmd, "rm -rf %s", targ);
> system(cmd);
If this is an assignment or a learning exercise, using system() defeats
the purpose. The OP should rather investigate the APIs provided by his
system for directory management, specifically opendir(), readdir(),
closedir(), unlink()/remove().
| |
| Aditya 2007-10-29, 7:32 am |
| On Oct 29, 2:18 pm, santosh <santosh....@gmail.com> wrote:
> Joachim Schmitz wrote:
>
>
> If this is an assignment or a learning exercise, using system() defeats
> the purpose. The OP should rather investigate the APIs provided by his
> system for directory management, specifically opendir(), readdir(),
> closedir(), unlink()/remove().- Hide quoted text -
>
> - Show quoted text -
I want to specify one more thing!! I don't know the filenames inside
the directory. Neither do I want to use any shell command. I also
don't want to use system call, because using system calls I will be
using a lot of resources transferring control to the shell. So, please
now suggest me something.
| |
| Maurizio Loreti 2007-10-29, 7:32 am |
| Aditya <adityagupta.18@gmail.com> writes:
> On Oct 29, 2:18 pm, santosh <santosh....@gmail.com> wrote:
>
>
>
> I want to specify one more thing!! I don't know the filenames inside
> the directory. Neither do I want to use any shell command. I also
> don't want to use system call, because using system calls I will be
> using a lot of resources transferring control to the shell. So, please
> now suggest me something.
I did. you did not care to read my answer, did you? <plonk>
--
| Maurizio Loreti -- ROT13:ybergv@cq.vasa.vg | (@_
| Un. of Padova, Dept. of Physics, Padova, Italy | //\
| http://www.pd.infn.it/~loreti/mlo.html | V_/_
| |
| Joachim Schmitz 2007-10-29, 7:32 am |
| "Maurizio Loreti" <mlo@foobar.it> schrieb im Newsbeitrag
news:rmzly2ci2c.fsf@mlinux.pd.infn.it...
> Aditya <adityagupta.18@gmail.com> writes:
>
>
> I did. you did not care to read my answer, did you? <plonk>
A sade quick in plonking, aren't you?
| |
| Joachim Schmitz 2007-10-29, 7:32 am |
|
"Joachim Schmitz" <nospam.jojo@schmitz-digital.de> schrieb im Newsbeitrag
news:fg4f7f$1oj$1@online.de...
> "Maurizio Loreti" <mlo@foobar.it> schrieb im Newsbeitrag
> news:rmzly2ci2c.fsf@mlinux.pd.infn.it...
> A sade quick in plonking, aren't you?
"shade" what what I meant to write...
Bye, Jojo
| |
| santosh 2007-10-29, 7:32 am |
| Aditya wrote:
> On Oct 29, 2:18 pm, santosh <santosh....@gmail.com> wrote:
[vbcol=seagreen]
> I want to specify one more thing!! I don't know the filenames inside
> the directory.
Of course you don't. That's what readdir() is for.
> Neither do I want to use any shell command.
OK.
> I also don't want to use system call,
What you want to do is inherently impossible to do without system calls,
unless you don't mind writing a kernel module.
> because using system calls I will be using a lot of resources
> transferring control to the shell.
Where have you got this stupid misconception from. System calls exist to
provide services and to get called. You won't be using more resources
than needed, because the system routines are written by programmers who
know what they are doing.
> So, please now suggest me something.
Like I said, open the directory file with opendir(), read it's contents
with readdir() and remove it's files with unlink() or remove(). After
you done this, close the directory with closedir().
| |
| santosh 2007-10-29, 7:32 am |
| Joachim Schmitz wrote:
>
> "Joachim Schmitz" <nospam.jojo@schmitz-digital.de> schrieb im
> Newsbeitrag news:fg4f7f$1oj$1@online.de...
> "shade" what what I meant to write...
^^^^
s/what/was

| |
| Joachim Schmitz 2007-10-29, 1:23 pm |
|
"santosh" <santosh.k83@gmail.com> schrieb im Newsbeitrag
news:fg4j4d$gu$2@aioe.org...
> Joachim Schmitz wrote:
>
> ^^^^
> s/what/was
>
> 
indeed. Fingers faster than brain
Bye, Jojo
| |
| Joachim Schmitz 2007-10-29, 1:23 pm |
| "santosh" <santosh.k83@gmail.com> schrieb im Newsbeitrag
news:fg4j30$gu$1@aioe.org...
> Aditya wrote:
>
>
> What you want to do is inherently impossible to do without system calls,
> unless you don't mind writing a kernel module.
system call vs. calling system()
Guess he meant the later
Bye, Jojo
| |
| Aditya 2007-10-30, 1:25 pm |
| On Oct 29, 10:22 pm, "Joachim Schmitz" <nospam.j...@schmitz-
digital.de> wrote:
> "santosh" <santosh....@gmail.com> schrieb im Newsbeitragnews:fg4j30$gu$1@aioe.org...> Aditya wrote:
>
>
>
> system call vs. calling system()
> Guess he meant the later
>
> Bye, Jojo
Thanks Jojo
and thanks Santosh
I read both of you. I have already tried this and I actually don't
know what the dirent->d_name structure contains. It has the complete
listing of the files inside a directory, all padded with junk
data(most of them being NULL). First is how to extract the filenames
from this. Second the dirent->d_type always returns DT_DIR(==4),
irrespective of a subfolder existence. I am stuck here. I am also
unable to find a good tutorial on this. Please help out again.
bye
Aditya
| |
| fjblurt@yahoo.com 2007-10-30, 7:22 pm |
| On Oct 30, 10:44 am, Aditya <adityagupta...@gmail.com> wrote:
> Thanks Jojo
> and thanks Santosh
> I read both of you. I have already tried this and I actually don't
> know what the dirent->d_name structure contains. It has the complete
> listing of the files inside a directory, all padded with junk
> data(most of them being NULL). First is how to extract the filenames
> from this.
It's a null-terminated string containing the name of a single file.
Anything following the first null byte is undefined and should be
ignored. To get the next filename, you call readdir() again.
> Second the dirent->d_type always returns DT_DIR(==4),
> irrespective of a subfolder existence.
I think you're doing something wrong. Could you post your code,
ideally in a stripped-down example that can be compiled and run?
> I am stuck here. I am also
> unable to find a good tutorial on this. Please help out again.
Section 4.21 of Stevens' _Advanced programming in the Unix
Environment_ seems like a good bet. It's a book every Unix programmer
ought to have.
| |
| Aditya 2007-10-31, 7:38 am |
| On Oct 31, 4:16 am, fjbl...@yahoo.com wrote:
> On Oct 30, 10:44 am, Aditya <adityagupta...@gmail.com> wrote:
>
>
> It's a null-terminated string containing the name of a single file.
> Anything following the first null byte is undefined and should be
> ignored. To get the next filename, you call readdir() again.
>
>
> I think you're doing something wrong. Could you post your code,
> ideally in a stripped-down example that can be compiled and run?
>
>
> Section 4.21 of Stevens' _Advanced programming in the Unix
> Environment_ seems like a good bet. It's a book every Unix programmer
> ought to have.
Thanks
I have got it by now!! The problem was that the first element was a
"." and hence the search started again from the root and I was lost
there. Now its all fixed.
Thanks for the suggested book.
|
|
|
|
|