LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-28-2006, 10:21 AM   #1
redir
Member
 
Registered: May 2004
Location: Virginia USA
Distribution: Debian_Ubuntu_FreeBSD
Posts: 110

Rep: Reputation: 15
How to count the number of files recursively with wild card


Hello,
How can I count the number of files in a directory. I need to know the number of files in one of my web sites. I do not want to include junk like pdf's and jpeg's so I just want to count the php and html pages. In windows for example I would use dir *.php and get a list with a number at the bottom. I of course need to do this recursively. I have found some use of the wc command but is'nt that just for line counting? Can that be piped through something else? Thanks in advance.

redir
 
Old 02-28-2006, 10:48 AM   #2
muha
Member
 
Registered: Nov 2005
Distribution: xubuntu, grml
Posts: 451

Rep: Reputation: 37
use -o in find to specify multiple extensions,
nl or wc can be piped to count anything, like so:
Code:
find . -name '*.html' -o -name '*.php' |wc -l
5
find . -name '*.html' -o -name '*.php' |nl
     1  ./bla/test.html
     2  ./old/store.html
     3  ./old/store_archive.html
     4  ./index.html
     5  ./test.html
the dot after find tells find to start in the current dir.
In general, relative to the current dir: find ./some_dir
In general, absolute: find /some_dir

Last edited by muha; 02-28-2006 at 10:54 AM.
 
Old 02-28-2006, 11:22 AM   #3
kilgoretrout
Senior Member
 
Registered: Oct 2003
Posts: 2,495

Rep: Reputation: 193Reputation: 193
You can combine find and wc to get a count of php files:

$ find -iname '*.php' | wc -l

The "find" part(find -iname '*.php') will give a recursive search for all php files in the current directory and print them out one line per file found matching the search. Run the find command separately and you will see what I mean. That output is piped through to wc which counts the new lines and prints the total. The above will only output the number from wc which is, in effect, the file count found by the find command.
 
Old 02-28-2006, 11:30 AM   #4
Hobbletoe
Member
 
Registered: Sep 2004
Location: Dayton, Oh
Distribution: Linux Mint 10, Linux Mint 11
Posts: 148

Rep: Reputation: 18
Here is what I came up with ...

Code:
#!/bin/bash

F_dirs()
{
 if [ `ls -l ${1}| egrep '^d' | wc -l` -eq 0 ]
 then
	 F_count ${1}
 else
	 for j in `ls -l ${1} | egrep '^d' | tr -s [:blank:] ' ' | cut -d' ' -f9`
	 do
	 	F_dirs ${1}/${j}
	 done
 fi
}

F_count()
{
 echo "${1} - php: `ls -l ${1}/*php 2> /dev/null | wc -l`"
 echo "${1} - html: `ls -l ${1}/*html 2> /dev/null | wc -l`"
}

echo -n "Enter the base directory:  "
read base_dir

for i in `ls -l ${base_dir} | egrep '^d' | tr -s [:blank:] ' ' | cut -d' ' -f9`
do
	F_dirs ${i}
done

exit 0
I assumed that you wanted to know the count/directory, and not just a lump sum. If I was wrong, then the find commands listed above are what you are looking for. Just put this all into a script, and make it executable

Code:
chmod +x filename
It asks for the base directory that you want to look at then does the rest. You can add different extentions to the F_count() if you want to count whatever you are looking for.

If anything is a bit confusing, just ask, and I can tell you why I did whatever.
 
Old 02-28-2006, 11:43 AM   #5
redir
Member
 
Registered: May 2004
Location: Virginia USA
Distribution: Debian_Ubuntu_FreeBSD
Posts: 110

Original Poster
Rep: Reputation: 15
Excellent, thank you. Now let me take this one step further. How can I omit certain subdirectories from this process. There are a few directories that I would like to not have their files counted. Possible?

Thanks again.
 
Old 02-28-2006, 12:25 PM   #6
Hobbletoe
Member
 
Registered: Sep 2004
Location: Dayton, Oh
Distribution: Linux Mint 10, Linux Mint 11
Posts: 148

Rep: Reputation: 18
Well, if you are using the find command, pipe it through egrep, and return just the ones that you want. I.e.

Code:
find . -name "*php" | egrep -v 'directory1|directory2|...' | wc -l
However, if you are using the script I provided (you didn't specify, so we'll cover all of the bases here), I modified the script to take the base directory as an argument if supplied (if not, it will prompt). In which case it would be

Code:
script_name.sh /directory_path | egrep -v 'directory1|directory2|...'
My new script is below ...

Code:
#!/bin/bash

# Standard Errors
E_SUCCESS=0	# Return if the script runs successfully
E_WRONGARGS=65	# Return if more than 1 argument is given

#Functions

F_dirs()
{
 if [ `ls -l ${1}| egrep '^d' | wc -l` -eq 0 ]
 then
	 F_count ${1}
 else
	 for j in `ls -l ${1} | egrep '^d' | tr -s [:blank:] ' ' | cut -d' ' -f9`
	 do
	 	F_dirs ${1}/${j}
	 done
 fi
}

F_count()
{
 echo "${1} - php: `ls -l ${1}/*php 2> /dev/null | wc -l`"
 echo "${1} - html: `ls -l ${1}/*html 2> /dev/null | wc -l`"
}

# Main Code

case "$#" in 
 0)
   echo -n "Enter the base directory:  "
   read base_dir;;
 1)
   base_dir=${1};;
 *)
   echo " Please use no arguments, or the base directory as the soul argument."
   exit $E_WRONGARGS;;
esac

for i in `ls -l | egrep '^d' | tr -s [:blank:] ' ' | cut -d' ' -f9`
do
	F_dirs ${i}
done

exit $E_SUCCESS
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
count total number of lines in several files xushi Programming 5 11-12-2005 05:42 PM
How to count number of argument received by a script? philipina Linux - General 2 07-05-2004 03:35 AM
How to count line numbers recursively in a directory? puzz_1 Linux - General 1 07-01-2004 10:43 AM
C++ STL count() number of substring occurences... nyk Programming 4 06-25-2004 08:12 AM
Count number of line in a file. philipina Programming 7 03-18-2004 06:04 PM


All times are GMT -5. The time now is 02:00 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration