Results 1 to 10 of 12
Anyone know of a script that gives me a count of the files under all the directories under a certain directory?
Thanks,
Klesla...
- 02-24-2005 #1Just Joined!
- Join Date
- Feb 2005
- Posts
- 2
Count Files under multiple directories
Anyone know of a script that gives me a count of the files under all the directories under a certain directory?
Thanks,
Klesla
- 02-24-2005 #2Linux Engineer
- Join Date
- Nov 2004
- Location
- Montreal, Canada
- Posts
- 1,267
This His the way to get the number of file under the current directory...
As for all the subdirectory, since I never tryed it, I'll write a little bash file that does this... (when I get home)\"Meditative mind\'s is like a vast ocean... whatever strikes the surface, the bottom stays calm\" - Dalai Lama
\"Competition ultimatly comes down to one thing... a loser and a winner.\" - Ugo Deschamps
- 02-24-2005 #3Just Joined!
- Join Date
- Feb 2005
- Posts
- 2
Thanks. I found a way on the net for counting in the current directory.
ll -1A|wc -l
But I can't seem to have it give me results for all the sub directories and there file count. Something that can tell me the directory and what count is under it.
- 02-24-2005 #4Linux Engineer
- Join Date
- Nov 2004
- Location
- Montreal, Canada
- Posts
- 1,267
That probably cant be done using simple bash command... I dont think its "that" powerfull...
Using C++ or Java, would make this an easy task thou\"Meditative mind\'s is like a vast ocean... whatever strikes the surface, the bottom stays calm\" - Dalai Lama
\"Competition ultimatly comes down to one thing... a loser and a winner.\" - Ugo Deschamps
- 02-24-2005 #5Linux User
- Join Date
- Jul 2004
- Location
- Poland
- Posts
- 368
Or even simpler using something like Perl or Python:
Originally Posted by UgoDeschamps
If you insist on using bash, it could be possible to use -R -1 flags and strip blank lines together with those ending with colons (subdirs). Look for pattern mathing in the bash manual page.Code:#!/usr/bin/env python import os, sys def numOfFiles(dir): count = 0 for tup in os.walk(dir): count += len(tup[2]) return count for arg in sys.argv[1:]: print arg, '->', numOfFiles(arg)
Hope you'll handle the problem soon, best regads."I don't know what I'm running from
And I don't know where I'm running to
There's something deep and strange inside of me I see"
- 02-25-2005 #6Linux Enthusiast
- Join Date
- Jan 2005
- Posts
- 575
I thought this might be a homework question so I didn't
answer earlier but since a script has been posted already
here's my effort using C-shell:
This assuming that the current directory is the one we'reCode:set a = "`find . -print0`" ; echo $#a
interested in.Otherwise replace the . (dot) after find by
the name of the directory.The count will include the head
directory.If you don't want that , subtract 1 from the value of $#a
- 02-25-2005 #7Linux User
- Join Date
- Jul 2004
- Location
- Poland
- Posts
- 368
It seems to count the number of everything in the current directory, but the question is about files. Or am I wrong?
Originally Posted by Santa's little helper "I don't know what I'm running from
And I don't know where I'm running to
There's something deep and strange inside of me I see"
- 02-25-2005 #8Linux Enthusiast
- Join Date
- Jan 2005
- Posts
- 575
When you say files do you mean just regular files ? I assumed the
question was about all kinds of files ie regular files , links , directories etc.
But anyway if one wants to exclude directories say they could write
Or to exclude directories and linksCode:set a = "`find . \! -type d -print0`" ; echo $#a
Code:set a = "`find . \! -type l \! -type d -print0`" ; echo $#a
- 02-25-2005 #9Linux Enthusiast
- Join Date
- Jan 2005
- Posts
- 575
Oh , yeah , forgot to say that a solution like ls -l | wc -l or
similar will only give the correct result only if no file name contains a newline character.This is almost certain but not 100% .
- 03-08-2005 #10Just Joined!
- Join Date
- Mar 2005
- Posts
- 1
here is the command line answer
ls -laR | wc -l
will recursivly count files


Reply With Quote