≡ Menu

Shell Scripting: Check File Size ( Find File Size )

How do I find out file size under UNIX / Linux operating system and store the same to a variable called s?

There are various ways and command tricks to find out file size under UNIX / Linux shell.

stat command example

You can display file or file system status with GNU/stat command. The -c option can be used to get specific information about file such as size in bytes:
$ stat -c %s fw8ben.pdf
Sample output:


Refer to your local stat command man page for exact syntax, for example, FreeBSD stat works as follows:
$ stat -s file.txt

ls command example

Use ls command as follows to get human readable format:
$ ls -lah fw8ben.pdf | awk '{ print $5}'
Sample output:


You can store output to a variable:
$ s=$(ls -lah idg.fw8ben.pdf | awk '{ print $5}')
$ echo $s

$ s=$( stat -c %s fw8ben.pdf)
$ echo $s

Share this tutorial on:

Your support makes a big difference:
I have a small favor to ask. More people are reading the nixCraft. Many of you block advertising which is your right, and advertising revenues are not sufficient to cover my operating costs. So you can see why I need to ask for your help. The nixCraft, takes a lot of my time and hard work to produce. If you use nixCraft, who likes it, helps me with donations:
Become a Supporter →    Make a contribution via Paypal/Bitcoin →   

Don't Miss Any Linux and Unix Tips

Get nixCraft in your inbox. It's free:

{ 9 comments… add one }
  • marz February 25, 2009, 9:19 pm

    s=$(du -h myfile | awk ‘{ print $1 }’)

    “-h” == “print sizes in human readable format (e.g., 1K 234M 2G)”

  • tom July 29, 2010, 12:55 pm

    Thanks for these tips.

    Question: in — $ stat -c %s fw8ben.pdf —- what does %s do ? Any how does one refer to it i.e what is it ?


  • Raghu B R June 22, 2011, 6:29 am

    du -k Filename

    • Arsalan December 3, 2012, 1:05 pm

      Thanks for giving this command, for some reason stat -c option is not working on my mac terminal but this command does.

      • PGR October 15, 2013, 3:39 pm

        On Mac OS X (18.8.x), you may use
        stat -f %z

  • marty February 26, 2012, 11:52 am

    did you try for android bash scripting, without wc, du, stat or awk?
    still working on it, looks challenging

  • Gabbar Singh November 1, 2012, 4:16 pm

    Does Android bash support this idiom?:

    IFS=’ ‘
    set -A size `ls -s “$1″`

  • PGR October 15, 2013, 3:39 pm

    On MacOS X (10.8.x), you may use
    stat -f %z

  • ee December 10, 2013, 7:22 pm

    i prefer on Freebsd stat -f “%z” as well

Leave a Comment

You can use these HTML tags and attributes: <strong> <em> <pre> <code> <a href="" title="">

   Tagged with: , , , , , , , , , ,