tags:

views:

105

answers:

4

I want to clean all files in a directory on Linux (not deleteing them, only clear their content)

I need to do it in C.

+2  A: 

You can open the file in write mode and then close it.

codaddict
how i get all the files in the directory in C
nis
+1  A: 

Actually, you really don't need to do it in C. UNIX includes tools that can just about do any task that you want.

find . -type f -exec cp /dev/null {} ';'

That particular snippet above will find all files under the current directory and attempt to copy the null device to it, effectively truncating the file to 0 bytes.

You can change the starting (top level) directory, restrict names (with -name '*.jpg' for example) and even restrict it to the current directory (no subdirectories) with -maxdepth 0.

There are many other options with find that you can discover by entering man find into your command line shell. Just don't enter it into Google, you may get more than you bargained for :-)


If the need to use C is an absolutely non-negotiable one, I would still do it this way but with:

system ("find . -type f -exec cp /dev/null {} ';'");

I'm not keen on re-writing software that someone's already put a bucketload of effort into providing for free :-)


If, after my advice, you still want to do it the hard way, you need to look into opendir, readdir and closedir for processing directories, then just use fopen in write mode followed by fclose on each candidate file.

If you want to navigate whole directory structures rather than just the current directory, you'll have to detect directories from readdir and probably recurse through them.

paxdiablo
yes but my code is in c
nis
Then, seriously, use `system ("find . -type f -exec cp /dev/null {} ';'");`. There's rarely a good reason to re-invent the wheel.
paxdiablo
you can also use `truncate(filename, 0)` instead of opening/closing the file
Hasturkun
Seriously don't use `system()` just because you're in a hurry. Find the cross-platform standard way to accomplish the same thing.
meagar
@meager, the question was tagged linux, hence cross-platform is a YAGNI requirement. In any case, ISO C itself doesn't provide a standard way of doing this but you can turn to POSIX, which is why I gave that as an option.
paxdiablo
+3  A: 
ypnos
btw, you don't need to run echo, `do > $i; done` works just as well
Hasturkun
thx, edited the answer accordingly.
ypnos
+3  A: 
  • scandir to list them, then for every file:
  • fopen(, w+)
  • fstat to get the size
  • fwrite the whole file with zeroes? (this is what you mean by clear?)
  • fclose

A nice shell variant would be: shred -z directory/*

domen
Just `scandir()` followed by a `truncate(filename, 0)` on each file will do the trick.
caf
Well... depends, if you want to make it hard(er) to recover, then writing data in place is sane. Otherwise yes, just `truncate()`, or more standard conforming `fopen(file, "w")+fclose()` will do the trick.
domen
It won't make a difference with the advent of copy-on-write filesystems, though ;)
caf
caf, please explain why COW is relevant here.
domen