如何获取未收到文件的天数列表。使用以下命令来获取文件计数和日期:
find . -maxdepth 1 -type f -printf '%TY-%Tm-%Td\n' | awk '{array[$0]+=1}END{ for(val in array) print val" "array[val] }'|sort
输出:
2019-05-09 1
2019-05-10 3
2019-05-13 2
2019-05-14 5
2019-05-15 1
2019-05-16 2
2019-05-17 1
2019-05-20 2
我还需要将缺失的天数计为 0。例如:
2019-05-12 0
答案1
试试这个代码:
TIME_STAMP=(`find . -maxdepth 1 -type f -printf '%TY-%Tm-%Td\n' | sort | sed -e 1b -e '$!d'`)
LIST ()
{
date1=$1
date2=`date -d "$date1 + 1 day" +"%Y-%m-%d"`
find . -maxdepth 1 -type f -newermt $date1 ! -newermt $date2 | echo "$date1 `wc -l` "
[ $date1 == $2 ] && exit 0;
LIST $date2 $2
}
LIST ${TIME_STAMP[0]} ${TIME_STAMP[1]}
答案2
date0='20190501'
date1='20190622'
numDays=$(( ( $(date -d "$date1" +'%s' ) - $(date -d "$date0" +'%s' ) ) / (60*60*24) ))
for day in $( seq 0 $((numDays-1)) ); do
d=$(date -d "$date0 + ${day}days" +"%Y-%m-%d")
echo $d $( find . -maxdepth 1 -type f -newermt "$d" ! -newermt "$d + 1day" | grep '.' -c )
done
date0
通过和之间的差值获取天数date1
,每天循环并打印找到的结果。
xarg版本
date0='20190501'
date1='20190622'
seq 0 $(( ( $(date -d "$date1" +'%s' ) - $(date -d "$date0" +'%s' ) ) / (60*60*24) )) \
| head -n -1 \
| xargs -I{} date -d "$date0 + {}days" +%Y%m%d \
| xargs -I{1} bash -c 'echo {1} $(find . -maxdepth 1 -type f -newermt "{1}" ! -newermt "{1} + 1day" | grep "." -c)'