Solved

Unix Command -- Challenging  question

Posted on 2016-09-17
7
110 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 83 total points
ID: 41803178
find . -maxdepth 1 -type f | wc -l

Open in new window


does that work?
1
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
LVL 40

Assisted Solution

by:omarfarid
omarfarid earned 83 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 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 30

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
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…
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.

717 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