DzikiChrzan
DzikiChrzan

Reputation: 3107

Find with 3 execs

I wrote this command:

find -exec test -e "{}/meta" ";" -exec du -h -t 500M {} ";"

It checks if file meta is in location and if it's whole location bigger than 500MB. Now I want to read first line of this meta file. I tried with this

find -exec test -e "{}/meta" ";" -exec test du -h -t 500M {} ";" -exec sed '1q;d' {}/meta ";"

or this

find -exec test -e "{}/meta" ";" -exec du -h -t 500M {} ";" -exec head -n 1 {}/meta ";"

But it ignores du and read line from every meta file.
How it should looks like?

Upvotes: 0

Views: 58

Answers (3)

SLePort
SLePort

Reputation: 15461

After a try with find . -type d -size +500M, it appears that the -size option applied to directory does not check its total file size.

Searching for the desired file and checking its directory size should be the better approach:

find . -type f -name 'meta' -execdir bash -c 's=$(du -sh .); [[ "${s%M*}" -gt "500" ]] && sed "1q" meta' \;

Upvotes: 1

aleksanderzak
aleksanderzak

Reputation: 1172

Another approach would be to use -execdir:

find -name meta -type f -execdir bash -c 's=($(du -s .)) ; (( s > 2000 ))'  \; -exec head -n1 {} \;

Upvotes: 0

redneb
redneb

Reputation: 23850

I would actually use a while loop in bash for this, like that:

find -type d | \
    while IFS= read -r dir; do
        if (($(du -ms -- "$dir" | cut -f1) >= 500)); then
            [[ -e "$dir/meta" ]] && head -n1 "$dir/meta"
        fi
    done

I am also not relying on the -t flag of du because it only affects the output, not the status code of `du, so I just use a simple arithmetic comparison in bash instead.

Upvotes: 1

Related Questions