Removing all files inside a directory in Linux using an API call from a C Code - Unix
This is a discussion on Removing all files inside a directory in Linux using an API call from a C Code - Unix ; 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 ...

- Forum
- OS Forums
- Unix
- Removing all files inside a directory in Linux using an API call from a C Code
-
Removing all files inside a directory in Linux using an API call from a C Code
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
-
Re: Removing all files inside a directory in Linux using an API call from a C Code
"Aditya" schrieb im Newsbeitrag
news:1193648196.858207.94110@i38g2000prf.googlegro ups.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
-
Re: Removing all files inside a directory in Linux using an API call from a C Code
- 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_/_
-
Re: Removing all files inside a directory in Linux using an API call from a C Code
Joachim Schmitz wrote:
> "Aditya" schrieb im Newsbeitrag
> news:1193648196.858207.94110@i38g2000prf.googlegro ups.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);
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().
-
Re: Removing all files inside a directory in Linux using an API call from a C Code
On Oct 29, 2:18 pm, santosh wrote:
> Joachim Schmitz wrote:
> > "Aditya" schrieb im Newsbeitrag
> >news:1193648196.858207.94110@i38g2000prf.googlegro ups.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);
>
> 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.
-
Re: Removing all files inside a directory in Linux using an API call from a C Code
Aditya writes:
> On Oct 29, 2:18 pm, santosh wrote:
> > Joachim Schmitz wrote:
> > > "Aditya" schrieb im Newsbeitrag
> > >news:1193648196.858207.94110@i38g2000prf.googlegro ups.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);
> >
> > 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.
I did. you did not care to read my answer, did you?
--
| Maurizio Loreti -- ROT13:ybergv@cq.vasa.vg | (@_
| Un. of Padova, Dept. of Physics, Padova, Italy | //\
| http://www.pd.infn.it/~loreti/mlo.html | V_/_
-
Re: Removing all files inside a directory in Linux using an API call from a C Code
"Maurizio Loreti" schrieb im Newsbeitrag
news:rmzly2ci2c.fsf@mlinux.pd.infn.it...
> Aditya writes:
>
>> On Oct 29, 2:18 pm, santosh wrote:
>> > Joachim Schmitz wrote:
>> > > "Aditya" schrieb im Newsbeitrag
>> > >news:1193648196.858207.94110@i38g2000prf.googlegro ups.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);
>> >
>> > 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.
>
> I did. you did not care to read my answer, did you?
A sade quick in plonking, aren't you?
-
Re: Removing all files inside a directory in Linux using an API call from a C Code
"Joachim Schmitz" schrieb im Newsbeitrag
news:fg4f7f$1oj$1@online.de...
> "Maurizio Loreti" schrieb im Newsbeitrag
> news:rmzly2ci2c.fsf@mlinux.pd.infn.it...
>> Aditya writes:
>>
>>> On Oct 29, 2:18 pm, santosh wrote:
>>> > Joachim Schmitz wrote:
>>> > > "Aditya" schrieb im Newsbeitrag
>>> > >news:1193648196.858207.94110@i38g2000prf.googlegro ups.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);
>>> >
>>> > 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.
>>
>> I did. you did not care to read my answer, did you?
> A sade quick in plonking, aren't you?
"shade" what what I meant to write...
Bye, Jojo
-
Re: Removing all files inside a directory in Linux using an API call from a C Code
Aditya wrote:
> On Oct 29, 2:18 pm, santosh wrote:
>> Joachim Schmitz wrote:
>> > "Aditya" schrieb im Newsbeitrag
>> >news:1193648196.858207.94110@i38g2000prf.googlegro ups.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);
>>
>> 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 -
> 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().
-
Re: Removing all files inside a directory in Linux using an API call from a C Code
Joachim Schmitz wrote:
>
> "Joachim Schmitz" schrieb im
> Newsbeitrag news:fg4f7f$1oj$1@online.de...
>> "Maurizio Loreti" schrieb im Newsbeitrag
>> news:rmzly2ci2c.fsf@mlinux.pd.infn.it...
>>> Aditya writes:
>>>
>>>> On Oct 29, 2:18 pm, santosh wrote:
>>>> > Joachim Schmitz wrote:
>>>> > > "Aditya" schrieb im Newsbeitrag
>>>> > >news:1193648196.858207.94110@i38g2000prf.googlegro ups.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);
>>>> >
>>>> > 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.
>>>
>>> I did. you did not care to read my answer, did you?
>> A sade quick in plonking, aren't you?
> "shade" what what I meant to write...
^^^^
s/what/was

-
Re: Removing all files inside a directory in Linux using an API call from a C Code
"santosh" schrieb im Newsbeitrag
news:fg4j4d$gu$2@aioe.org...
> Joachim Schmitz wrote:
>
>>
>> "Joachim Schmitz" schrieb im
>> Newsbeitrag news:fg4f7f$1oj$1@online.de...
>>> "Maurizio Loreti" schrieb im Newsbeitrag
>>> news:rmzly2ci2c.fsf@mlinux.pd.infn.it...
>>>> Aditya writes:
>>>>
>>>>> On Oct 29, 2:18 pm, santosh wrote:
>>>>> > Joachim Schmitz wrote:
>>>>> > > "Aditya" schrieb im Newsbeitrag
>>>>> > >news:1193648196.858207.94110@i38g2000prf.googlegro ups.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);
>>>>> >
>>>>> > 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.
>>>>
>>>> I did. you did not care to read my answer, did you?
>>> A sade quick in plonking, aren't you?
>> "shade" what what I meant to write...
> ^^^^
> s/what/was
>
> 
indeed. Fingers faster than brain
Bye, Jojo
-
Re: Removing all files inside a directory in Linux using an API call from a C Code
"santosh" schrieb im Newsbeitrag
news:fg4j30$gu$1@aioe.org...
> Aditya wrote:
>
>> On Oct 29, 2:18 pm, santosh wrote:
>> 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.
system call vs. calling system()
Guess he meant the later
Bye, Jojo
-
Re: Removing all files inside a directory in Linux using an API call from a C Code
On Oct 29, 10:22 pm, "Joachim Schmitz"
digital.de> wrote:
> "santosh" schrieb im Newsbeitragnews:fg4j30$gu$1@aioe.org...> Aditya wrote:
>
> >> On Oct 29, 2:18 pm, santosh wrote:
> >> 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.
>
> 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
-
Re: Removing all files inside a directory in Linux using an API call from a C Code
On Oct 30, 10:44 am, Aditya 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.
-
Re: Removing all files inside a directory in Linux using an API call from a C Code
On Oct 31, 4:16 am, fjbl...@yahoo.com wrote:
> On Oct 30, 10:44 am, Aditya 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.
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.