?
Solved

using ls and grep command together

Posted on 2003-10-22
10
Medium Priority
?
885 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
Get MySQL database support online, now!

At Percona’s web store you can order your MySQL database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Suggested Courses

765 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