LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-07-2008, 04:57 PM   #1
jadeddog
LQ Newbie
 
Registered: Jun 2008
Posts: 26

Rep: Reputation: 15
find the file size


i need a way to find the size of any file... you'd think this would by easy with a cut command... something similar to the following (the following is with ubuntu):

root@penguinKiller:/home/rhett/Documents/scripts/logging# ls -l maillog
-rw-r--r-- 1 root root 20964 2008-07-07 15:16 maillog
root@penguinKiller:/home/rhett/Documents/scripts/logging# ls -l maillog | cut -f5 -d " "
20964

by the above it would appear that that cut command gives you the proper response, but it doesn't work for all files or all file sizes, and seems to depend on the formatting of the ls command for different types of UNIX (the following is with solaris):

mailsrv /opt/SUNWmsgsr/log # ls -l mail.log_current
-rw------- 1 mailsrv mailgrp 105649354 Jul 7 15:53 mail.log_current
mailsrv /opt/SUNWmsgsr/log # ls -l mail.log_current | cut -f5 -d " "
mailsrv

in both of the above examples, the size is the 5th column, but because of extra spacing between the columns, the cut commands produce different results... i need a command that will find the file size regardless of the UNIX box it is on, and what convention it uses for the ls command.... so i need a better way to use the cut command, or i need a different way to access the file size altogether

any help would be greatly appreciated!
 
Old 07-07-2008, 05:08 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
use awk instead of cut
awk '{print $5}'
 
Old 07-07-2008, 05:17 PM   #3
jadeddog
LQ Newbie
 
Registered: Jun 2008
Posts: 26

Original Poster
Rep: Reputation: 15
edit: i figured out the awk part, it seems to work pretty good, thank you very much

Last edited by jadeddog; 07-07-2008 at 05:20 PM.
 
Old 07-07-2008, 05:49 PM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Well ... the beauty of awk is that it will take ANY amount of ANY whitespace (spaces, tabs, ... not NewLines ;})
as a field separator by default ... you could use sed instead, too, but that would be quite lengthy.
 
Old 07-07-2008, 06:05 PM   #5
gilead
Senior Member
 
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 14.0
Posts: 4,141

Rep: Reputation: 168Reputation: 168
The following may be redundant since it still uses awk, but is another way to get file-size info.

Does your Solaris box have the stat command with the -t (terse) option? It give output like this (the first 2 fields are the relevant ones, file name and size):
Code:
stat -t /var/log/maillog
/var/log/maillog 74594 160 81a0 0 0 6807 128762 1 0 0 1215467845 1215467801 1215467801 4096
stat -t /var/log/maillog | awk '{print $2}'
74594
 
Old 07-07-2008, 06:52 PM   #6
jadeddog
LQ Newbie
 
Registered: Jun 2008
Posts: 26

Original Poster
Rep: Reputation: 15
yeah the box has that command... could do it that way i suppose... either way it works though, which is great... that will teach me for developing something on ubuntu and implementing on solaris, lol
 
Old 07-07-2008, 07:01 PM   #7
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Quote:
Originally Posted by jadeddog View Post
yeah the box has that command... could do it that way i suppose... either way it works though, which is great... that will teach me for developing something on ubuntu and implementing on solaris, lol
You would have hit that snag irrespective of the OS you work
on... to throw "cut" off all it takes is files w/ different owners/
groups since it goes by single characters as delimiters, and
the padding WILL vary.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to find size increase most file or folder? jimmyjiang Linux - Server 1 07-16-2007 10:47 PM
any ideas to reduce log file size or make log file size managed? George2 Programming 2 08-13-2006 06:55 AM
Code to find out text file size Deepak Inbasekaran Programming 10 04-11-2006 04:52 AM
Command to find total file size satimis Linux - Newbie 1 01-22-2004 11:19 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:32 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration