I have to remove few hundreds of files inside my C code. I use “remove” in a loop. Is there any faster way to do it than using “remove”? I ask this because I can’t give wildchars using “remove”.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
No, there isn’t a quicker way than using
remove()– orunlink()on POSIX systems – in a loop.The system
rmcommand does that too – at least in the simple, non-recursive case where the names are given on the command line. The shell expands the metacharacters, andrm(in)famously goes along deleting what it was told to delete, unaware of the disastrous*.*notation that was used on the command line. (In the recursive case, it uses a function such asnftw()to traverse the directory structure in depth-first order and repeated calls tounlink()to remove the files andrmdir()to remove the (now-empty) directories.)POSIX does provide functions (
glob()andwordexp()) to generate lists of file names from metacharacters as used in the (POSIX) shell, plusfnmatch()to see whether a name matches a pattern.