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

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 9 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersncdu pydf
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
9 comments… add one
  • marz Feb 25, 2009 @ 21:19

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

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

  • tom Jul 29, 2010 @ 12:55

    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 Jun 22, 2011 @ 6:29

    du -k Filename

    • Arsalan Dec 3, 2012 @ 13:05

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

      • PGR Oct 15, 2013 @ 15:39

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

  • marty Feb 26, 2012 @ 11:52

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

  • Gabbar Singh Nov 1, 2012 @ 16:16

    Does Android bash support this idiom?:

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

  • PGR Oct 15, 2013 @ 15:39

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

  • ee Dec 10, 2013 @ 19:22

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

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum