Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Unix Command -- Challenging  question

Posted on 2016-09-17
7
Medium Priority
?
126 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
[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
7 Comments
 
LVL 30

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 35

Assisted Solution

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

Open in new window


does that work?
1
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 40

Assisted Solution

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

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

Assisted Solution

by:Gerwin Jansen, EE MVE
Gerwin Jansen, EE MVE earned 332 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 30

Accepted Solution

by:
serialband earned 1004 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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
In this post we will learn different types of Android Layout and some basics of an Android App.
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Suggested Courses

618 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