Looping through directory for file and do a word count

Posted on 2009-05-04
Last Modified: 2013-12-26
Hello All.

I am working on a script which basically loop through a directory for files and does a word count for each file and if the file has more than 1 row then writes that filename to a log file.

1. Loop thorugh a directory and pick up files
2. do a word count on each file
3. write the filename to a logfile only if the file has more than 1 record.

I have a written some code and it works but it is writing all files to logfile
for file in `ls`
 Count=`echo cat $file |  wc -l`
echo " my count is $Count"
if [ "$count" -gt 1 ];
echo $count
echo $file >> /home/log.txt
 echo "there is no file to write"

Open in new window

Question by:antvu
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2

Expert Comment

ID: 24295835
watch your case. $Count is initialized with capital C and later you are using lower case c..

Author Comment

ID: 24295936
I  changed the case with upper case C and executed the script.. below is the result i got
my count is 1
there is no file to write
 my count is 1
there is no file to write
 my count is 1
there is no file to write
 my count is 1
there is no file to write

I have 4 files in the directory , one of the files has 1 record and other 3 have more than 1 ..

Accepted Solution

mostart earned 50 total points
ID: 24295967
also remove the "echo" in line 3.

Author Comment

ID: 24295991
Thanks mostart.. that worked.
I am closing the question now.
LVL 48

Expert Comment

ID: 24299015
Let's remove redundant code and shorten
for file in *
  [ -f $file ] || continue  # Skip non-files
  lines=$(wc -l <$file)
  [ $lines -gt 1 ] && echo $file >>/home/log.txt

Open in new window


Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VBA Shell can't Find Word document 11 155
IE 11 + long running scripts 3 124
Interactive Powershell script to reset password age in AD 4 65
[bash] complex renaiming file 2 20
Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In a recent question ( here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question