如何使用ftw删除目录的内容

时间:2016-12-02 15:51:15

标签: linux nftw

我们可以使用FTW_DEPTH使用ftw删除非空目录。但是我想删除目录的内容而不是目录本身有些东西类似于rm -rf dir /*.

如何使用nftp / ftw实现此目的?

1 个答案:

答案 0 :(得分:1)

您可以尝试此操作(警告,无需确认):

#include <stdio.h>
#include <ftw.h>
#include <iostream>

using namespace std;
int list(const char *name, const struct stat *status, int type);

int main(int argc, char *argv[])
{
  ftw(argv[1], list, 1);
  return 0;
}

int list(const char *name, const struct stat *status, int type) {
  if(type != FTW_D) {
    cout << "Deleting " << name << endl;
    remove( name );
  }
return 0;
}

并致电您的应用:

  

./ main path_to_delete

相关问题