c++clinuxunix

How to delete all files in a folder, but not delete the folder using NIX standard libraries?


I am trying to create a program that deletes the contents of the /tmp folder, I am using C/C++ on linux.

system("exec rm -r /tmp")

deletes everything in the folder but it deletes the folder too which I dont want.

Is there any way to do this by some sort of bash script, called via system(); or is there a direct way i can do this in C/C++?

My question is similar to this one, but im not on OS X... how to delete all files in a folder, but not the folder itself?


Solution

  • In C/C++, you could do:

    system("exec rm -r /tmp/*")
    

    In Bash, you could do:

    rm -r /tmp/*
    

    This will delete everything inside /tmp, but not /tmp itself.