Combination of find -xargs & wc -l


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Combination of find -xargs & wc -l
# 1  
Old 07-08-2008
Combination of find -xargs & wc -l

Dear all,

I have to calculate sum of record count of files of the specified directory.
First I tried the following way which prints one or more outputs.
How can I sum of this output?

Code:
find /home/work/tmp/1/O/ -type f -print0 | xargs -0 wc -l | grep total
  1666288 total
  1073908 total

Thanks in advance
# 2  
Old 07-08-2008
If it's just for that directory, you could do a wc -l on those files like
Code:
root@sremhv27:/usr/IBM/HTTPServer/conf/sicher> wc -l *| tail -1| cut -d " " -f1
     916 httpd.conf
     209 ldap.prop.elan
     209 ldap.prop.papyrus
    1334 total

# ie. 

root@sremhv27:/usr/IBM/HTTPServer/conf/sicher> wc -l *| tail -1 |awk '{print $1}'
1334

If you want to have it including files in the subdiretories (which might be the reason you are using find), I would go and just fill an array with it like:

Code:
#!/usr/bin/ksh

typeset -i RESULT=0

set -A ARRAY "`find . -type f -exec wc -l {} \; | awk '{print $1}'`"

for ELE in ${ARRAY[*]}; do
        let RESULT=${RESULT}+${ELE}
done

echo "Result is: ${RESULT}"

This ksh, but if you are using another shell, you can just do ARR=`somecommandtomakealist`; for ELE in $ARR; do something; done
So no extra defining of an array with set -A has to be done. Just wanted to do it correct for ksh.
# 3  
Old 07-08-2008
Assuming no newlines in the filenames:

Code:
(IFS=$'\n'; awk 'END{print NR}' $(find -type f))

If you have zsh:

Code:
awk 'END{print NR}' **/*(.)


Last edited by radoulov; 07-08-2008 at 05:16 AM.. Reason: corrected
# 4  
Old 07-08-2008
Thank you very much,

There aren't any subdirectory.
There are many files so I can't directly use wc or awk.

Code:
wc -l * | tail -1
-bash: /usr/bin/wc: Argument list too long

I can do it by following script in bash.

Code:
sum=0

for eachtotal in `find /home/work/tmp/1/O/ -maxdepth 1 -type f -print0 | xargs -0 wc -l | grep total | awk '{print $1}'`
do
sum=`expr $sum + $eachtotal`
done

echo $sum

Are there some way to do this in one line code, because I am writing bash shell script which consists of hundreds of lines?

Smilie
# 5  
Old 07-08-2008
Something like this:

Code:
find -type f -exec wc -l {} +|awk '{_+=$1}END{print _}'

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

find files in sub dir with tag & add "." at the beginning [tag -f "Note" . | xargs -0 {} mv {} .{}]

I am trying find files in sub dir with certain tags using tag command, and add the period to the beginning. I can't use chflags hidden {} cause it doesn't add period to the beginning of the string for web purpose. So far with my knowledge, I only know mdfind or tag can be used to search files with... (6 Replies)
Discussion started by: Nexeu
6 Replies

2. Shell Programming and Scripting

Using find for variable combination of perms

Hi, I'm trying to use find in kshell (AIX) to find all files with perms of write for other AND any execute bit set. e.g: r--r-x-w- would qualify and rw-rw--wx would qualify but ---rwxr-xr-x wouldn't qualify So far, I've been trying something like this: find . -type f -perm... (4 Replies)
Discussion started by: alanp36
4 Replies

3. Shell Programming and Scripting

Xargs + Find Help

Guys i want to run a command to list all directories that havn't been modified in over 548 days ( 1.5 yrs ). Id like to run a script to first print what the command finds ( so i get a list of the files pre move ... i have a script set for this : find /Path/Of\ Target/Directory/ -type d -mtime... (4 Replies)
Discussion started by: modulartention
4 Replies

4. Shell Programming and Scripting

find and xargs

hi, i've been trying to figure this weird error but I cannot seem to know why. I am using below find command: find . \( ! -name . -prune \) -type f -mtime +365 -print The above code returns no file because no files are really more then 365 days old. However, when I use xargs, its... (9 Replies)
Discussion started by: The One
9 Replies

5. UNIX for Dummies Questions & Answers

XARGS and FIND together

I am trying to delete files older than 60 days from a folder: find /myfolder/*.dat -mtime +60 -exec rm {} \; ERROR - argument list too long: find I can't just give the folder name, as there are some files that I don't want to delete. So i need to give with the pattern (*.dat). I can... (3 Replies)
Discussion started by: risshanth
3 Replies

6. Shell Programming and Scripting

Advice using cut & echo combination commands

Hi, I am cutting data from a fixed length test file and then writing out a new record using the echo command, the problem I have is how to stop multiple spaces from being written to the output file as a single space. Example: cat filea | while read line do field1=`echo $line | cut -c1-2` ... (6 Replies)
Discussion started by: dc18
6 Replies

7. Shell Programming and Scripting

combination between || and && in IF condition with ksh

Dear All, Please advice about this issue. when i run this line in a script if && || && || && if i enter $x = test3 and $y = test1 the If condition apply while it should not Best Regards (2 Replies)
Discussion started by: islam.said
2 Replies

8. UNIX for Dummies Questions & Answers

problem with grep in combination with xargs

Dear all, I have tried the following 2 lines xargs -t -i -exec grep -i -w {} file_1 >>test < file_2 cat -s file_2| xargs -t -i -exec grep -i -w {} file_1 >> test They were meant to search for the contents of file_2 in file_1 and write the respective lines of file_1 into file "test" .... (15 Replies)
Discussion started by: Bruno
15 Replies

9. UNIX for Dummies Questions & Answers

Grep and find combination

Hello All, I'm trying the following:find . -name "*" -exec grep -ln "IsAlpha" {} \; It gives me file names only (having string "IsAlpha"), I want to get line numbers also, something like this: test 1: Line 52 test 1: Line 95 etc Is it possible to obtain using grep & find only. (5 Replies)
Discussion started by: nervous
5 Replies

10. Shell Programming and Scripting

'find' and 'tar' combination

I'm trying to tar the files I get from the 'find' command result. However I can't make it run successfuly? This is for our archiving process. Here is my script: find /mnt/LOGS -mtime -10 -name "TUXLOG.*" -exec tar -cvf /mnt/LOGS/combine.tar {} \; Im not sure why it is not working or it is... (2 Replies)
Discussion started by: kharen11
2 Replies
Login or Register to Ask a Question