方案一:递归删除文件夹

在 Linux 上使用 C++ 删除文件夹的代码可以使用 POSIX 标准库的 <dirent.h> 头文件和系统调用的 rmdir() 和 remove() 函数。以下是一个简单的例子:

#include <iostream>
#include <dirent.h>
#include <unistd.h>
#include <string.h>
// 递归删除文件夹
bool removeDirectory(const char *path) {
    DIR *dir = opendir(path);
    if (dir == nullptr) {
        // 打开目录失败
        return false;
    dirent *entry;
    while ((entry = readdir(dir)) != nullptr) {
        if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) {
            // 跳过当前目录和父目录
            continue;
        // 构造文件/文件夹的完整路径
        std::string fullPath = std::string(path) + "/" + entry->d_name;
        if (entry->d_type == DT_DIR) {
            // 如果是文件夹,递归删除
            if (!removeDirectory(fullPath.c_str())) {
                closedir(dir);
                return false;
        } else {
            // 如果是文件,直接删除
            if (remove(fullPath.c_str()) != 0) {
                closedir(dir);
                return false;
    // 关闭目录流
    closedir(dir);
    // 删除当前目录
    if (rmdir(path) != 0) {
        return false;
    return true;
int main() {
    const char *path = "/path/to/directory";
    if (removeDirectory(path)) {
        std::cout << "成功删除文件夹:" << path << std::endl;
    } else {
        std::cout << "删除文件夹失败:" << path << std::endl;
    return 0;

请注意,这只是一个简单的例子,实际应用中可能需要更加复杂的错误处理和安全性检查,例如检查文件夹是否存在、是否有删除权限等。使用文件删除操作时请谨慎,并在生产环境中进行充分测试。

方案二:调用系统命令

c++版本

可以使用 C++ 的 system() 函数来调用系统命令来删除文件夹。以下是一个使用 system() 函数调用 Linux 的 rm 命令删除文件夹的简单示例:

#include <iostream>
#include <cstdlib>
int main() {
    const char *path = "/path/to/directory";
    std::string command = "rm -r " + std::string(path);  // 构造删除命令
    int result = std::system(command.c_str());  // 调用系统命令
    if (result == 0) {
        std::cout << "成功删除文件夹:" << path << std::endl;
    } else {
        std::cout << "删除文件夹失败:" << path << std::endl;
    return 0;

这里使用了 -r 选项来指定 rm 命令以递归方式删除文件夹。需要注意的是,在使用 system() 函数时要小心输入的命令,确保命令参数的安全性,避免潜在的安全风险,比如命令注入攻击。建议在使用 system() 函数时谨慎处理用户输入的数据,并进行充分的错误处理和安全性检查。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
    const char *path = "/path/to/directory";
    char command[256];  // 命令缓冲区
    snprintf(command, sizeof(command), "rm -r %s", path);  // 构造删除命令
    int result = system(command);  // 调用系统命令
    if (result == 0) {
        printf("成功删除文件夹:%s\n", path);
    } else {
        printf("删除文件夹失败:%s\n", path);
    return 0;

其他:判断文件夹是否存在

#include <stdio.h>
#include <unistd.h>
int directoryExists(const char* path) {
    if (access(path, F_OK) != -1) {
        return 1;
    } else {
        return 0;
int main() {
    const char* directoryPath = "/path/to/directory";  // 需要判断的文件夹路径
    if (directoryExists(directoryPath)) {
        printf("文件夹存在\n");
    } else {
        printf("文件夹不存在\n");
    return 0;
unlink函数删除文件系统中的一个名字,如果这个名字是该文件的最后一个link并且该文件没有被任何进程打开,那么删除文件。否则等到文件被关闭或最后一个link被删除删除文件并释放空间。
#include <uni...
				
linux删除目录很简单,很多人还是习惯用rmdir,不过一旦目录非空,就陷入深深的苦恼之中,现在使用rm -rf命令即可。 直接rm就可以了,不过要加两个参数-rf 即:rm -rf 目录名字 -r 就是向下递归,不管有多少级目录,一并删除 -f 就是直接强行删除,不作任何提示的意思 删除文件夹实例: rm -rf /var/log/httpd/access 将会删除/var/log/httpd/access目录以及其下所有文件文件夹 删除文件使用实例: rm -f /var/log/httpd/access.log 将会强制删除/var/log/httpd/access.log
在管理系统日志的时候常常需要清空已有的日志来杀死进程,但是如何engine_pids中的数量极为庞大的话,那么单个文件类似rm filename就不那么合理了,需要一种可以直接删除日志文件目录下面的所有进程信息但是却不删除文件夹本身,首先简单贴一下rm命令的用法: 删除文件夹【rm】   一、rm命令使用权限     所有用户都可以在终端使用 rm命令删除目录。
1 int removeDir(string dirPath) 3 struct _finddata_t fb; //find the storage structure of the same properties file. 4 string path; 5 long handle; 6 int resu...