count of files and number of bytes


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting count of files and number of bytes
# 15  
Old 04-24-2011
Quote:
Originally Posted by jack1985
[indent]
Code:
#!/bin/bash 
FILE="/tmp/count.txt" 
ls -l $1 > $FILE
echo " count : $(grep ^- $FILE -c) "
awk '/^-/ {x=x+$5} END{print "Size:" x}' $FILE
rm -f  $FILE

Thnaks Ahamed .. It works Smilie

But it will break if $1 contains a character in $IFS (such as a space). Always quote variable references: "$1"
Quote:
but I just need one more thing

I'm new in Shell Programming :

please, can you explain this code a little for me :
Code:
awk '/^-/ {x=x+$5} END{print "Size:" x}' $FILE



/^-/ Select lines beginning with a hyphen (i.e. listings of regular files)
{x=x+$5} Add the file size to x
END{print "Size:" x} After the input is finished, print the total size,

It could be done more simply (without the temporary file and grep):
Code:
ls -l "$1" |
awk '/^-/ {x+=$5; n++} END{print "Count: " n; print "Size:" x}' "$FILE"

This User Gave Thanks to cfajohnson For This Post:
# 16  
Old 04-24-2011
Sample output of ls -l command
Code:
-rw-r--r--   1 ahamed   other       2085 Apr 23 21:22 ntpd.log
#This is a regular file. Notice that it starts with "-" in the permission tag "-rw-r--r--" 

drw-r--r--   1 ahamed   other       4096 Apr 23 21:22 check/
#This is a directory and hence starts with "d" in "drw-r--r--"

Also note, the fifth field in the output is the size.
So we are interested in finding the size of only normal files.
Code:
awk '/^-/ {x=x+$5} END{print "Size :" x} $FILE

Code:
/^-/

=> process only lines starting with "-"

Code:
x=x+$5

=> If you find line starting with "-" then add the value of the 5th column to the variable x and store it in x.
By default the field separator in awk is "space".

Code:
END{print "Size :" x}

=> After all the lines are processed, at the end print the value of x which is the total size.

regards,
Ahamed

Last edited by ahamed101; 04-24-2011 at 05:15 AM..
This User Gave Thanks to ahamed101 For This Post:
# 17  
Old 04-24-2011
Thank you a lot Ahamed , you helped so much
thank you all very much ..
# 18  
Old 04-24-2011
echo directory name
read dname
cd $dname
ls -l >file
wc -l file
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help for Count number of files in certain time

Chaps, I need to count number of files in a remote directory from Linux (FreeBSD) as if 10 trace files (log files) been generated within 5min of time. So this is the script then I can setup a monitoring. I came across with ls -1 \ip\d:\Logs | wc -l but then what else requires to check time... (8 Replies)
Discussion started by: samwijekoon
8 Replies

2. Shell Programming and Scripting

How to count the number of files moved?

I'm writing a script for searching substring in file content and then moving found files. So far I've wrote script shown below grep -lir 'stringtofind' $1 | xargs mv -t $2 How can i count number of files moved? (4 Replies)
Discussion started by: Kadikis
4 Replies

3. Shell Programming and Scripting

Count number of files

Hi All! I need to have a script that counts the number of files arriving in a landing directory, them some app pick these files to be processed and load to a DB. But this process is so fast that I am not able to count all the files arriving on a landing directory. Please can you help? My... (6 Replies)
Discussion started by: fretagi
6 Replies

4. Shell Programming and Scripting

Count the number of subset of files in a directory

hi I am trying to write a script to count the number of files, with slightly different subset name, in a directory for example, in directory /data, there are a subset of files that are name as follow /data/data_1_(1to however many).txt /data/data_2_(1 to however many).txt... (12 Replies)
Discussion started by: piynik
12 Replies

5. Shell Programming and Scripting

How to count number of files in directory and write to new file with number of files and their name?

Hi! I just want to count number of files in a directory, and write to new text file, with number of files and their name output should look like this,, assume that below one is a new file created by script Number of files in directory = 25 1. a.txt 2. abc.txt 3. asd.dat... (20 Replies)
Discussion started by: Akshay Hegde
20 Replies

6. Shell Programming and Scripting

Bash script to find the number of files and identify which ones are 0 bytes.

I am writing a bash script to find out all the files in a directory which are empty. I am running into multiple issues. I will really appreciate if someone can please help me. #!/bin/bash DATE=$(date +%m%d%y) TIME=$(date +%H%M) DIR="/home/statsetl/input/civil/test" ... (1 Reply)
Discussion started by: monasharma13
1 Replies

7. UNIX for Dummies Questions & Answers

Count number of files in directory excluding existing files

Hi, Please let me know how to find out number of files in a directory excluding existing files..The existing file format will be unknown..each time.. Thanks (3 Replies)
Discussion started by: ammu
3 Replies

8. Shell Programming and Scripting

count number of files in a directory

what's the script to do that? i want to only count the number of files in that directory, not including any sub directories at all (5 Replies)
Discussion started by: finalight
5 Replies

9. Shell Programming and Scripting

Count the number of files in a directory

Hi All, How do i find out the number of files in a directory using unix command ? (14 Replies)
Discussion started by: Raynon
14 Replies

10. UNIX for Dummies Questions & Answers

Count number of files in subdirectories

Hello, I am new to unix and would like to have a count of all the files in the current directory as well as all the files in a subdirectory. The command I used was ls -R | wc -l but the number returned wasn't correct. Can someone please help? Thanks (2 Replies)
Discussion started by: cbeverly
2 Replies
Login or Register to Ask a Question