Computing.Net > Forums > Unix > Count # of codes in directory

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Count # of codes in directory

Reply to Message Icon

Name: masayako
Date: August 10, 2005 at 14:38:52 Pacific
OS: Unix/Linux
CPU/Ram: N/A
Comment:

Hi there,

Need help here. In Unix,is there a way to coutn the total number of lines for all the files in a given directory?

I know there is a comand called" "wc" which can count WORD, LINE, but it only can count 1 file at a time, not all the files in a directory. PLEASE HELP!



Sponsored Link
Ads by Google

Response Number 1
Name: Jim Boothe
Date: August 10, 2005 at 15:12:32 Pacific
Reply:

There are a lot of ways to code this, but for starters, you should be able to get your answer with

wc -l * | tail -1

I did wc -l on a directory and got zero which is good. I don't know if that will hold true for all directories.


0

Response Number 2
Name: masayako
Date: August 10, 2005 at 15:18:03 Pacific
Reply:

Hi there,
It doesn't seem to count the subdirectory. It only counts the current directory.

{ ~/CrashDump} 136% wc -l * | tail -1
wc: XXX: Is a directory
wc: YYY: Is a directory
233297 total
Exit 1


0

Response Number 3
Name: Jim Boothe
Date: August 11, 2005 at 06:35:43 Pacific
Reply:

You can discard those error messages:

wc -l * 2> /dev/null | tail -1

Or you can target only the regular files, ignoring directories, links, and all the other file types:

# !/bin/ksh

fcount=0
lcount=0

for fname in *
  do
  if [ -f $fname ] ; then
     ((fcount=fcount+1))
     k=$(wc -l < $fname)
     ((lcount=lcount+k))
  fi
  done

echo "files=$fcount  lines=$lcount"

files=100  lines=79123


0

Response Number 4
Name: Luke Chi
Date: August 12, 2005 at 16:19:53 Pacific
Reply:

There's an another way:

find . -type f -exec wc -l "{}" \; | awk ' { sum += $1 } END {print sum}'

Luke Chi


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Count # of codes in directory

grep SQL code www.computing.net/answers/unix/grep-sql-code/6601.html

Any chance of HSP in Linux? www.computing.net/answers/unix/any-chance-of-hsp-in-linux/1118.html

unix: count unique fields in column www.computing.net/answers/unix/unix-count-unique-fields-in-column/7746.html