file size problem

Hi Everyone

I am having very strange problem

I want to know if i do

ls -ltr

then the file size it return will always be in bytes or it could be different in different scenario.

or is there any command which will return the size of file in only one unit.

Please help me regarding this.

with -h parameter for human-friendly units
with -k parameter for viewing units count

What other circumstances do you have where it doesn't show sizes in bytes?

hi
first of all thanks a lot for replying

actually i have one requirement where i need to convert file size to mb , for this i need file size in one unit. if i use

 
ls -h 

it will show some file size in kb, some in mb . but i want in one unit only so i can use proper mechanism to convert

please guide me into this.

On NetBSD there is

stat -f "%z" file

On Debian Linux there is

stat -c "%s" file

they both return the size of the file in bytes.

ls -ltr|awk 'NR>1 { printf("%s" " --> %10.10f " "%s\n" , $8 , $5/1024/1024, "MB" ) }'

@above

hi i did the same but i have one concern .. does

 
ls -ltr 

always shows the output in bytes or not

There are several different implementations of "ls".
What Operating System and version are you running?

Whatever the version, "ls -ltr" will list items which are not files (e.g. Directories, Special Files, Pipes, Links etc. and also output a "Total" line". A general purpose tool will need to cater for non-files.
If you just want a rough total size of a directory and its subdirectories then see the "du" command (with paramters).

to Methyl's point: if you're trying to stabilize your output to ensure that you can always expect bytes, you'll want to apply a filter that only picks up files. You'll probably want to query the system, using something like the find utility and wrap your ls around it, like this:

ls -ltr $(find /opt/my/directory/to_search -type f -ctime 0 )

Again, ls can vary by your particular situation, but -l is fairly standard across all versions to provide the long file details. The -h option you'd mentioned at one point is intended as "human-readable", but this itself depends on how computer literate the human reading it is. :slight_smile: