The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Help it works but its to SLOW Garlandxj Shell Programming and Scripting 10 04-03-2008 12:17 AM
How does NORET_TYPE works ? amit4g Linux 1 01-28-2008 02:27 AM
Works with FTP but not NFS pbonilla UNIX for Dummies Questions & Answers 1 01-08-2008 01:31 PM
how domain name reg works vutty81 IP Networking 4 11-20-2007 02:00 PM
Code works sometimes and sometimes not hp-aix-lnx-user Shell Programming and Scripting 2 04-06-2006 08:43 AM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-11-2008
Registered User
 

Join Date: Sep 2006
Location: Mysore, India
Posts: 153
How ls | wc -l works?

ls displays files in tabbed output. Say a directory contains 3 files. ls will list all 3 in one line. So, I expect ls | wc -l to give 1, but it counts the nr of files and gives 3.

Can someone explain how this works?
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 04-11-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 2,707
Hint: ls | cat

It displays differently when it's connected to a terminal (human-readable) than when it's not (machine-readable). This is a feature, but it can be a problem, too.
Reply With Quote
  #3 (permalink)  
Old 04-11-2008
Registered User
 

Join Date: Sep 2006
Location: Mysore, India
Posts: 153
oh, yes! Thanks for the clarification.
Reply With Quote
  #4 (permalink)  
Old 04-11-2008
joeyg's Avatar
premier etoile de match
 

Join Date: Dec 2007
Location: Home of world champion Boston Celtics
Posts: 511
Wink

From the manpage:
Code:
     There are three major listing formats.  The  default  format
     for  output  directed  to  a  terminal  is multi-column with
     entries sorted down the columns. The -1 option allows single
     column  output and -m enables stream output format. In order
     to determine output formats for the -C, -x, and -m  options,
     ls  uses  an environment variable, COLUMNS, to determine the
     number of character positions available on one output  line.
     If  this  variable  is  not set, the terminfo(4) database is
     used to determine  the  number  of  columns,  based  on  the
     environment  variable,  TERM.  If this information cannot be
     obtained, 80 columns are assumed.
What you are seeing in your example
> ls | wc -l
> 3

is related to the number of columns that can be displayed.
whap is happening is similar to
> ls -1 | wc -l
> 3

and here, the output was forced to just one column
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 06:04 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0