Solved

Unix Command -- Challenging  question

Posted on 2016-09-17
7
90 Views
Last Modified: 2016-09-17
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.
0
Comment
Question by:sam_2012
7 Comments
 
LVL 28

Expert Comment

by:serialband
ID: 41803136
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
 

Author Comment

by:sam_2012
ID: 41803166
Hi I mean both . any help is reallly appreciated
0
 
LVL 34

Assisted Solution

by:Seth Simmons
Seth Simmons earned 83 total points
ID: 41803178
find . -maxdepth 1 -type f | wc -l

Open in new window


does that work?
1
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 
LVL 40

Assisted Solution

by:omarfarid
omarfarid earned 83 total points
ID: 41803215
try

ls-al | grep "^d|^-" | wc -l
0
 
LVL 37

Assisted Solution

by:Gerwin Jansen, EE MVE
Gerwin Jansen, EE MVE earned 83 total points
ID: 41803305
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
 
LVL 28

Accepted Solution

by:
serialband earned 251 total points
ID: 41803318
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
 

Author Closing Comment

by:sam_2012
ID: 41803414
awesome. Thanks a lot.
0

Featured Post

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
Fine Tune your automatic Updates for Ubuntu / Debian
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
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.

809 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