Solved

using ls and grep command together

Posted on 2003-10-22
10
869 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

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 58
deburging in oracle form 12 105
Fibonacci challenge 11 133
Increment column based of a FK 8 31
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

840 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