Counting Files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Counting Files
# 1  
Old 12-02-2010
Counting Files

In a script, how would I go about finding the number of files for the first parameter after my script name?

For instance, my script name is myscript.sh and the folder I am checking is not the current working directory, lets say it's folder1.

so I type myscript.sh folder1

This script below only works for current directory. Here is what I have so far
Code:
FILE=$1
count=0

for FILE in *
do
     if [ -f $FILE ]
     then
     count=`expr $count + 1`
     fi
done

echo "File Count: $count"

Thanks!

Last edited by Scott; 12-02-2010 at 07:34 PM.. Reason: Please use code tags
# 2  
Old 12-02-2010
Hi.

With only a couple of modifications:

Code:
DIR=${1:-.}  # if $1 isn't given, use current directory (.)
count=0

[ ! -d $DIR ] && echo "No such directory." && exit 1

for FILE in $DIR/*  # Safer using a while loop, really!
do
     if [ -f "$FILE" ]
     then
     count=$((count + 1))
     fi
done

echo "File Count: $count"

This User Gave Thanks to Scott For This Post:
# 3  
Old 12-02-2010
Quote:
Originally Posted by scottn
Hi.

With only a couple of modifications:

Code:
DIR=${1:-.}  # if $1 isn't given, use current directory (.)
count=0

[ ! -d $DIR ] && echo "No such directory." && exit 1

for FILE in $DIR/*  # Safer using a while loop, really!
do
     if [ -f "$FILE" ]
     then
     count=$((count + 1))
     fi
done

echo "File Count: $count"

I think I tried everything in the world except $DIR/* , just the $DIR/ is what made the difference. Thank you so much!
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Counting files without ls or wc

i need to write a shell script to "count the number of files in the current directory but without using either ls or wc command"..... please help! (1 Reply)
Discussion started by: lexicon
1 Replies

2. Shell Programming and Scripting

multiple files: counting

In a directory, I have 5000 multiple files that contains around 4000 rows with 10 columns in each file containing a unique string 'AT' located at 4th column. OM 3328 O BT 268 5.800 7.500 4.700 0.000 1.400 OM 3329 O BT 723 8.500 8.900... (7 Replies)
Discussion started by: asanjuan
7 Replies

3. Shell Programming and Scripting

counting prefixes of files

Hello, at the moment I'm on with programming some kind of version history script for network devices. The configration files are uploaded in the form: devicename-confg_date_time. For keeping the last 10 configurations I want to split the devicename from the rest. This works well with... (5 Replies)
Discussion started by: Sally[-_-]
5 Replies

4. UNIX for Dummies Questions & Answers

Counting rows in many files

I'm regularly counting the number of rows in a number of files. I need to know how many rows their are in all files together. Counting rows in one file I can handle, but how do I count rows in all at once? I'd be grateful for any answer. (7 Replies)
Discussion started by: DrZoidberg
7 Replies

5. Solaris

Counting up files

Hi, I have a load of if statements that look for files in a directory, I want to be able to count them up and the total files confirmed in an email? I ahve tried expr but i this does not work and it only reads in the first if and ignores the rest. Please see script, #!/bin/ksh ... (2 Replies)
Discussion started by: Pablo_beezo
2 Replies

6. Shell Programming and Scripting

Help with counting files please

Hi all. If I have a unix directory with multiple files, lets say, I have some with .dat extensions, some with .txt extensions, etc etc. How in a script would I provide a count of all the different file types (so, the different extensions, I guess) in the directory?? So if I had: test.dat... (6 Replies)
Discussion started by: gerard1
6 Replies

7. UNIX for Dummies Questions & Answers

Counting lines and files

Hi experts, First of all thanks for all your help. How can i count the lines within a text file and send this number to another text file? And by the way how can i count the number of files inside a tape ("/dev/rtp") that as one pattern (Ex. "/CTA/") and send this number to a text file? I... (6 Replies)
Discussion started by: jorge.ferreira
6 Replies

8. UNIX for Dummies Questions & Answers

counting files

Which one line command can count and print to the screen the number of files containing "a" or "A" in their name (5 Replies)
Discussion started by: Edy
5 Replies

9. UNIX for Dummies Questions & Answers

Counting Occurances in Two Files

I have two files I want to compare, one is a list of variables and the other is a text file COBOL program. Basically what I want to do is display only those variables that appear in the COBOL program only once. However I would also accept a count of each variable as it appears in the COBOL... (2 Replies)
Discussion started by: Keith Gergel
2 Replies
Login or Register to Ask a Question