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
# 15  
Old 05-02-2013
Quote:
A better approach is to use some
very simple shell script logic to
parse the the output of ls -l
Go back to the original post. They wanted an alternative to that approach.
# 16  
Old 05-02-2013
Perhaps the script can utilize uname to determine which field to use.

If you can depend on perl availability, then you can use its stat builtin function identically on any platform. A simple example that does not implement any error handling:
Code:
find . | perl -lpe '$_ = (stat($_))[7] . "\t$_"'

du was mentioned in several posts. Note that du does not report the file size; it reports the amount of storage that the file occupies. These two values are usually unequal.

Regards,
Alister
# 17  
Old 05-02-2013
Quote:
Originally Posted by SkySmart
to get the most granular size of a file, you can do so with:
Code:
solaris hosts:

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

Code:
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.
This is very strange. On any UNIX System (such as Solaris, HP/UX, OS X, and AIX), the output from the ls -l command should have the file size in the 5th field; not the 4th. Are you using an alias on the Solaris hosts for ls that is employing other options that alter the output? Could you please show us the output of just the ls:
Code:
"ls" -l /tmp/filea

on one of your Solaris hosts without feeding it through awk? (Note that I put the ls in quotes to prevent any possible alias substitutions.)
# 18  
Old 05-02-2013
Quote:
Originally Posted by Don Cragun
This is very strange. On any UNIX System (such as Solaris, HP/UX, OS X, and AIX), the output from the ls -l command should have the file size in the 5th field; not the 4th. Are you using an alias on the Solaris hosts for ls that is employing other options that alter the output? Could you please show us the output of just the ls:
Code:
"ls" -l /tmp/filea

on one of your Solaris hosts without feeding it through awk? (Note that I put the ls in quotes to prevent any possible alias substitutions.)
here, i'm still getting them in different fields:
Code:
[sunbox-01] "ls" -l filea 
-rw-------   1 root       71126 Apr 27 07:21 filea

Code:
[linuxbox-01]#  "ls" -l filea 
-rwxr-xr-x 1 root root 71126 Apr 29 20:27 filea

# 19  
Old 05-02-2013
Quote:
Originally Posted by SkySmart
here, i'm still getting them in different fields:
Code:
[sunbox-01] "ls" -l filea 
-rw-------   1 root       71126 Apr 27 07:21 filea

Code:
[linuxbox-01]#  "ls" -l filea 
-rwxr-xr-x 1 root root 71126 Apr 29 20:27 filea

Please try:
Code:
"ls" -ln filea

With the amount of space between "root" and the file size, it looks like you might have a group name that is entirely made up of spaces and/or tabs??? Using the l and n options will print user and group numbers instead of names.

Please also show us the output from uname -a on sunbox-01.
This User Gave Thanks to Don Cragun For This Post:
# 20  
Old 05-03-2013
You are certainly using /usr/ucb/ls
an old version of ls, only present to provide compatibility with the old SunOS 4, that was terminated 10 years ago (and in turn was compatible with 4.2 BSD Unix).
Use
Code:
ls -lo

or
Code:
PATH=/bin:/usr/bin ls -l

or both
Code:
PATH=/bin:/usr/bin ls -lo

or also set locale with
Code:
LC_ALL=C PATH=/bin:/usr/bin ls -lo

This User Gave Thanks to MadeInGermany For This Post:
# 21  
Old 05-03-2013
Hi Yoda...

There are definately leading spaces and could be a trailing space as well.
Also newline(s) may be part of the result...

Code:
Last login: Fri May  3 08:01:47 on ttys000
Barrys-MacBook-Pro:~ barrywalker$ wc -c < SCOPE.GIF
   17424
Barrys-MacBook-Pro:~ barrywalker$

EDIT:

Code:
Last login: Fri May  3 08:01:47 on ttys000
Barrys-MacBook-Pro:~ barrywalker$ wc -c < SCOPE.GIF
   17424
Barrys-MacBook-Pro:~ barrywalker$ wc -c < SCOPE.GIF > length.txt
Barrys-MacBook-Pro:~ barrywalker$ ls -l len*
-rw-r--r--  1 barrywalker  staff  9  3 May 08:15 length.txt
Barrys-MacBook-Pro:~ barrywalker$ hexdump -C length.txt
00000000  20 20 20 31 37 34 32 34  0a                       |   17424.|
00000009
Barrys-MacBook-Pro:~ barrywalker$

There as a proof test...

Last edited by wisecracker; 05-03-2013 at 04:18 AM..
This User Gave Thanks to wisecracker For This Post:
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