Shell – How to get exact file size and file name

command linelsshell-script

Right now I am using ls -lt /my/directory (see below) then php parses the output. But inconsistently because the delimiter is space and there might be two or more spaces in between two fields.

The solution is output only file name and its size. How can I list files (using wild card) and its full size in command line?

So instead of

-rw------- 1 db2inst1 db2iadm1 855658496 Apr 22 02:31 MONTE.0.db2inst1.NODE0000.CATN0000.20120422023005.001

I get

855658496 MONTE.0.db2inst1.NODE0000.CATN0000.20120422023005.001

Best Answer

As others have noted, if this is a dynamic web application, you're much better off using PHP's functions for accessing file size.

But if you're going to get it by executing a shell command, don't use ls. Instead, use stat, and tell it exactly what you want:

$ stat -c '%n %s' x.txt
x.txt 12
Related Question