Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GNU find: test for file size greater/smaller-equal a certain size

Tags:

linux

find

Is there a way with GNU find to find files with a size >= or <= a certain size? I have only found the >, <, == operators, e.g. -size +1M, -size -1M, -size 1M, respectively.

In this blog, the author suggested a combination of multiple -size arguments as in find . -type f -size +1M -size -2M. However, this does not work for my find (GNU findutils) 4.4.2.

like image 571
Dr. Jan-Philip Gehrcke Avatar asked Dec 14 '12 15:12

Dr. Jan-Philip Gehrcke


People also ask

How do you check if a file's size is greater than a certain value in bash?

if [ (( $FILESIZE > MAXSIZE)) ]; -> if (( FILESIZE > MAXSIZE )); The arithmetic operators can serve as a conditional expression. (you don't need the $ within ((...)) )

How do I find files larger than 10mb in size in usr directory?

Syntax of find command to find files bigger than given size in Linux. For example, “-size +4G” makes the find command to search for files greater than 4GB. Here, + sign is denote that look for files greater than or equal to N[Type], like in this case, -size +4G will make find command to look for files bigger than 4GB.

How do you find files if their size is more than 1mb?

Using the -size flag The -size flag is used to find files of a specific size using k for kilobytes, M for megabytes, G for gigabytes, T for terabytes and P for petabytes. However on its own the size specified needs to be an exact match.


2 Answers

Since the operator <= is logically equivalent to not > (Not greater than), these 2 operators can be swapped with each other. In our example, to find files with size less than or equal to 1M, you can look for files not larger than 1M: -not -size +1M.

The same logic can be applied to >= using not <.

like image 139
ehudt Avatar answered Oct 31 '22 01:10

ehudt


the following command seems to work:

]$  find -version
find (GNU findutils) 4.4.2

find  ~ -type f -size '+1k' -a  -size '-3k' -exec ls -lah '{}' ';'
like image 44
Pierre Avatar answered Oct 31 '22 01:10

Pierre