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:
74777
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:
74K
You can store output to a variable:
$ s=$(ls -lah idg.fw8ben.pdf | awk '{ print $5}')
$ echo $s
OR
$ s=$( stat -c %s fw8ben.pdf)
$ echo $s
Featured Articles:
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- 20 Linux Server Hardening Security Tips
- My 10 UNIX Command Line Mistakes
- Linux: 20 Iptables Examples For New SysAdmins

- 25 PHP Security Best Practices For Sys Admins
- The Novice Guide To Buying A Linux Laptop
- 10 Greatest Open Source Software Of 2009
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- Top 20 OpenSSH Server Best Security Practices
- Top 10 Open Source Web-Based Project Management Software
- Top 5 Linux Video Editor Software
Facebook it - Tweet it - Print it -


{ 3 comments… read them below or add one }
s=$(du -h myfile | awk ‘{ print $1 }’)
“-h” == “print sizes in human readable format (e.g., 1K 234M 2G)”
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 ?
Tom
du -k Filename