LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-26-2007, 04:21 AM   #1
7stud
LQ Newbie
 
Registered: Feb 2007
Posts: 22

Rep: Reputation: 15
ls pipes and wc


Hi,

According to the definition of wc(word count), it takes a filename or a list of filenames as input. If there is more than one filename specifed for input, then wc will produce one line of output for each file.

According to the definition of ls, it "displays" the names of files.

Now suppose I do this:

ls | wc

I would expect ls to "display" multiple filenames, which are piped to wc. Since the definition of wc says it will give a word count for each filename it receives as input, I would expect multiple lines of output. However, the actual result is a single line of output--as if wc received only one filename. In other words, the output of the command above would appear to be equivalent to this:

ls > some_file | wc

What's even more confounding is that the last command does not even work: wc outputs 0 0 0--even though there are several files in the working directory and upon checking I can see multiple lines of data in some_file.

Can someone help me out with an explanation or point me to a tutorial which addresses this issue?

Last edited by 7stud; 02-26-2007 at 04:26 AM.
 
Old 02-26-2007, 04:28 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974
using a pipe is not the same as supplying a filename. what it means is that the data piped in is the FILE, not eh FILENAME. as such, it'll do a wc of the ls output. the reason the last command there doesn't work, is that you've already sent all the output to a text file, so wc recieved no data at all.

you presumably want to do somethign like

wc *

not

ls | wc
 
Old 02-26-2007, 05:24 AM   #3
7stud
LQ Newbie
 
Registered: Feb 2007
Posts: 22

Original Poster
Rep: Reputation: 15
Hi,

Thanks for the response.

Quote:
Originally Posted by acid_kewpie
using a pipe is not the same as supplying a filename. what it means is that the data piped in is the FILE, not eh FILENAME.
So any output from a command is placed into a temporary file? Or, how would you phrase that?

Quote:
the reason the last command there doesn't work, is that you've already sent all the output to a text file, so wc recieved no data at all.
Ahh, I see.

Quote:
you presumably want to do somethign like

wc *

not

ls | wc
Actually, I want the ls|wc output, I just never thought to try that because I assumed it would supply multiple filenames to wc, which would give me the word counts inside each of those files--and I was just trying to count the number of files returned by ls. The approach I took was to redirect the filenames to a file and then attempt to pipe the filename to wc, which didn't work.

Last edited by 7stud; 02-26-2007 at 05:35 AM.
 
Old 02-26-2007, 05:45 AM   #4
timmeke
Senior Member
 
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515

Rep: Reputation: 61
Quote:
Originally Posted by 7stud
So any output from a command is placed into a temporary file? Or, how would you phrase that?
That's not 100% true, but it's kind of like that. You should read up on file descriptors and named/unnamed pipes for more information.

Simply said, a pipe copies the output of one command to the input (standard input) of the next one.
What "wc" does, is simply this:
If you supply one or more filenames as command line parameters, it'll do it's computations (line/word counting) on the files given.
If you give it some text on input, it'll do it's computations directly on that input, without supposing that the supplied input actually represents some file names. So, using "ls|wc", wc doesn't compute the line/word count for the files mentioned by the ls output, but rather on the file listing itself.
So, the point is that command line parameters supplied to a command are not necessarily treated the same way as the input (on standard input/stdin).

"wc some_filename_pattern" or "wc *" (for all files in current directory) are the easiest ways to accomplish what you're trying.

Another solution would be the "xargs" command. This transforms it's input to command line parameters.
So, you could do something like:
ls | xargs wc
In this case, xargs takes all the filenames listed by ls and executes "wc", using the specified files as parameters (so it actually executes "wc filenames_from_ls").
 
Old 02-26-2007, 06:10 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Quote:
Originally Posted by 7stud
--and I was just trying to count the number of files returned by ls
You can use options of wc command to retrieve what you want. In the case above, you will simply do
Code:
ls | wc -l
which will count the number of lines in the output from ls (please note that using ls this way, it will put one file per line, so the output of the command above is exactly the number of items in the current directory).
wc without options gives newline, word, and byte counts, hence the three numbers you got (see man wc for details).
Just a note: on some systems (usually UNIX) the ls command put also . and .. in the output, so you can get the number of files/directories inside the current dir plus 2, e.g.
Code:
> ls -1 -F
.
..
testfile/
testfile.awk
testfile.sh*
testfile.txt

> ls | wc -l
6

Last edited by colucix; 02-26-2007 at 06:11 AM.
 
Old 02-26-2007, 06:22 PM   #6
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
And if you really want to use a pipe to pass things to wc, you could
use
Code:
ls -1 *.txt|xargs wc -l

Cheers,
Tink
 
Old 02-26-2007, 09:17 PM   #7
7stud
LQ Newbie
 
Registered: Feb 2007
Posts: 22

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by colucix
You can use options of wc command to retrieve what you want. In the case above, you will simply do
Code:
ls | wc -l
which will count the number of lines in the output from ls (please note that using ls this way, it will put one file per line, so the output of the command above is exactly the number of items in the current directory).
Yep. That's what I did.


Quote:
Just a note: on some systems (usually UNIX) the ls command put also . and .. in the output, so you can get the number of files/directories inside the current dir plus 2, e.g.
Code:
> ls -1 -F
.
..
testfile/
testfile.awk
testfile.sh*
testfile.txt

> ls | wc -l
6
I'm using mac os x, and I would have to do a ls -a to get all the hidden files, which include . and ..

Thanks for all the replies everyone.
 
Old 02-27-2007, 08:24 AM   #8
Hobbletoe
Member
 
Registered: Sep 2004
Location: Dayton, Oh
Distribution: Linux Mint 10, Linux Mint 11
Posts: 148

Rep: Reputation: 18
You can use the -A option with ls to get all of the hidden files except for . and .. .
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
pipes in python? SciYro Programming 9 08-02-2006 10:35 AM
pipes smart girl Programming 6 04-30-2006 11:20 AM
about pipes kpachopoulos Programming 1 10-15-2005 01:37 PM
Question regarding pipes alltime Programming 3 04-15-2005 08:54 PM
devices and pipes siemens Linux - General 1 03-23-2005 04:30 AM


All times are GMT -5. The time now is 12:39 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration