Monday 20 November 2017

linux - What is producing an inconsistent conditional find result?


I'm writing a small script that gets assorted file stats about two different file extensions (*.parquet OR *.metadata) in an arbitrary directory. I need to get these file stats only from files that were modified/created within the last 24h


TD=${1:-`pwd`}
find $TD -maxdepth 1 -type f -mtime -1 -name "*.parquet" -o -name "*.metadata" | wc -l
find $TD -maxdepth 1 -type f -mtime -1 -name "*.parquet" | wc -l
find $TD -maxdepth 1 -type f -mtime -1 -name "*.metadata" | wc -l

The first line outputs 60540


The second line outputs 430


The third line outputs 430


The expected output is for the first line to be the sum of the second and third lines (or the second and third lines to be firstline / 2


What I'm trying to do is listing all the files that end with either .parquet OR .metadata extension, and count the total amount of files for both extensions, total file sizes for each extension, average file size for each extension, sum of all file sizes


Finding the stats is easy, it's just listing the files what is throwing me off. What am I doing wrong?



Answer



It is operator precedence that is causing your problem. Since all expressions without logical operations between are implicitly linked with -a, which takes associates with higher precedence than -o, your combined expression is equivalent to:


find $TD \( -maxdepth 1 -a -type f -a -mtime -1 -a -name "*.parquet" \) -o -name "*.metadata" | wc -l

This means that you are finding all normal files in the search directory which have been modified in the last 24 hours with the name *.parquet, plus all files/directories/sockets, etc, located anywhere in the search directory tree, modified at any time, and with the name *.metadata. What you need is:


find $TD -maxdepth 1 -type f -mtime -1 \( -name "*.parquet" -o -name "*.metadata" \) | wc -l

No comments:

Post a Comment

Where does Skype save my contact's avatars in Linux?

I'm using Skype on Linux. Where can I find images cached by skype of my contact's avatars? Answer I wanted to get those Skype avat...