• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 908
  • Last Modified:

using ls and grep command together

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
wshark
Asked:
wshark
  • 3
  • 2
  • 2
  • +2
1 Solution
 
jmcgOwnerCommented:
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
 
glassdCommented:
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
 
TintinCommented:
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
liddlerCommented:
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
 
TintinCommented:
How was this determined to be a homework question?  It doesn't seem obvious to me.
0
 
jmcgOwnerCommented:
I thought that was one of my more magificent answers, perhaps even worth PAQing, even if I don't get any points.
0
 
liddlerCommented:
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
 
glassdCommented:
I'm not fussed. If you want to hand out the points elsewhere then feel free.
0
 
jmcgOwnerCommented:
PAQ is fine by me.
0
 
moduloCommented:
PAQed - no points refunded (of 25)

modulo
Community Support Moderator
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 3
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now