我正在尝试创建一个程序,用于删除Linux系统上 /tmp 文件夹中的内容。我的程序使用C/C++语言编写。
system("exec rm -r /tmp")
删除文件夹中的所有内容,但不删除文件夹本身。
是否有一种通过某种bash脚本调用system()
的方式来实现此功能;或者是否有一种在C/C ++中可以直接完成的方法?
我的问题类似于这个问题,但我不在OS X上... 如何删除文件夹中的所有文件,但不删除文件夹本身?
我正在尝试创建一个程序,用于删除Linux系统上 /tmp 文件夹中的内容。我的程序使用C/C++语言编写。
system("exec rm -r /tmp")
删除文件夹中的所有内容,但不删除文件夹本身。
是否有一种通过某种bash脚本调用system()
的方式来实现此功能;或者是否有一种在C/C ++中可以直接完成的方法?
我的问题类似于这个问题,但我不在OS X上... 如何删除文件夹中的所有文件,但不删除文件夹本身?
#include <stdio.h>
#include <dirent.h>
int main()
{
// These are data types defined in the "dirent" header
DIR *theFolder = opendir("path/of/folder");
struct dirent *next_file;
char filepath[256];
while ( (next_file = readdir(theFolder)) != NULL )
{
// build the path for each file in the folder
sprintf(filepath, "%s/%s", "path/of/folder", next_file->d_name);
remove(filepath);
}
closedir(theFolder);
return 0;
}
您不希望通过system()
或类似方法生成新的shell - 这会产生很大的开销,却只是为了执行一些非常简单的操作,并且假设(和依赖)系统上可用的内容不必要。
if (0==strcmp(next_file->d_name, ".") || 0==strcmp(next_file->d_name, "..")) { continue; }
。 - Daniel在C/C++中,您可以这样做:
system("exec rm -r /tmp/*")
在Bash中,你可以这样做:rm -r /tmp/*
这将删除/tmp内的所有内容,但不会删除/tmp目录本身。
system
不太好用,但我的回答只是在扩展原帖的问题,很明显是在使用system
的情况下。您应该将您的观点发布为对原问题的反馈。 - Jay Sullivan*
,您可以删除所有类型的文件。
system("exec rm -r /tmp/*")
system("exec find /tmp -mindepth 1 -exec rm {} ';'");
在C/C++中,您可以使用(包括隐藏目录):
system("rm -r /tmp/* /tmp/.*");
system("find /tmp -mindepth 1 -delete");
但是如果在sh中没有“rm”或“find”实用程序,最好使用“ftw”和“remove”:
#define _XOPEN_SOURCE 500
#include <ftw.h>
static int remove_cb(const char *fpath, const struct stat *sb, int typeFlag, struct FTW *ftwbuf)
{
if (ftwbuf->level)
remove(fpath);
return 0;
}
int main(void)
{
nftw("./dir", remove_cb, 10, FTW_DEPTH);
return 0;
}
我知道这是一个很老的问题,但在Demitri的优秀答案基础上,我创建了一个函数,如果需要,将递归删除子文件夹中的文件
它还进行了一些错误处理,以便传回errno。函数头是为doxygen解析而编写的。这个函数在我使用的简单示例情况下可以工作,并删除隐藏文件夹和隐藏文件。
我希望这能帮助未来的某个人
#include <stdio.h>
#include <dirent.h>
#include <sys/stat.h>
#define SUCCESS_STAT 0
/**
* checks if a specific directory exists
* @param dir_path the path to check
* @return if the path exists
*/
bool dirExists(std::string dir_path)
{
struct stat sb;
if (stat(dir_path.c_str(), &sb) == 0 && S_ISDIR(sb.st_mode))
return true;
else
return false;
}
/**
* deletes all the files in a folder (but not the folder itself). optionally
* this can traverse subfolders and delete all contents when recursive is true
* @param dirpath the directory to delete the contents of (can be full or
* relative path)
* @param recursive true = delete all files/folders in all subfolders
* false = delete only files in toplevel dir
* @return SUCCESS_STAT on success
* errno on failure, values can be from unlink or rmdir
* @note this does NOT delete the named directory, only its contents
*/
int DeleteFilesInDirectory(std::string dirpath, bool recursive)
{
if (dirpath.empty())
return SUCCESS_STAT;
DIR *theFolder = opendir(dirpath.c_str());
struct dirent *next_file;
char filepath[1024];
int ret_val;
if (theFolder == NULL)
return errno;
while ( (next_file = readdir(theFolder)) != NULL )
{
// build the path for each file in the folder
sprintf(filepath, "%s/%s", dirpath.c_str(), next_file->d_name);
//we don't want to process the pointer to "this" or "parent" directory
if ((strcmp(next_file->d_name,"..") == 0) ||
(strcmp(next_file->d_name,"." ) == 0) )
{
continue;
}
//dirExists will check if the "filepath" is a directory
if (dirExists(filepath))
{
if (!recursive)
//if we aren't recursively deleting in subfolders, skip this dir
continue;
ret_val = DeleteFilesInDirectory(filepath, recursive);
if (ret_val != SUCCESS_STAT)
{
closedir(theFolder);
return ret_val;
}
}
ret_val = remove(filepath);
//ENOENT occurs when i folder is empty, or is a dangling link, in
//which case we will say it was a success because the file is gone
if (ret_val != SUCCESS_STAT && ret_val != ENOENT)
{
closedir(theFolder);
return ret_val;
}
}
closedir(theFolder);
return SUCCESS_STAT;
}
从C++17开始,您可以使用std::filesystem。下面的代码将使用directory_iterator列出目录中的所有文件和子目录,并调用remove_all来删除它们:
#include <filesystem>
namespace fs = std::filesystem;
void delete_dir_content(const fs::path& dir_path) {
for (auto& path: fs::directory_iterator(dir_path)) {
fs::remove_all(path);
}
}
filesystem_error
异常。您可以通过以下方式避免这种情况:void delete_dir_content(const fs::path& dir_path) {
for (auto& path: fs::directory_iterator(dir_path)) {
std::error_code err;
std::uintmax_t n = fs::remove_all(path, err);
if (static_cast<std::uintmax_t>(-1) == n) {
std::cout << "Failed to remove_all(" << path << ") with error: " << err.message() << std::endl;
}
}
}
rm -r /tmp/*
将会删除该文件夹中的所有内容。 - AusCBlokeboost::filesystem
来遍历并删除每一个文件。 - chris*
扩展到的文件),但不会删除任何以点号.
开头的文件。 - David Rodríguez - dribeas