繁体   English   中英

如何查找最近10天修改过的10个最大目录,并使用Bash对其进行排序

[英]How to find the 10 biggest directories modified in the last 10 days and sort them using Bash

我需要找到最近10天修改过的10个最大目录,并使用bash按大小对它们进行排序。

我需要以人类可读的格式获取尺寸。

使用du以一种不错的格式获取目录大小将很容易:

sudo du -h /mypath/ --max-depth=1

但是du没有选择在最近10天之内添加修改的内容

这就是为什么我也一直尝试使用find的原因:

sudo find /mypath/e -xdev -depth -type d -ls -mtime 10

但是在这种情况下,我无法获得尺寸。

我一直在尝试在StackOverflow中混合并匹配其他几个问题,但发现可以使用xargs,但输出仍然不正确:

sudo find /mypath/ -mtime -10 -type d  | xargs ls -la

我仍在尝试许多选择,但是我找不到为此提供良好,可靠和优雅的解决方案。

你能帮忙吗?

您可以使用

sudo find /mypath/ -mtime -10 -type d  | xargs ls -ldrt

并非优雅,而是一种分离最近修改的目录,计算其存储,对结果进行排序并显示摘要的方法。 可以处理名称中带有空格的文件名,但不能使用制表符或换行符。 (供读者练习。)

#!/bin/bash

if [[ ! -d "$1" ]]; then
  echo "Usage: $(basename $0) <directory>" >&2
  exit 1
fi

find "$1" -mindepth 1 -xdev \
    -type d -mtime -10 |         # subdirs of $1 modified in last 10 days
  xargs -r -n 1 -d $'\n' du -s | # total storage in each such subdir
  sort -t $'\t' -k 1nr,1 |       # sorted in descending order
  head -10 |                     # first 10 are "top 10"
  awk -F $'\t' '{print $2}' |    # emit just the subdirs
  xargs -r -n 1 -d $'\n' du -sh  # total, human-readable, for those 10

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM