How to count the number of files starting with a pattern in a Directory


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to count the number of files starting with a pattern in a Directory
# 1  
Old 07-24-2011
How to count the number of files starting with a pattern in a Directory

Hi!

In our current directory there are around 35000 files.

Out of these a few thousands(around 20000) start with, "testfiles9842323879838".

I want to count the number of files that have filenames starting with the above pattern. Please help me with the command i could use.

Thank you!!!
# 2  
Old 07-24-2011
Code:
ls testfiles9842323879838* | wc -l

# 3  
Old 07-24-2011
Actually i forgot to mention but i already tried this... Smilie and it gave the following error:

"Argument list too long".

Can you help me with this ? Thanks so much for you reply!
# 4  
Old 07-24-2011
Try this then:
Code:
ls | awk '/^testfiles9842323879838/{c++};END{print c}'

This User Gave Thanks to bartus11 For This Post:
# 5  
Old 07-24-2011
Hi,
You could try find command. I suppose it will work faster than ls and also the issue due to huge no. of files to be tried should disappear.

Code:
find . -name "testfiles9842323879838*" | wc -l

In case you are sure all the files are located directly under one directory, you could also use maxdepth with find. This would prevent find cmd from recursively searching all the subdirectories located underneath the directory in focus.

Code:
find . -maxdepth 1 -name "testfiles9842323879838*" | wc -l


Last edited by radoulov; 07-24-2011 at 05:15 PM.. Reason: Code tags.
# 6  
Old 07-24-2011
Wow!!!! It worked! You are a genuis Sir. Thanks so much.

Now, if i want to list the same files into a Txt file, can i have the command to do that? We need to have the list of all these files(20000) into a txt file or some file.
Forgive me if i am using the same post to ask a different/related question.
God bless!

---------- Post updated at 03:18 PM ---------- Previous update was at 03:13 PM ----------

Quote:
Originally Posted by bartus11
Try this then:
Code:
ls | awk '/^testfiles9842323879838/{c++};END{print c}'


Pranab/ thanks for your help too Smilie

Last edited by atechcorp; 07-24-2011 at 04:22 PM.. Reason: update
# 7  
Old 07-24-2011
Try:
Code:
ls | awk '/^testfiles9842323879838/' > file.txt

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 pattern matches per line for all files in directory

I have a directory of files, each with a variable (though small) number of lines. I would like to go through each line in each file, and print the: -file name -line number -number of matches to the pattern /comp/ for each line. Two example files: cat... (4 Replies)
Discussion started by: pathunkathunk
4 Replies

2. Shell Programming and Scripting

Count the number of subset of files in a directory

hi I am trying to write a script to count the number of files, with slightly different subset name, in a directory for example, in directory /data, there are a subset of files that are name as follow /data/data_1_(1to however many).txt /data/data_2_(1 to however many).txt... (12 Replies)
Discussion started by: piynik
12 Replies

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

4. UNIX for Dummies Questions & Answers

Count number of files in directory excluding existing files

Hi, Please let me know how to find out number of files in a directory excluding existing files..The existing file format will be unknown..each time.. Thanks (3 Replies)
Discussion started by: ammu
3 Replies

5. Shell Programming and Scripting

perl script on how to count the total number of lines of all the files under a directory

how to count the total number of lines of all the files under a directory using perl script.. I mean if I have 10 files under a directory then I want to count the total number of lines of all the 10 files contain. Please help me in writing a perl script on this. (5 Replies)
Discussion started by: adityam
5 Replies

6. UNIX for Dummies Questions & Answers

Read directory files and count number of lines

Hello, I'm trying to create a BASH file that can read all the files in my working directory and tell me how many words and lines are in that file. I wrote the following code: FILES="*" for f in "$FILES" do echo -e `wc -l -w $f` done My issue is that my file is outputting in one... (4 Replies)
Discussion started by: jl487
4 Replies

7. Shell Programming and Scripting

Count the number of occurrences of a pattern between each occurrence of a different pattern

I need to count the number of occurrences of a pattern, say 'key', between each occurrence of a different pattern, say 'lu'. Here's a portion of the text I'm trying to parse: lu S1234L_149_m1_vg.6, part-att 1, vdp-att 1 p-reserver IID 0xdb registrations: key 4156 4353 0000 0000 ... (3 Replies)
Discussion started by: slipstream
3 Replies

8. Shell Programming and Scripting

count number of files in a directory

what's the script to do that? i want to only count the number of files in that directory, not including any sub directories at all (5 Replies)
Discussion started by: finalight
5 Replies

9. Shell Programming and Scripting

nawk-how count the number of occurances of a pattern, when don't know the pattern

I've written a script to count the total size of SAN storage LUNs, and also display the LUN sizes. From server to server, the LUNs sizes differ. What I want to do is count the occurances as they occur and change. These are the LUN sizes: 49.95 49.95 49.95 49.95 49.95 49.95 49.95 49.95... (2 Replies)
Discussion started by: cyber111
2 Replies

10. Shell Programming and Scripting

Count the number of files in a directory

Hi All, How do i find out the number of files in a directory using unix command ? (14 Replies)
Discussion started by: Raynon
14 Replies
Login or Register to Ask a Question