Get size of a file using ls on all UNIX OSes

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Get size of a file using ls on all UNIX OSes
# 1  
Old 05-02-2013
Get size of a file using ls on all UNIX OSes

to get the most granular size of a file, you can do so with:
solaris hosts:

ls -l /tmp/filea | awk '{print $4}'

linux hosts:

ls -l /tmp/filea | awk '{print $5}'

Is there a more universal command that will give the file size?

i'm leery of this ls command because the fields in which the file size can be found differs on sunos and linux, and i'm not even sure if it is also like that with other unix oses.

so, is there a command that can accomplish the above without needing to check a field/column which, for all we know, wont be the same on another unix os.

i've tried "du -s". but that doesn't seem to work across all unix systems.

Last edited by SkySmart; 05-02-2013 at 07:06 PM..
# 2  
Old 05-02-2013
wc -c file

will give the file size in bytes.
This User Gave Thanks to hanson44 For This Post:
# 3  
Old 05-02-2013
It must read the entire file to do so, so would be a big problem for big files...
This User Gave Thanks to Corona688 For This Post:
# 4  
Old 05-02-2013
Reading a non-cached file, on generic Dell OptiPlex 380:
$ time wc -c

real    0m0.009s
user    0m0.000s
sys     0m0.004s

This User Gave Thanks to hanson44 For This Post:
# 5  
Old 05-02-2013
Last login: Thu May  2 21:50:22 on ttys000
Barrys-MacBook-Pro:~ barrywalker$ ls -l SCOPE.GIF
-rw-r--r--  1 barrywalker  staff  17424 11 May  2012 SCOPE.GIF
Barrys-MacBook-Pro:~ barrywalker$ printf `wc -c SCOPE.GIF`
17424Barrys-MacBook-Pro:~ barrywalker$ printf `wc -c SCOPE.GIF` > length.txt
Barrys-MacBook-Pro:~ barrywalker$ ls -l length.txt
-rw-r--r--  1 barrywalker  staff  5  2 May 22:04 length.txt
Barrys-MacBook-Pro:~ barrywalker$ less length.txt

Note the length on line 5, 5 characters long...
Also on line 5 gives the same as a file...
Line 7 proves the file length.txt is 5 characters long...

Last line gives...
length.txt (END)

"wc -c filename" gives below...

Barrys-MacBook-Pro:~ barrywalker$ wc -c SCOPE.GIF
   17424 SCOPE.GIF
Barrys-MacBook-Pro:~ barrywalker$

Note the spaces and filname...
This User Gave Thanks to wisecracker For This Post:
# 6  
Old 05-02-2013
Originally Posted by Corona688
It must read the entire file to do so, so would be a big problem for big files...
True. Too much overhead.
But the following is smart like ls -l:
du -sk file

gives size in kbytes.
NB du -sk directory is recursive and prints the sum, du -k directory also gives the individual sizes.
This User Gave Thanks to MadeInGermany For This Post:
# 7  
Old 05-02-2013
ok. so it looks like "wc -c" is the answer here? it gives the exact same numbers as "ls -l". difference is, with "wc -c", the file size will always be in the first column. that's perfect! not too sure about the potential overhead though, if there ever is one.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Get the remote server file size in UNIX

Hi All, I am trying to sftp, get a file from remote server. Post this we need to check the remote server file size or checksum value of the remote server file and compare it with the file size in the current server. But I am facing issue to get the remote server file size. Could you please... (2 Replies)
Discussion started by: abhi_123
2 Replies

2. Shell Programming and Scripting

Unix file empty.. but size is greater than zero??/

Hi, I have a file by redirecting some contents in unix shell. Even when there is no content that is being redirected, the file size still shows greater than zero. but even if there is no matching pattern the file APPRES has size greater than 0bytes. awk -f AA.awk $logfile>APPRES... (3 Replies)
Discussion started by: justchill
3 Replies

3. UNIX for Advanced & Expert Users

how to find the file size in unix

Anybody can help HOW TO FIND THE FILE SIZE IN UNIX (5 Replies)
Discussion started by: lmraochodisetti
5 Replies

4. UNIX for Dummies Questions & Answers

what's the Linux|Unix OSes works on Servers

Hi everybody , I'm new here in the forum and new Dummy in L|U systems (Hope finding welcomes...:)). I just want to ask : What is the OS's that works on servers and the OS's that work as client OS?? I just know that Solaris Work on sarvers :D.. and i'm glad to be memmber in this... (1 Reply)
Discussion started by: derbi
1 Replies

5. UNIX for Dummies Questions & Answers

Maximum size of a file in unix

What's the maximum file size supported by unix. (3 Replies)
Discussion started by: nagalenoj
3 Replies

6. UNIX for Dummies Questions & Answers

file size different in unix and windows

I have a file on solaris/linux. ls -ls shows the logical size to be: 13292 However, when I transfer the file to my windows machine.. Rightclick->Properties shows the file size as: 13421 I wrote a small program on unix and windows that does a stat() on the file and reports the st_size... (6 Replies)
Discussion started by: the_learner
6 Replies

7. Filesystems, Disks and Memory

unix file size becomes zero

Hi, when can a unix library file size become zero? For example.: can mistyping this command -> /usr/ucb/ps -auxww|grep -i <process name> make the "ps" library file size to become zero or its contents to get deleted? Is there any other way that an inadvertant mistake could cause the file size to... (1 Reply)
Discussion started by: ananthmm
1 Replies

8. Shell Programming and Scripting

File size limitation of unix sort command.

hi , iam trying to sort millions of records which is delimited and i cant able to use sort command more than 60 million..if i try to do so i got an message stating that "File size limit exceeded",Is there any file size limit for using sort command.. How can i solve this problem. thanks ... (7 Replies)
Discussion started by: cskumar
7 Replies

9. HP-UX

Unix file size - HP-UX

Hi All, Currently we are using HP-UX machine.. We are facing problems with respect to file size. The file size does not seem to be exceeding 2 GB. Could you please let me know the following 1. Is there any difference between a 32 bit application and 64 bit application with respect to file... (2 Replies)
Discussion started by: krishna7
2 Replies

10. UNIX for Dummies Questions & Answers

Hp Unix file size problem

We have two files /var/adm/wtmp and /var/opt/OV/tmp/OpC/guiagtdf Could you please tell me if what these files are and if I can purge them as they are very big and Vi will not let me look at them, it gives me a message 'file to long'. Help any ideas Thanks :cool: (3 Replies)
Discussion started by: A Roberts
3 Replies
Login or Register to Ask a Question

Featured Tech Videos