0

I want to check the file size in a shell script. I am trying to check if the file in a specific directory exceeds 2 GB, i.e., 2,147,483,648 bytes.

How can I easily do this in a shell script?

I have the following two files:

-rw-rw-rw-    1 op       general  1977591120 Jul 02 08:27 abc
-rw-rw-rw-    1 op       general  6263142976 Jul 01 18:39 xyz

When I run find . -size +2047MB, I get both the files as output

./abc
./xyz

I expect only xyz in the output size it is ~6 GB and abc is slightly less than ~2 GB . What can be the reason for both files showing up in the output?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jitesh Dani
  • 385
  • 3
  • 10
  • 17

6 Answers6

3

How to find files in a specific directory

What man says

-size n[cwbkMG]

        File uses n units of space.  The following suffixes can be used:

        `b'    for 512-byte blocks (this is the default if no suffix is used)
        `c'    for bytes
        `w'    for two-byte words
        `k'    for Kilobytes (units of 1024 bytes)
        `M'    for Megabytes (units of 1048576 bytes)
        `G'    for Gigabytes (units of 1073741824 bytes)

Examples

Find files larger than 2 GB in the current directory, but don't look in subdirectories

find . -size +2G -maxdepth 1

Output it with the ls -dils format

find . -size +2G -maxdepth 1 -ls

Other comments

I'm surprised your MB didn't kick out an error. Example: find: invalid -size type `B'

This may be due to your distribution.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
vol7ron
  • 40,809
  • 21
  • 119
  • 172
2

Compare:

stat -f "%z bytes   %N"  ./*     # FreeBSD stat                                         syntax highlighter fix */

find . -size +$((2*1024*1024*1024))c    #  man 1 find | less -p '-size'
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
juan
  • 21
  • 1
  • This helps to find if the file present in a directory is bigger or not. If I know the file which I am checking, how can I check the size of that file in the shell script? – Jitesh Dani Jul 02 '10 at 19:22
1

Try find . -size +2047M without the B. This seems to work in subdirectories too.

Danilo Bargen
  • 18,626
  • 15
  • 91
  • 127
0

My guess would be that find is including the file system overhead and any unused space in the cluster occupied by the file.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
duck
  • 171
  • 1
  • 3
0

Try using +2G instead of MB...

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
thegeek
  • 2,388
  • 2
  • 13
  • 10
0

I would suggest checking out the du command.

Use the -h option like... du yourfilename.ext -h and you'll get something humanly readable.

  • important side note, du uses 1024 byte blocks, so 1KB blocks. you can do the math from there to find GB size and whatnot. Hope this helps! – Codex-Major Jan 20 '22 at 21:35