Solved

using ls and grep command together

Posted on 2003-10-22
10
874 Views
Last Modified: 2013-12-26
how can i use an ls and grep command to count the number of all files in the working directory including the hidden ones i.e. the . and the .. file. and also how can i use the same type of command to count sub directories in the working directory including the hidden ones.

0
Comment
Question by:wshark
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +2
10 Comments
 
LVL 20

Expert Comment

by:jmcg
ID: 9604098
Welcome to Experts-Exchange and the UNIX Programming section.

You haven't mentioned which programming language you were thinking about when you asked this question. I'll have to guess that you just want to be able to do this from shell scripts.

The command 'ls' will list all the files in a directory except those whose names begin with a "." (what you've called "hidden"). The command 'ls -a' will list all the files in a directory including those whose names begin with a "." (including the link named "." which refers to the directory itself and the link named ".." which refers to this directory's parent).

We often use the 'wc' command in shell scripts to count things like lines; it's a little more compact than writing a counting loop in the shell.

So, to count all the files in a directory, we would do something like:

  ls -a | wc -l

To find subdirectories (and their subdirectories) we need a more specialized tool: the 'find' command.

  find . -type d -print | wc -l

will give a count of all directories (type "d") below the current directory (but by convention, it omits the . and .. entry in each directory).

If you just wanted a list of one level of subdirectories of a particular directory, you can take advantage of a feature that most modern shells have: if you give a trailing slash to a wildcard argument, only directory names that match the wildcard will be substituted. Thus

  echo */ | wc -l

will give you a count of subdirectories if there are any. If there aren't any, you'll either get an error message or a bogus count of 1, depending on how your shell treats non-matching wildcard patterns.

I hope this helps a bit. I realize I haven't addressed the use of 'grep' but the problem you've set does not appear to be one where 'grep' offers much advantage.


0
 
LVL 7

Expert Comment

by:glassd
ID: 9605149
If you want to count the files (including directories) in the current directory, as suggested above:

   ls -a | wc -l

If you want just the files:

   find * -prune -type f | wc -l

And only the directories:

   find * -prune -type d | wc -l

Although this does not include . and .. (why should you need them?)

You could do similar things using ls -l, grepping for the first character (d for directory) and extracting the last field with awk. If you want to go down that root then just say.
0
 
LVL 48

Expert Comment

by:Tintin
ID: 9610660
If you *really* did want to use *only* ls and grep, then these will work

All files in your current directory (including hidden files)

ls -aF | grep -v '/' | grep -c .

To count all of the subdirs from your current directory (including hidden dirs)

ls -aFR |grep  ':' | grep -c .
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 18

Expert Comment

by:liddler
ID: 10191498
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Delete no refund  (homework)

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

liddler
EE Cleanup Volunteer
0
 
LVL 48

Expert Comment

by:Tintin
ID: 10197660
How was this determined to be a homework question?  It doesn't seem obvious to me.
0
 
LVL 20

Expert Comment

by:jmcg
ID: 10198520
I thought that was one of my more magificent answers, perhaps even worth PAQing, even if I don't get any points.
0
 
LVL 18

Expert Comment

by:liddler
ID: 10199692
Tintin,
it was the "...how can i use an ls and grep...", rather than "how would I ?" tone of the Q that made me think it was a homework Q.   I'm happy to be wrong and if so, all three were excellent answers.  25 points can't be split, so PAQ or points to jmcg?
Comments?
0
 
LVL 7

Expert Comment

by:glassd
ID: 10200229
I'm not fussed. If you want to hand out the points elsewhere then feel free.
0
 
LVL 20

Expert Comment

by:jmcg
ID: 10200908
PAQ is fine by me.
0
 

Accepted Solution

by:
modulo earned 0 total points
ID: 10241700
PAQed - no points refunded (of 25)

modulo
Community Support Moderator
0

Featured Post

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
repeatSeparator  java  challenge 13 59
Smart Camera scanning and reading information 3 126
Excel file not created as expected 7 108
Adjust the codes 3 63
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question