LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   size of regular file (https://www.linuxquestions.org/questions/linux-newbie-8/size-of-regular-file-933741/)

Micky12345 03-10-2012 08:04 AM

size of regular file
 
how to find the total size of regular files

TB0ne 03-10-2012 09:00 AM

Quote:

Originally Posted by Micky12345 (Post 4623373)
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}'

David the H. 03-10-2012 09:59 AM

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?

sysmicuser 03-10-2012 04:53 PM

@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

Micky12345 03-11-2012 08:41 AM

i got its correct answer

thanx all
i did
Code:


ls -l | grep "^-" | awk '{total += $5} END {print total}'


unSpawn 03-11-2012 09:07 AM

Quote:

Originally Posted by TB0ne (Post 4623420)
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 (Post 4624008)
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.

TB0ne 03-11-2012 12:00 PM

Quote:

Originally Posted by unSpawn (Post 4624027)
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.

Micky12345 03-11-2012 07:52 PM

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
????

unSpawn 03-11-2012 08:14 PM

Quote:

Originally Posted by TB0ne (Post 4624104)
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

sysmicuser 03-12-2012 09:33 AM

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!

TB0ne 03-12-2012 09:44 AM

Quote:

Originally Posted by Micky12345 (Post 4624313)
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.

Tinkster 03-12-2012 04:51 PM

Quote:

Originally Posted by sysmicuser (Post 4624801)
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

sysmicuser 03-13-2012 07:22 AM

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.


All times are GMT -5. The time now is 10:25 AM.