简体   繁体   中英

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

I need to find the 10 biggest directories that were modified in the last 10 days and sort them by size using bash.

I'd need to get the size in a human readable format.

It would be easy to get the directory size in a nice format using du:

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

But du has no option to add the modified in the last 10 days bit

That is why I've been also trying to use find:

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

But in this case I cannot get sizes.

I've been trying to mix and match several other questions here in StackOverflow and I've found that I could use xargs but still the output doesn't look right:

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

I am still trying many options but I cannot find the good, reliable and elegant solution for this.

Can you please help?

您可以使用

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

No elegant, but one way of separating recently-modified directories, computing their storage, sorting the results, and displaying the summary. Handles file names with spaces in their names ok, but not tabs or newlines. (Exercise for the reader.)

#!/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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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