List of directories with file sizes within range

I want to specify directories in which all file sizes are within a range. My solution is to look at each directory, and if all its sizes are in the range, show it. I want to know if there is an easier way to check if there is a switch in the find command or any other command like this.

e.g. range = 10-20

dir1: f1 size=12 f2 size= 19 dir2: f3 size=22 f4 size=11 OUTPUT = dir1 

dir2 is excluded because f3 is outside the range of 10-20. dir1 is not excluded because all of its files have sizes within the range.

+5
source share
2 answers

Borrowing code from 4ae1e1 comment:

Find the first exception to the rule (if any) in each subdirectory specified on the command line. Print it, if allowed.

 dir_filesize_rangefilter() { # args: lo hi paths... # sizes in MiB # return value: dir names printed to stdout local lo=$1 hi=$2 shift 2 # " $@ " is now just the paths for dir; do # in " $@ " is implicit local safedir=$dir [[ $dir = /* ]] || safedir=./$dir # make sure find doesn't treat weird -filenames as -options # find the first file smaller than lo or larger than hi [[ -z "$(find "$safedir" -type f \( -size "-${lo}M" -o -size "+${hi}M" \) -print -quit )" ]] && printf '%s\n' "$dir" done } 

I used "printf" because "echo" breaks if one of the directory names starts with -e or something like that. You may have added the allowed directories to the array instead of printing them to stdout if you really want to be paranoid with respect to the actual file names (since you have to parse the output of this using the while IFS= read or something that allows any character, and which still breaks dir names containing a new line.)

Apparently, SO syntax highlighting doesn't know citation rules inside $(command substitution) : /

+2
source

Here is a possible solution in 3 lines. I cited it in a specific example:

  • List of all files larger than 1 MB per file: du -hat 1M > gr.dat

  • List all files smaller than 3 MB in another file: du -hat -3M > sm.dat

  • Use grep to find matches in both generated files: grep -F -x -f gr.dat sm.dat

+1
source

Source: https://habr.com/ru/post/1236998/


All Articles