Bash查找文件并按日期和大小过滤

问题描述 投票:0回答:1

我有一个包含大量文件的目录。每天都会自动添加新文件。

文件名的格式如下:[GROUP_ID] _ [RANDOM_NUMBER] .txt示例:012_1234.txt

对于每一天,对于每个GROUP_ID(032,024,044 ......等),我想只保留当天最大的文件。

例如,在3月27日和28日的两天里,我有:

March 27    -      012_1234.txt      -         12ko
March 27    -      012_0243.txt      -         3000ko
March 27    -      016_5647.txt      -         25ko
March 27    -      024_4354.txt      -         20ko
March 27    -      032_8745.txt      -         40ko

March 28    -      032_1254.txt      -         16ko
March 28    -      036_0456.txt      -         30ko
March 28    -      042_7645.txt      -         500ko
March 28    -      042_2310.txt      -         25ko
March 28    -      042_2125.txt      -         34ko
March 28    -      044_4510.txt      -         35ko

我希望:

March 27    -      012_0243.txt      -         3000ko
March 27    -      016_5647.txt      -         25ko
March 27    -      024_4354.txt      -         20ko
March 27    -      032_8745.txt      -         40ko

March 28    -      032_1254.txt      -         16ko
March 28    -      036_0456.txt      -         30ko
March 28    -      042_7645.txt      -         500ko
March 28    -      044_4510.txt      -         35ko

我找不到合适的bash ls / find命令,有人有想法吗?

使用此命令,我可以显示每天最大的文件。

ls -l *.txt --time-style=+%s |
awk '{$6 = int($6/86400); print}' |
sort -nk6,6 -nrk5,5 | sort -sunk6,6

但我想要每天每个GROUP_ID文件的最大文件。所以,如果有一个文件为“012”group_id文件,10ko,我想显示它,即使有其他组ID更大的文件...

bash shell find ls
1个回答
0
投票

我发现自己是解决方案:

ls -l | tail -n+2 |
awk '{ split($0,var,"_"); group_id=var[5]; print $0" "group_id }' |
sort -k9,9 -k5,5nr |
awk '$10 != x { print } { x = $10 }'

这给了我每个group_id最大的文件,所以现在我只是添加处理日期部分。

有关信息:

tail -n+2:隐藏ls命令输出的“总”部分

首先awk:获取group_id部分(012,036 ...)并在原始行($ 0)之后显示它

排序:按文件名和大小排序

取每个group_id的最大大小(在开始时由awk添加的第10列)

© www.soinside.com 2019 - 2024. All rights reserved.