linux 文件管理

简介: 获取linux某个磁盘的剩余容量,可以根据目录或文件的日期去删除文件

1、查看磁盘剩余容量

int get_free_memory(const char* path, uint64_t* total, uint64_t* free)
{
   
    struct statfs diskInfo;
    int ret = statfs(path, &diskInfo);
    if (ret < 0)
    {
   
        printf("statfs = -1\r\n");
        return -1;
    }
    unsigned long long blocksize = diskInfo.f_bsize;// 每个block里面包含的字节数
    unsigned long long totalsize = blocksize * diskInfo.f_blocks;//总的字节数
    *total = totalsize;
    char totalsize_GB[10] = {
    0 };
    //printf("TOTAL_SIZE == %llu KB  %llu MB  %llu GB\n", totalsize >> 10, totalsize >> 20, totalsize >> 30); // 分别换成KB,MB,GB为单位
    sprintf(totalsize_GB, "%.2f", (float)(totalsize >> 20) / 1024);
    //printf("totalsize_GB=%s\n", totalsize_GB);
    unsigned long long freesize = blocksize * diskInfo.f_bfree; //再计算下剩余的空间大小
    *free = freesize;
    //printf("DISK_FREE == %llu KB  %llu MB  %llu GB\n", freesize >> 10, freesize >> 20, freesize >> 30);
    unsigned long long usedsize = blocksize * (diskInfo.f_blocks - diskInfo.f_bfree);
    char usedsize_GB[10] = {
    0 };
    sprintf(usedsize_GB, "%.2f", (float)(usedsize >> 20) / 1024);
    //printf("usedsize_GB=%s\n", usedsize_GB);
    return 0;
}

2、删除文件

void delete_directory(const char* path) {
   
    DIR* dir = opendir(path);
    struct dirent* entry;
    while ((entry = readdir(dir)) != NULL) {
   
        // check if entry is a directory
        if (entry->d_type == DT_DIR) {
   
            // skip . and ..
            if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) {
   
                continue;
            }
            // delete subdirectory
            char sub_path[1000];
            snprintf(sub_path, sizeof(sub_path), "%s/%s", path, entry->d_name);
            delete_directory(sub_path);
        }
        else {
   
            // delete file
            char file_path[1000];
            snprintf(file_path, sizeof(file_path), "%s/%s", path, entry->d_name);
            remove(file_path);
        }
    }
    closedir(dir);
    rmdir(path);
}

3、删除给定时间前的文件

void deleteOldFiles(const char* directoryPath, int daysThreshold) {
   
    DIR* dir = opendir(directoryPath);
    if (dir == NULL) {
   
        printf("Failed to open directory: %s\n", directoryPath);
        return;
    }
    struct dirent* entry;
    struct stat fileStat;
    char filePath[1024];
    time_t currentTime = time(NULL);
    time_t threshold = (daysThreshold * 24 * 60 * 60);

    while ((entry = readdir(dir)) != NULL) {
   
        if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
            continue;
        snprintf(filePath, sizeof(filePath), "%s/%s", directoryPath, entry->d_name);
        if (stat(filePath, &fileStat) == -1) {
   
            printf("Failed to get file stats: %s\n", filePath);
            continue;
        }

        if ( difftime(currentTime, fileStat.st_mtime) > threshold) {
   
            delete_directory(filePath); 
        }
        else
        {
   
            //printf("file: %s   ,%d:%d,%f,threshold%ld\n", filePath, S_ISREG(fileStat.st_mode), S_ISDIR(fileStat.st_mode), difftime(currentTime, fileStat.st_mtime), threshold);
        }

    }

    closedir(dir);
}
目录
相关文章
|
存储 Shell Linux
【Shell 命令集合 文件管理】Linux 读取命令 read命令使用指南
【Shell 命令集合 文件管理】Linux 读取命令 read命令使用指南
394 0
|
监控 网络协议 Shell
【Shell 命令集合 文件管理】Linux 远程文件复制命令 rcp 命令使用教程
【Shell 命令集合 文件管理】Linux 远程文件复制命令 rcp 命令使用教程
280 0
|
Shell Linux C语言
【Shell 命令集合 文件管理】Linux 删除 rm命令使用指南
【Shell 命令集合 文件管理】Linux 删除 rm命令使用指南
494 0
|
安全 Shell Linux
【Shell 命令集合 文件管理】Linux ssh 远程主机之间复制文件 scp 命令使用教程
【Shell 命令集合 文件管理】Linux ssh 远程主机之间复制文件 scp 命令使用教程
824 0
|
存储 Shell Linux
【Shell 命令集合 文件管理】Linux 快速定位文件和目录 slocate命令使用教程
【Shell 命令集合 文件管理】Linux 快速定位文件和目录 slocate命令使用教程
166 0
|
存储 Linux
Linux文件管理(超详细讲解)
Linux文件管理(超详细讲解)
657 5
|
存储 安全 Linux
Linux文件管理命令md5sum awk
通过结合 `md5sum`和 `awk`,不仅可以高效地进行文件完整性校验,还能灵活地处理和分析校验结果,为系统管理、数据审计等工作提供强大的支持。
329 2
|
存储 搜索推荐 Linux
2024年 15 个最佳免费开源 Linux 文件管理器
【4月更文挑战第25天】
11477 53
2024年 15 个最佳免费开源 Linux 文件管理器
|
Linux C++
Linux文件管理命令(一)
这是关于Linux命令行的一些内容,主要包括了一些常见的Linux命令及其参数的用法。例如,`ls` 命令用于列出目录内容,不同的参数如 `-a` 显示所有文件(包括隐藏文件),`-l` 以详细模式显示,`-S` 按大小排序等。`mkdir` 命令用于创建目录,`cp` 命令用于复制文件或目录,`mv` 命令用于移动或重命名文件或目录,而`rm` 命令则用于删除文件或目录。在使用这些命令时,可以结合不同的参数来实现不同的操作。
|
存储 Linux Shell
Linux 导航和文件管理
Linux 导航和文件管理
167 0

热门文章

最新文章