Solved

using ls and grep command together

Posted on 2003-10-22
10
860 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
  • 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

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: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
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.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now