Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Locked thread
j6p
Nov 16, 2003
Okay, here's what I hope is an easy one. I am brand new to the world of BSD and have been using some downloaded teaching material to learn my away around. I should note that I am learning on OSX Tiger (I know this isn't an ideal situation to begin my understanding of BSD but it is my only available option at this time). I'm using tcsh in case that's relevant. Today I ran into a problem while attempting to perform an exercise in one of my books:

"Create a command that will list the contents of each subdirectory of the /home directory beginning with the letter k, count the number of files found, and save that number in a file called k-users-files."

The answer to this problem, as given by the book, is:

code:
ls /home/k* | wc > k-users-files
However this does not produce the correct answer, at least on my machine. The output from the ls command lists files in columns with a separate line identifying each subdirectory's path and a blank line to separate each subdirectory listing. So the output of wc (the line, word, and byte count of the resulting "k-users-files" file) doesn't tell me anything about the number of files in the "k" subdirectories because there are extra words and lines being counted.

Is this just an OSX Tiger quirk or did the authors of this book screw up, and what is the "real" answer to this problem?

Adbot
ADBOT LOVES YOU

j6p
Nov 16, 2003
Thanks for your input. Now that you've put it that way, I guess my problem is also one of grammar. It is difficult to tell by the way the question is worded if I am supposed to count files only in subdirectories that begin with the letter k or if I am supposed to count files that begin with the letter k in all subdirectories. In the latter case then your code will indeed produce the correct answer - you could even add the -l argument to the wc command to get nothing but the correct answer.

Your interpretation of the question is probably correct, insofar as it seems a more likely real-world scenario. With that solved, though, can anyone produce a command to solve the former interpretation of the question? That is, how would one go about listing all files in subdirectories that begin with the letter k?

  • Locked thread