Technology Questions

Go Back   Technology Questions > Software Questions > Operating System Questions > Linux

Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old 11-12-2007, 12:20 PM
M
Newsgroup Contributor
 
Posts: n/a
File Count

Is there a command that will simply give me a count of files in a given
directory? ls lists them and du gives me size of directory. I just want a
count.

Its for a perl script I am working on.

M


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

 
Old 11-12-2007, 12:20 PM
  #2 (permalink)  
Old 11-12-2007, 12:30 PM
Bit Twister
Newsgroup Contributor
 
Posts: n/a
Re: File Count

On Mon, 12 Nov 2007 14:12:31 -0600, M wrote:
> Is there a command that will simply give me a count of files in a given
> directory? ls lists them and du gives me size of directory. I just want a
> count.


ls | wc -l


> Its for a perl script I am working on.


Sorry do not know how to do it in Perl language.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

  #3 (permalink)  
Old 11-12-2007, 01:30 PM
Chris F.A. Johnson
Newsgroup Contributor
 
Posts: n/a
Re: File Count

On 2007-11-12, M wrote:
>
> Is there a command that will simply give me a count of files in a given
> directory? ls lists them and du gives me size of directory. I just want a
> count.


set -- *
count=$#

> Its for a perl script I am working on.


This sort of thing is much easier in a shell script.

--
Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
Shell Scripting Recipes: | My code in this post, if any,
A Problem-Solution Approach | is released under the
2005, Apress | GNU General Public Licence
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

  #4 (permalink)  
Old 11-12-2007, 03:20 PM
Thomas P Brisco
Newsgroup Contributor
 
Posts: n/a
Re: File Count

On Mon, 12 Nov 2007 20:17:09 +0000, Bit Twister wrote:

> On Mon, 12 Nov 2007 14:12:31 -0600, M wrote:
>> Is there a command that will simply give me a count of files in a given
>> directory? ls lists them and du gives me size of directory. I just
>> want a count.

>
> ls | wc -l
>
>
>> Its for a perl script I am working on.

>
> Sorry do not know how to do it in Perl language.


Be nice now, aliases of "ls" can botch *that*...

/bin/ls -1 | wc -l

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

  #5 (permalink)  
Old 11-12-2007, 09:40 PM
Mark South
Newsgroup Contributor
 
Posts: n/a
Re: File Count

On Mon, 12 Nov 2007 17:10:37 -0600, Thomas P Brisco wrote:

> On Mon, 12 Nov 2007 20:17:09 +0000, Bit Twister wrote:
>
>> On Mon, 12 Nov 2007 14:12:31 -0600, M wrote:
>>> Is there a command that will simply give me a count of files in a given
>>> directory? ls lists them and du gives me size of directory. I just
>>> want a count.

>>
>> ls | wc -l
>>
>>
>>> Its for a perl script I am working on.

>>
>> Sorry do not know how to do it in Perl language.

>
> Be nice now, aliases of "ls" can botch *that*...
>
> /bin/ls -1 | wc -l


Small correction: that should have been

/bin/ls | wc -l

because "/bin/ls -l" is off by one, due to the "total" line it throws at
the top of the list.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

  #6 (permalink)  
Old 11-13-2007, 05:20 AM
Curt
Newsgroup Contributor
 
Posts: n/a
Re: File Count

On 2007-11-12, Chris F.A. Johnson <cfajohnson******.com> wrote:
> On 2007-11-12, M wrote:
>>
>> Is there a command that will simply give me a count of files in a given
>> directory? ls lists them and du gives me size of directory. I just want a
>> count.

>
> set -- *
> count=$#


I almost forget to do "echo $count" and thought I had nothing.

;-)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

  #7 (permalink)  
Old 11-13-2007, 08:50 AM
Robert Newson
Newsgroup Contributor
 
Posts: n/a
Re: File Count

Mark South wrote:

....
>>>ls | wc -l
>>>
>>>>Its for a perl script I am working on.
>>>>
>>>Sorry do not know how to do it in Perl language.
>>>

>>Be nice now, aliases of "ls" can botch *that*...
>>
>>/bin/ls -1 | wc -l

>
> Small correction: that should have been
>
> /bin/ls | wc -l
>
> because "/bin/ls -l" is off by one, due to the "total" line it throws at
> the top of the list.


/bin/ls -1 (as in /bin/ls -<the_numer_one>) forces single column output (the
default for to a non-tty) is what was suggested, not /bin/ls -l (as in
/bin/ls -<letter_ell>) as that lists the files *and* gives a total count.
(There is a slight difference on my screen, but it is very hard to spot.)

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

  #8 (permalink)  
Old 11-13-2007, 09:50 AM
Mark South
Newsgroup Contributor
 
Posts: n/a
Re: File Count

On Tue, 13 Nov 2007 16:44:03 +0000, Robert Newson wrote:

> Mark South wrote:
>
> ...
>>>>ls | wc -l
>>>>
>>>>>Its for a perl script I am working on.
>>>>>
>>>>Sorry do not know how to do it in Perl language.
>>>>
>>>Be nice now, aliases of "ls" can botch *that*...
>>>
>>>/bin/ls -1 | wc -l

>>
>> Small correction: that should have been
>>
>> /bin/ls | wc -l
>>
>> because "/bin/ls -l" is off by one, due to the "total" line it throws at
>> the top of the list.

>
> /bin/ls -1 (as in /bin/ls -<the_numer_one>) forces single column output (the
> default for to a non-tty) is what was suggested, not /bin/ls -l (as in
> /bin/ls -<letter_ell>) as that lists the files *and* gives a total count.
> (There is a slight difference on my screen, but it is very hard to spot.)


My apologies, in my font they are more or less indistinguishable :-)

It should, as you point out, be unnecessary, because ls should write each
element of its output on a new line when piped.

Anyway, I need glasses or better fonts. Or maybe I have an excuse to buy
a new monitor? Yeah! That must be the solution ;-)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

  #9 (permalink)  
Old 11-13-2007, 10:10 AM
Douglas O'Neal
Newsgroup Contributor
 
Posts: n/a
Re: File Count

M wrote:
> Is there a command that will simply give me a count of files in a given
> directory? ls lists them and du gives me size of directory. I just want a
> count.
>
> Its for a perl script I am working on.
>
> M
>
>


#! /usr/bin/perl -w
use File::Find ();
my $Count=0;
sub wanted {(my @stat = lstat($_)) && -f _ && $Count++;}


File::Find::find({wanted => \&wanted}, '.');
print "$Count\n";
exit;



Doug
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

  #10 (permalink)  
Old 11-13-2007, 11:00 AM
eu.alug@gmail.com
Newsgroup Contributor
 
Posts: n/a
Re: File Count

On 12 nov, 17:12, "M" <n...@spam.tonsjunkmail.com> wrote:
> Is there a command that will simply give me a count of files in a given
> directory? ls lists them and du gives me size of directory. I just want a
> count.
>
> Its for a perl script I am working on.
>
> M



Try:

/bin/ls | wc -m
Check, in wc help, the correct argument for words.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

  #11 (permalink)  
Old 11-13-2007, 07:50 PM
Florian Diesch
Newsgroup Contributor
 
Posts: n/a
Re: File Count

"M" <no@spam.tonsjunkmail.com> wrote:

> Is there a command that will simply give me a count of files in a given
> directory? ls lists them and du gives me size of directory. I just want a
> count.
>
> Its for a perl script I am working on.


$dir='/tmp/*';
print scalar map {$_} <${dir}>, "\n"


Florian
--
<http://www.florian-diesch.de/>
-----------------------------------------------------------------------
** Hi! I'm a signature virus! Copy me into your signature, please! **
-----------------------------------------------------------------------
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

  #12 (permalink)  
Old 11-13-2007, 10:40 PM
Robert Newson
Newsgroup Contributor
 
Posts: n/a
Re: File Count

Mark South wrote:

....
>>/bin/ls -1 (as in /bin/ls -<the_numer_one>) forces single column output (the
>>default for to a non-tty) is what was suggested, not /bin/ls -l (as in
>>/bin/ls -<letter_ell>) as that lists the files *and* gives a total count.
>>(There is a slight difference on my screen, but it is very hard to spot.)

>
> My apologies, in my font they are more or less indistinguishable :-)
>
> It should, as you point out, be unnecessary, because ls should write each
> element of its output on a new line when piped.


But it *IS* useful to know just in cases ls is aliased to include things
like -C (the last of -C and -1 (minus one) takes precedence).

> Anyway, I need glasses or better fonts. Or maybe I have an excuse to buy
> a new monitor? Yeah! That must be the solution ;-)

^_^

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

  #13 (permalink)  
Old 11-13-2007, 10:50 PM
Robert Newson
Newsgroup Contributor
 
Posts: n/a
Re: File Count

eu.alug******.com wrote:

....
> Try:
>
> /bin/ls | wc -m
> Check, in wc help, the correct argument for words.


Hmmm...
$ man ls
....
-w, --words
print the word counts

So I presume you mean -w. Ok let's try it:
$ cd /win/c
$ ls -F
Config.sys* autoexec.bat* inspect.txt* os832217.bin* system.1st*
DDCheck.txt* autoexec.dos* io.sys* recycled/ timer/
Matrox/ command.com* kpcms/ scandisk.log* tmp/
My Documents/ config.dos* msdos.sys* setupxlg.txt* utils/
Program Files/ detlog.txt* nc/ suhdlog.---* windows/
Wanadoo/ dist.txt* netlog.txt* suhdlog.dat*
$ ls | wc -l
29
$ ls -1 | wc -l
29
$ ls -l | wc -l
30
$ ls | wc -w
31

[The 2nd line is "ls minus one pipe...", the 3rd is "ls minus ell pipe..."]

Oops...the directory "Program Files" is 2 [space separated] words, as is the
directory "My Documents".

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

  #14 (permalink)  
Old 11-14-2007, 12:10 PM
Allodoxaphobia
Newsgroup Contributor
 
Posts: n/a
Re: File Count

On Tue, 13 Nov 2007 10:52:15 -0800, eu.alug******.com wrote:
> On 12 nov, 17:12, "M" <n...@spam.tonsjunkmail.com> wrote:
>> Is there a command that will simply give me a count of files in a given
>> directory? ls lists them and du gives me size of directory. I just want a
>> count.
>>
>> Its for a perl script I am working on.

>
> Try:
>
> /bin/ls | wc -m
> Check, in wc help, the correct argument for words.


ITYM: ls | wc -l

Jonesy
--
Marvin L Jones | jonz | W3DHJ | linux
38.24N 104.55W | @ config.com | Jonesy | OS/2
*** Killfiling google posts: <http://jonz.net/ng.htm>
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

  #15 (permalink)  
Old 11-15-2007, 05:30 AM
Douglas O'Neal
Newsgroup Contributor
 
Posts: n/a
Re: File Count

Allodoxaphobia wrote:
> On Tue, 13 Nov 2007 10:52:15 -0800, eu.alug******.com wrote:
>> On 12 nov, 17:12, "M" <n...@spam.tonsjunkmail.com> wrote:
>>> Is there a command that will simply give me a count of files in a given
>>> directory? ls lists them and du gives me size of directory. I just want a
>>> count.
>>>
>>> Its for a perl script I am working on.

>> Try:
>>
>> /bin/ls | wc -m
>> Check, in wc help, the correct argument for words.

>
> ITYM: ls | wc -l
>
> Jonesy


$ mkdir /tmp/tmp
$ cd /tmp/tmp
$ touch 'a a'
$ /bin/ls | wc
1 2 4

Neither wc -l or wc -w works if you have files with white space.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Reply

Bookmarks

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
File count in WExplorer incorrect Skip Bisconer Windows XP 2 10-01-2007 12:10 AM
WORD COUNT TDT Consulting Microsoft Office 2 07-09-2007 11:11 PM
word count VanessaLou Microsoft OneNote 1 03-04-2007 03:15 AM
Inaccurate File and Byte Count in File Explorer: a bug? Shawnews Windows XP 10 02-26-2007 08:00 PM
File count wrong in folder C.Wilder Windows XP 4 01-27-2007 12:00 AM


New To Technology Questions? Do You Need Help with Your Computer or Device? Do You Need Help with this site?

All times are GMT -8. The time now is 12:02 AM.


2003 - 2009 All Rights Reserved. Technology Questions

Search Engine Friendly URLs by vBSEO 3.3.0