centos7 文件夹下 按年月统计文件大小

我是一个运维人员,我想统计centos7.5环境下,/app/minio/file-bucket文件夹内,文件按照日期,按年月统计文件总大小

当然可以!我来帮您改造这个命令,使其能够按照年月分组统计。当前的命令只能按月份统计,无法区分不同年份的同月份。以下是几种改进方案: ## 方案一:基于ls命令的改造(简单版) ```bash ls
-l --time-style=+"%Y-%m" /app/minio/file-bucket | awk ' { # 提取年月和文件大小 year_month = $6; size = $5; # 如果是文件且不是目录行 if (year_month != "" && size ~ /^[0-9]+$/) { months[year_month] += size; } } END { # 按年月排序输出 n = asorti(months, sorted); for (i = 1; i <= n; i++) { month = sorted[i]; printf "年月: %s, 总大小: %.2f MB, %.2f GB\n", month, months[month]/1024/1024, months[month]/1024/1024/1024; } }' ``` ## 方案二:使用完整日期格式(推荐) ```bash ls -l --time-style=+"%Y-%m-%d" /app/minio/file-bucket | awk ' { # 提取完整日期和文件大小 date_str = $6; size = $5; # 如果是文件且不是目录行 if (date_str != "" && size ~ /^[0-9]+$/) { # 提取年月部分 (前7个字符) year_month = substr(date_str, 1, 7); months[year_month] += size; count[year_month]++; } } END { # 按年月排序输出 n = asorti(months, sorted); printf "%-10s %12s %12s %12s\n", "年月", "文件数量", "大小(MB)", "大小(GB)"; print "----------------------------------------------------"; for (i = 1; i <= n; i++) { month = sorted[i]; printf "%-10s %12d %12.2f %12.2f\n", month, count[month], months[month]/1024/1024, months[month]/1024/1024/1024; } }' ``` ## 方案三:使用find命令获取更准确的信息 ```bash find /app/minio/file-bucket -type f -printf "%TY-%Tm %s\n" | awk ' { year_month = $1; size = $2; total[year_month] += size; count[year_month]++; } END { # 按年月排序输出 n = asorti(total, sorted); printf "%-10s %12s %12s %12s\n", "年月", "文件数量", "大小(MB)", "大小(GB)"; print "----------------------------------------------------"; for (i = 1; i <= n; i++) { month = sorted[i]; printf "%-10s %12d %12.2f %12.2f\n", month, count[month], total[month]/1024/1024, total[month]/1024/1024/1024; } }' ``` ## 方案四:功能完整的统计脚本 ```bash #!/bin/bash target_dir="/app/minio/file-bucket" echo "正在统计目录: $target_dir" echo "==========================================" ls -l --time-style=+"%Y-%m-%d" "$target_dir" | awk ' BEGIN { total_size = 0; total_files = 0; } { date_str = $6; size = $5; if (date_str != "" && size ~ /^[0-9]+$/) { year_month = substr(date_str, 1, 7); months[year_month] += size; count[year_month]++; total_size += size; total_files++; } } END { # 输出表头 printf "%-10s %12s %12s %12s\n", "年月", "文件数量", "大小(MB)", "大小(GB)"; print "----------------------------------------------------"; # 按年月排序输出 n = asorti(months, sorted); for (i = 1; i <= n; i++) { month = sorted[i]; printf "%-10s %12d %12.2f %12.2f\n", month, count[month], months[month]/1024/1024, months[month]/1024/1024/1024; } print "----------------------------------------------------"; printf "%-10s %12d %12.2f %12.2f\n", "总计", total_files, total_size/1024/1024, total_size/1024/1024/1024; }' ``` ## 各方案对比: - **方案一**:最接近您原有命令的改造,简单直接 - **方案二**:推荐使用,显示完整信息且格式美观 - **方案三**:使用find命令,更适合大量文件的情况 - **方案四**:功能最完整,包含总计信息 ## 使用建议: 1. **如果目录文件不多**,使用方案二 2. **如果文件数量很大**,使用方案三(find命令效率更高) 3. **需要定期统计**,可以使用方案四保存为脚本 您可以根据实际需求选择合适的方案。方案二应该是您最需要的,它在您原有命令基础上增加了年月分组和更好的显示格式。

 

posted @ 2025-11-06 10:50  瘦瘦的猴子  阅读(12)  评论(0)    收藏  举报