Unix Command -- Challenging question

Hi Team,

I need an help on the unix command, I need to write an single command which will display the number of files in the current directory.
I tried the below command , but not getting the desired output.

ls-al | grep "^d|^ " | wc -l

Any help is really appreciated.
sam_2012Asked:
Who is Participating?
 
serialbandConnect With a Mentor Commented:
Oh, that was a pipe.  EE fonts have always been quite horrid for code clarity.  You should use egrep. as suggested.

ls -al | egrep "^d|^-" | wc -l

Open in new window



or just use

ls -al | grep "^[d-]" | wc -l

Open in new window

0
 
serialbandCommented:
What are you trying to do with the grep  That should return nothing or blank because there's no way you can have the start (^) of line in 2 places.

Are you trying to just display files, and not links nor directories?
ls -la |grep -v "^d" |grep -v "^l"|wc
0
 
sam_2012Author Commented:
Hi I mean both . any help is reallly appreciated
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
Seth SimmonsConnect With a Mentor Sr. Systems AdministratorCommented:
find . -maxdepth 1 -type f | wc -l

Open in new window


does that work?
1
 
omarfaridConnect With a Mentor Commented:
try

ls-al | grep "^d|^-" | wc -l
0
 
Gerwin Jansen, EE MVEConnect With a Mentor Topic Advisor Commented:
If you're trying unix commands and they don't work, kindly post the error you are getting.

Your command starts with "ls-al" which most likely will not work (unless you have an alias set) because there is a space missing, your command should be "ls -al" (yet it would return 0 files found).

One minor correction would have given you a number: using egrep instead of grep - egrep will handle regular expressions (your "^d|^ " is a regular expression that will search for lines beginning with a d or a space).

The -a will list hidden files (they start with a dot in the name) as well, is this what you want?

If you're not interested in hidden files and directories, this will give you a number of files:

ls -l | grep -v ^d | wc -l
0
 
sam_2012Author Commented:
awesome. Thanks a lot.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.