LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-10-2012, 08:04 AM   #1
Micky12345
Member
 
Registered: Feb 2012
Posts: 58

Rep: Reputation: Disabled
size of regular file


how to find the total size of regular files

Last edited by Micky12345; 03-11-2012 at 08:45 AM.
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 03-10-2012, 09:00 AM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,606

Rep: Reputation: 7960Reputation: 7960Reputation: 7960Reputation: 7960Reputation: 7960Reputation: 7960Reputation: 7960Reputation: 7960Reputation: 7960Reputation: 7960Reputation: 7960
Quote:
Originally Posted by Micky12345 View Post
how to find the total size of regular files can be found
A brief Google search turns up:
Code:
find . -name "<whatever pattern you want>" -ls | awk '{total += $7} END {print total}'
 
Old 03-10-2012, 09:59 AM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
I think you need to define your request in more detail. What do you mean by "total size" exactly? Of all files on the system, on a partition, in a directory, owned b a user, what? Are there any other conditions or requirements involved?

Have you taken a look at the du application yet?
 
Old 03-10-2012, 04:53 PM   #4
sysmicuser
Member
 
Registered: Mar 2010
Posts: 458

Rep: Reputation: 0
@TBOne

I always get mesmerized by awk's line processing beauty, so following your logic I did something like this.

Code:
[user01@centos-flash ~]$ls -l|awk '{ print $5}'

398
4096
36611
88
343
12216
10896
523
5290
19499
1393044
776
129
454
4096
21
[user01@centos-flash ~]$ls -l
total 1560
-rw-r--r-- 1 user01 user01     398 Mar  9 23:16 50percent_useful.sh
drwxrwxrwx 6 user01 user01    4096 Feb  7 13:04 atlassian-cli-2.4.0
-rw-r--r-- 1 user01 user01   36611 Mar 11 00:31 a.txt
-rwxr-xr-x 1 user01 user01      88 Mar  7 22:38 cpw.sh
-rw-r--r-- 1 user01 user01     343 Feb 13 14:06 garbage.txt
-rwxr-xr-x 1 user01 user01   12216 Feb 12 13:35 1.sh
-rwxrwxrwx 1 user01 user01   10896 Feb 12 13:26 opmn.xml
-rw-r--r-- 1 user01 user01     523 Feb 13 15:02 oratab
-rw-r--r-- 1 user01 user01    5290 Mar 11 00:26 output.txt
-rw-r--r-- 1 user01 user01   19499 Mar  9 12:22 p4_opened_files.sh.log
-rwxr--r-- 1 user01 user01 1393044 Mar 10 17:21 p4sql
-rwxr-xr-x 1 user01 user01     776 Feb 12 13:22 parse.sh
-rw-r--r-- 1 user01 user01     129 Feb 13 19:26 regex.txt
-rw-r--r-- 1 user01 user01     454 Feb 12 13:21 sample.xml
drwxr-xr-x 8 user01 user01    4096 Aug 11  2010 test
-rw-r--r-- 1 user01 user01      21 Mar 11 00:23 t.txt
[user01@centos-flash ~]$ls -l|awk '{ total += $5 } END { print $total }'

[user01@centos-flash ~]$
Now I am wondering why summation is not giving us any output?

Please help me understand.

Thanks
 
Old 03-11-2012, 08:41 AM   #5
Micky12345
Member
 
Registered: Feb 2012
Posts: 58

Original Poster
Rep: Reputation: Disabled
i got its correct answer

thanx all
i did
Code:
ls -l | grep "^-" | awk '{total += $5} END {print total}'
 
Old 03-11-2012, 09:07 AM   #6
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Quote:
Originally Posted by TB0ne View Post
Code:
find . -name "<whatever pattern you want>" -ls
He said regular files so shouldn't that be 'find . -type f'?


Quote:
Originally Posted by Micky12345 View Post
i got its correct answer
No you didn't. You should not treat 'ls' output that way. See http://mywiki.wooledge.org/ParsingLs for reasons why.
 
2 members found this post helpful.
Old 03-11-2012, 12:00 PM   #7
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,606

Rep: Reputation: 7960Reputation: 7960Reputation: 7960Reputation: 7960Reputation: 7960Reputation: 7960Reputation: 7960Reputation: 7960Reputation: 7960Reputation: 7960Reputation: 7960
Quote:
Originally Posted by unSpawn View Post
He said regular files so shouldn't that be 'find . -type f'?
Indeed, good catch, unSpawn.
Quote:
No you didn't. You should not treat 'ls' output that way. See http://mywiki.wooledge.org/ParsingLs for reasons why.
Given that the OP seems to be asking homework-type questions, I left some parts of the answer as an exercise to the student. Also, the "du -c" command can be used, but given the lack of clarity (want files in ONE directory, or all subs?), I omitted that as a possibility.
 
Old 03-11-2012, 07:52 PM   #8
Micky12345
Member
 
Registered: Feb 2012
Posts: 58

Original Poster
Rep: Reputation: Disabled
but by using grep '^-' we are saying that is should be a regular file

and in ls -l 5 th field gives the size of regular file is n't it???????????

den where is the wrong
????
 
Old 03-11-2012, 08:14 PM   #9
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Quote:
Originally Posted by TB0ne View Post
Given that the OP seems to be asking homework-type questions, I left some parts of the answer as an exercise to the student.
Hmm. Yeah, good thing. Especially since this one can't or won't read ;-p
 
Old 03-12-2012, 09:33 AM   #10
sysmicuser
Member
 
Registered: Mar 2010
Posts: 458

Rep: Reputation: 0
All,

are we safe to assume that
Code:
ls -l |grep "^-"|awk '{ total+= $5 } END {print total}
the correct way to get size of regular files?

Thanks unSpawn for that url it is pretty good,I was shocked though!
 
Old 03-12-2012, 09:44 AM   #11
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,606

Rep: Reputation: 7960Reputation: 7960Reputation: 7960Reputation: 7960Reputation: 7960Reputation: 7960Reputation: 7960Reputation: 7960Reputation: 7960Reputation: 7960Reputation: 7960
Quote:
Originally Posted by Micky12345 View Post
but by using grep '^-' we are saying that is should be a regular file and in ls -l 5 th field gives the size of regular file isn't it???????????

den where is the wrong????
"den"? As you've been asked before, SPELL OUT YOUR WORDS. This is an exercise for you...you've been given tools/links, and pointed in the right direction, but we're not going to spoon-feed you answers. You will actually have to do your own homework, and think/understand what things are doing.
 
Old 03-12-2012, 04:51 PM   #12
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 sysmicuser View Post
All,

are we safe to assume that
Code:
ls -l |grep "^-"|awk '{ total+= $5 } END {print total}
the correct way to get size of regular files?

Thanks unSpawn for that url it is pretty good,I was shocked though!
Why bother w/ parsing 'ls' output still, then, if you were shocked?
Code:
find -type f -exec stat --printf="%s\n" {} \; | awk '{size+=$1}END{print size}'
And if you MUST (INSIST ON) use ls ... at least give the unnecessary grep a miss.
Code:
ls -l |awk '/^-/{ total+= $5 } END {print total}'


Cheers,
Tink

Last edited by Tinkster; 03-12-2012 at 06:51 PM. Reason: fixed code tags
 
1 members found this post helpful.
Old 03-13-2012, 07:22 AM   #13
sysmicuser
Member
 
Registered: Mar 2010
Posts: 458

Rep: Reputation: 0
Code:
find -type f -exec stat --printf="%s\n" {} \; | awk '{size+=$1}END{print size}'
Thanks for that.
It works wonders.

Since I wanted only files in current directory and not sub-directories I did something like this.
Code:
find . -maxdepth 1 -type f -exec stat --print="%s\n" {} \;|awk '{size +=$1} END {print size}'
Also to eliminate from searching and evaluating hidden configurations files, I did something like this.
Code:
find . -maxdepth 1 -not -name ".*" -exec stat --print="%s\n" {} \; |awk '{size += $1} END {print size}'
One thing though which is not related with this thread but another thread of mine,
http://www.linuxquestions.org/questi...814/page2.html

Another member is helping me but I would also like your input on how to stop searching for "some" directories for some reason it is not working...

Thank you for your assistance.
 
  


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
cannot create regular file ...: text file busy laode Linux - General 4 11-27-2012 01:56 AM
[SOLVED] Store the size of largest file from a file/directory listing into variable SIZE lainey Linux - Newbie 3 11-15-2011 12:29 PM
Minimum file size to report a size in vsftpd log anon091 Linux - Server 1 10-12-2011 06:24 PM
any ideas to reduce log file size or make log file size managed? George2 Programming 2 08-13-2006 06:55 AM
file system size larger than fysical size:superblock or partition table corrupt klizon Linux - General 0 06-18-2004 04:18 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:24 PM.

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