Counting number of files in a directory


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Counting number of files in a directory
# 1  
Old 09-05-2005
Counting number of files in a directory

Some simple questions from a simple man.

If i wanted to count the number of files contained within a directory, say /tmp would ls -l /tmp ¦ wc -l suffice and will it be accurate?

second one: How would i check the number of files with a certain string in the filename, in the same directory.

Appolgies if this has been answered before, i've been unable to find any posts.

Regards,

Al

Last edited by iamalex; 09-05-2005 at 10:53 AM..
# 2  
Old 09-05-2005
ls -l /tmp | wc -l would count files, directories, and also the line that displays the total size of the listing.

The best way is
ls -1 /tmp | wc -l (ls -number_one) although this still shows directories too.

If you just want a count of files (including soft links, etc)....

echo "`ls -l | grep -v "^d" | wc -l` - 1" | bc

You can grep -v out stuff you don't want.

On the second question....

ls /my/dir/*some_string* | wc -l

Or to use regular expressions instead of globbing

ls /my/dir | grep "some_[Ss]tring" | wc -l

Many ways to skin these cats.....

Cheers
ZB
# 3  
Old 09-05-2005
Top Man

Great, thanks, i had a feeling i was going wrong somewhere, excellent reply and really helpful.

regards,

Al
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Counting number of times content in columns occurs in other files

I need to figure out how many times a location (columns 1 and 2) is present within a group of files. I figured using a combination of 'while read' and 'grep' I could count the number of instances but its not working for me. cat file.txt | while read line do grep $line *08-new.txt | wc -l... (6 Replies)
Discussion started by: ncwxpanther
6 Replies

2. Shell Programming and Scripting

Counting the number of files within a directory input by the user

So I have a loop that stated if a directory exists or not. If it does it prints the number of files within that directory. I use this code... result=`(ls -l . | egrep -c '^-')` However, no matter which directory I input, it outputs the number "2" What is wrong here? (4 Replies)
Discussion started by: itech4814
4 Replies

3. Shell Programming and Scripting

counting the number of characters in the filename of all files in a directory?

I am trying to display the output of ls and also print the number of characters in EVERY file name. This is what I have so far: #!/bin/sh for x in `ls`; do echo The number of characters in x | wc -m done Any help appreciated (1 Reply)
Discussion started by: LinuxNubBrah
1 Replies

4. UNIX for Dummies Questions & Answers

Counting number of folders in a Directory

Help Needed ! Can we count number of folders of specific date in a directory, even if directory has folders of different dates. Please reply as soon as possible. (1 Reply)
Discussion started by: vishal_215
1 Replies

5. UNIX for Advanced & Expert Users

Counting files in a given directory

Hi all, Need some help counting files... :) I'm trying to count the number of files in a given directory (and subdirectories) which reportedly contains "thousands" of files. I'm using this: ls -R | wc -l However it's been an hour and looks like it's still running; there is no output... (18 Replies)
Discussion started by: verdepollo
18 Replies

6. Shell Programming and Scripting

Counting number of files that contain words stored in another file

Hi All, I have written a script on this but it does not do the requisite job. My requirement is this: 1. I have two kinds of files each with different extensions. One set of files are *.dat (6000 unique DAT files all in one directory) and another set *.dic files (6000 unique DIC files in... (1 Reply)
Discussion started by: shoaibjameel123
1 Replies

7. Shell Programming and Scripting

Counting the number of readable, writable, and executable items in a directory

Hello, I'm writing a script in sh in which the first command line argument is a directory. from that, i'm suppose to count the number of readable, writable, and executable items in the directory. I know using $1 represents the directory, and ls would display all the items in the directory, and that... (4 Replies)
Discussion started by: kratos22
4 Replies

8. Shell Programming and Scripting

need help with counting of files then pass number to variable

hi all, i'm trying to pass a count of files to a variable thru these set of codes: sh_count=$(ls -1 fnd_upload_LV*.* |wc -l) problem is if no files matches that, it will give an error "ls: fnd_upload_LV*.*: No such file or directory". how do i avoid having the shell script show that... (2 Replies)
Discussion started by: adshocker
2 Replies

9. UNIX for Dummies Questions & Answers

count the number of files which have a search string, but counting the file only once

I need to count the number of files which have a search string, but counting the file only once if search string is found. eg: File1: Please note that there are 2 occurances of "aaa" aaa bbb ccc aaa File2: Please note that there are 3 occurances of "aaa" aaa bbb ccc... (1 Reply)
Discussion started by: sudheshnaiyer
1 Replies

10. Shell Programming and Scripting

rm files in a directory, looping, counting, then exit

I am trying to write a script that will look for a file in a directory, then remove it. I need it to loop until it has removed a certain number of files. Is it better to do a repeat or to list each file in a pattern? Files will be numbered like RAF.01.*, RAF.02.*, etc. Thanks, James (6 Replies)
Discussion started by: JporterFDX
6 Replies
Login or Register to Ask a Question