help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Check if file size greater than a small number?


From: Greg Wooledge
Subject: Re: Check if file size greater than a small number?
Date: Wed, 9 Mar 2022 13:11:23 -0500

On Wed, Mar 09, 2022 at 11:42:11AM -0600, Dennis Williamson wrote:
> On Wed, Mar 9, 2022 at 11:10 AM Peng Yu <pengyu.ut@gmail.com> wrote:
> > But if my goal is just to know whether the file size is greater than a
> > smaller (say 40). What is the most efficient way to do so in bash?

> size=41
> filename=testfile
> read -n "$size" < "$filename"
> if ((${#REPLY} >= size)) ...

That doesn't look like it will do what the OP desires.  For at least
two reasons:

1) It won't handle NUL bytes at all.

2) It will try to read 41 *characters*, but the OP might (probably does)
   want the file's size in *bytes*.  Of course, this part can be fixed
   by setting locale variables to C, but see part 1.

unicorn:~$ printf 'abcde\0ghijk' > foo
unicorn:~$ ls -l foo
-rwxr-xr-x 1 greg greg 11 Mar  9 13:09 foo*
unicorn:~$ read -n 11 < foo
unicorn:~$ echo "${#REPLY}"
10



reply via email to

[Prev in Thread] Current Thread [Next in Thread]