Solved

Unix Command -- Challenging  question

Posted on 2016-09-17
7
78 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 27

Expert Comment

by:serialband
Comment Utility
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
Comment Utility
Hi I mean both . any help is reallly appreciated
0
 
LVL 34

Assisted Solution

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

Open in new window


does that work?
1
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 40

Assisted Solution

by:omarfarid
omarfarid earned 83 total points
Comment Utility
try

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

Assisted Solution

by:Gerwin Jansen
Gerwin Jansen earned 83 total points
Comment Utility
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 27

Accepted Solution

by:
serialband earned 251 total points
Comment Utility
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
Comment Utility
awesome. Thanks a lot.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

763 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now