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
You should follow me on twitter here or grab rss feed to keep track of new changes.
Featured Articles:
- 30 Handy Bash Shell Aliases For Linux / Unix / Mac OS X
- Top 30 Nmap Command Examples For Sys/Network Admins
- 25 PHP Security Best Practices For Sys Admins
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- 20 Linux Server Hardening Security Tips
- Linux: 20 Iptables Examples For New SysAdmins
- Top 20 OpenSSH Server Best Security Practices
- Top 20 Nginx WebServer Best Security Practices
- 20 Examples: Make Sure Unix / Linux Configuration Files Are Free From Syntax Errors
- 15 Greatest Open Source Terminal Applications Of 2012

- My 10 UNIX Command Line Mistakes
- Top 10 Open Source Web-Based Project Management Software
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- The Novice Guide To Buying A Linux Laptop






![Linux Copy File Command [ cp Command Examples ]](http://s13.cyberciti.org/images/shared/rp/3/30.jpg)






{ 6 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
Thanks for giving this command, for some reason stat -c option is not working on my mac terminal but this command does.
did you try for android bash scripting, without wc, du, stat or awk?
still working on it, looks challenging
Does Android bash support this idiom?:
OIFS=”$IFS”
IFS=’ ‘
set -A size `ls -s “$1″`
IFS=$OIFS
filesize=${size[0]}