Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List the directories whose the file sizes are all within a range

Tags:

linux

bash

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

for example: the 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 10-20 range. dir1 is not excluded, because all its files have sizes inside the range.

like image 321
Fattaneh Talebi Avatar asked Nov 28 '15 10:11

Fattaneh Talebi


People also ask

How the check the size of all files from a directory?

Right-click the file and click Properties. The image below shows that you can determine the size of the file or files you have highlighted from in the file properties window. In this example, the chrome. jpg file is 18.5 KB (19,032 bytes), and that the size on disk is 20.0 KB (20,480 bytes).

Which function is used to list all files in a directory?

The ls command is used to list files or directories in Linux and other Unix-based operating systems. Just like you navigate in your File explorer or Finder with a GUI, the ls command allows you to list all files or directories in the current directory by default, and further interact with them via the command line.

How do I get a list of directories in a directory?

You can use combination of ls command, find command, and grep command to list directory names only. You can use the find command too. In this quick tutorial you will learn how to list only directories in Linux or UNIX.


1 Answers

Borrowing code from 4ae1e1's comment:

Find the first exception to the rule (if any) in each command-line-specified subdirectory. Print that if it's 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. You could have this add allowed directories to an array, instead of printing them to stdout, if you really want to be paranoid about valid filenames (since you'd have to parse the output of this with an while IFS= read loop or something to allow any character, and that still breaks on dir names containing a newline.)

Apparently SO's syntax highlighting doesn't know the quoting rules for quotes inside $(command substitution) :/

like image 99
Peter Cordes Avatar answered Sep 29 '22 03:09

Peter Cordes