File number count


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting File number count
# 1  
Old 05-17-2009
File number count

hi all
i want to count the number of files in a particlar dir i have wrote the codes as below

Code:
 
fileCount= ls | wc -l
if [ $fileCount -eq 0 ]
then 
echo " "
elif [ $fileCount -gt 0 ]
then 
echo " "
fi

however when i excute the program
there are error happens " unary operator expected"
i just woundering
Code:
fileCount= ls | wc -l

will ls | wc -l store into my variable fileCount?

thanks for helping
# 2  
Old 05-17-2009
Code:
fileCount=`ls | wc -l`

or on bash
Code:
fileCount=$(ls | wc -l)

# 3  
Old 05-17-2009
try this,

Code:
fileCount=`ls | wc -l`
echo $fileCount

although i seem to remember having to pipe to sed as well to remove all the blank spaces that wc returns

Calypso
# 4  
Old 05-17-2009
thanks for helping
It works nicely ^^
# 5  
Old 05-17-2009

You don't need any external commands, let alone two of them:

Code:
set -- *
echo $#

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Count number of occurrence of a string in file

if there's a file containing: money king money queen money cat money also money king all those strings are on one line in the file. how can i find out how many times "money king" shows up in the line? egrep -c "money king" wont work. (7 Replies)
Discussion started by: SkySmart
7 Replies

2. 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

3. Shell Programming and Scripting

How to count number of occurances of string in a file?

Gurus, Need little guidance. I have A.txt and B.txt file. B.txt file contains Unique strings. Sample content of B.txt file for which i cut the fourth column uniquely and output directed to B.txt file And A.txt file contains the above string as a fourth column which is last column. So A.txt... (7 Replies)
Discussion started by: Shirisha
7 Replies

4. Shell Programming and Scripting

Count the number of lines in a file with one condition

Hi Everyone, 1.txt Mon 08 Feb 2010 12:30:44 AM MYT;1265560244;e164:0000116047275464;T;Central;0; Mon 08 Feb 2010 12:30:46 AM MYT;1265560246;e164:0000116047275464;T;Central;0; Mon 08 Feb 2010 12:30:48 AM MYT;1265560248;e164:0000116047275464;T;Central;0; Mon 08 Feb 2010 12:30:50 AM... (1 Reply)
Discussion started by: jimmy_y
1 Replies

5. UNIX for Dummies Questions & Answers

Count of Number of Lines in a File

Dear Members, I want to count the number of lines in a file; for that i am using the following command : FILE_LINE_COUNT=`wc -l $INT_IN/$RAW_FILE_NAME` if i do an echo on FILE_LINE_COUNT then i get 241 /home/data/testfile.txt I don't want the directory path to be displayed. Variable... (1 Reply)
Discussion started by: sandeep_1105
1 Replies

6. Shell Programming and Scripting

Count the number of files with only the partial file name

I have 1800 files in a directory. The file name is like out_cpty_XXXX. The "XXXX" vaires from file to file. I want to get the count of files with file name out_cpty_XXXX. How to get the count with just the partial file name? Any help would be appreciated? (4 Replies)
Discussion started by: Sangtha
4 Replies

7. Programming

to count the number of commented lines in a file

can someone guide me how to have a C pgm to count the number of commented lines? (3 Replies)
Discussion started by: naan
3 Replies

8. UNIX for Dummies Questions & Answers

How to count number of delimiters in a file name

I have a list of files with names as "FULL_abcd_xyz_timestamp.txt" and "FULL_xx_abcd_xyz_timestamp.txt". I am writing a script with a 'for loop' to take each file, strip the "FULL" and "timestamp" from the file name and do some actions on the contains of the file. So I need to know the number of... (4 Replies)
Discussion started by: ayanbiswas
4 Replies

9. Shell Programming and Scripting

hw can i count the number of character in a file by perl

i want to count the number of character contained in afile using perl cript help me out (1 Reply)
Discussion started by: trupti_rinku
1 Replies

10. Shell Programming and Scripting

Count number of lines in Binary file

Hi Friends Please help me out to count number of lines in binary file. It gives some wrong(less) using wc -l. Is there any other way to count lines of binary file. Thanks. (3 Replies)
Discussion started by: vanand420
3 Replies
Login or Register to Ask a Question