Consider:
var=`ls -l | grep TestFile.txt | awk '{print $5}'`
I am able to read file size, but how does it work?
size=$( stat -c '%s' TestFile.txt )
ls - list files, stat - file statistics. Clever names...Yes, so basically you could divide it into 4 parts:
ls -lList the current directory content (-l for long listing format)
| grep TestFile.txt Pipe the result and look for the file you are interested in
| awk '{print $5}Pipe the result to awk program which cuts (by using spaces as separator) the fifth column which happens to be the file size in this case (but this can be broken by spaces in the filename, for example)
The backquotes (`) enclose commands. The output of the commands gets stored in the var variable.
NOTE: You can get the file size directly by using du -b TestFile.txt or stat -c %s TestFile.txt
du doesn't support showing the file size in bytes. Leave the -b option off (or substitute with -k) and it'll show file sizes in kilobytes instead.du and stat can report two very different numbers in the case of sparse files - stat will report the same number that ls -l does, which is why it is probably the better answer in this case, while du will report the actual total disk space actually being used (although some versions of du have an --apparent-size or similar option).
ls -l | grep TestFile.txt | awk '{print $5}'reverse quote is not visible here, when i check -> ls -l | grep TestFile.txt output is only file is displayed how come awk got the size alsodu.stat -c %s TestFile.txtwould be a far superior approach...