Macworld Forums: Counting files with ls - Macworld Forums

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Counting files with ls

#1 User is offline   sereluna Icon

  • Veteran
  • PipPipPip
  • Group: Members
  • Posts: 1,037
  • Joined: 24-February 04

Posted 05 May 2006 - 03:49 PM

How do I use ls to get the number of files in a directory and all the directories beneath it?
0

#2 User is offline   Tom_Diola Icon

  • Veteran
  • PipPipPip
  • Group: Members
  • Posts: 2,273
  • Joined: 19-October 01

Posted 05 May 2006 - 07:32 PM

I didn't try this yet but maybe try this guy's suggestion: web page
My disclaimer: 1) There's an old saying in unix "not can you do something in unix - but how can I do it in unix... 2) Please take unix seriously - When I was first starting out using AIX (a variation of unix) I succeeded in erasing an entire hard drive but playing with the scripts out of the books ... don't treat unix like windoze and hope for the best ... try to see what the commands/scripts are doing (work through them and then and only then execute them).
0

#3 User is offline   Nobody Icon

  • Power User
  • PipPipPipPip
  • Group: Members
  • Posts: 58,347
  • Joined: 18-October 07

Posted 05 May 2006 - 11:34 PM

AIX --- that brought back some memories... nightmarish ones!
But I did get better at it by keeping all the needed commands neatly typed by my side on day-2 onwards. /forums/ubbthreads/images/graemlins/grin.gif
0

#4 User is offline   sereluna Icon

  • Veteran
  • PipPipPip
  • Group: Members
  • Posts: 1,037
  • Joined: 24-February 04

Posted 06 May 2006 - 06:26 AM

code:
ls | wc -l

works well for a directory with no subdirectories, but if I do
code:
ls -R | wc -l

it counts all the subdirectories, and blank lines in the output of ls. Can I use grep or something to exclude any blank lines, or those starting with ./?
Actually, I'm not sure exactly what wc is counting:
code:
$ ls -R
dir1 dir2 file1 file2 file3
./dir1:
file1 file2
./dir2:
file1 file2
$ ls -R | wc -l
13


0

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

4 User(s) are reading this topic
0 members, 4 guests, 0 anonymous users