LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-14-2006, 01:37 AM   #1
sarajevo
Member
 
Registered: Apr 2005
Distribution: Debian, OpenBSD,Fedora,RedHat
Posts: 228
Blog Entries: 1

Rep: Reputation: 31
ls |wc -l and ls -l|wc -l what is difference


Hi, I have some some directory with folders in it. i just need to count how many folders resides in that directory.
When I use

ls |wc -l I got 74

When I use ls -l|wc -l I got 75

or directly from shell

[frenky@web 13]$ ls |wc -l
74
[frenky@bweb 13]$ ls -l|wc -l
75
[frenky@bweb 13]$

I am asking all of you out there what is difference between above two commands.

Thanks in advance.

Regards
 
Old 06-14-2006, 02:56 AM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
If you look carefully, you'll see an extra line like
total 108
for ls -l
 
Old 06-14-2006, 05:20 AM   #3
addy86
Member
 
Registered: Nov 2004
Location: Germany
Distribution: Debian Testing
Posts: 332

Rep: Reputation: 31
Use
ls -1 | wc -l
This prints exactly one file name per line, but omits the header "total N".
Note: the parameter to ls is a "one", not an "ell".
 
Old 06-14-2006, 05:21 AM   #4
ppanyam
Member
 
Registered: Oct 2004
Location: India
Distribution: Redhat
Posts: 88

Rep: Reputation: 15
I created 26 files in a new directory with names a,b,c,..z and tried your problem.

For ls | wc -l, the answeer was 1.

For ls -l| wc -l, the answer was 27.

ls writes more than 1 file name to output, if the output window width is sufficient. In my case, it could write all 26 file names in a single line.

In case of ls -l, it found 26 lines for 26 files and 1 summary line

ppanyam
 
Old 06-14-2006, 05:28 AM   #5
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Rep: Reputation: 282Reputation: 282Reputation: 282
Please note that the output of ls depends on the alias (if any). On my system,
Code:
alias ls='/usr/bin/ls $LS_OPTIONS'
and $LS_OPTIONS set to --color=auto -F -b -T 0, ls only gives files and directories without additional info and with as many on a line as fits.

Your approach on my system will definitely give the incorrect results with the ls command.

Edit: ppanyam was quicker

Last edited by Wim Sturkenboom; 06-14-2006 at 05:33 AM.
 
Old 06-14-2006, 06:13 AM   #6
sarajevo
Member
 
Registered: Apr 2005
Distribution: Debian, OpenBSD,Fedora,RedHat
Posts: 228

Original Poster
Blog Entries: 1

Rep: Reputation: 31
And you mean diffence is that ls -l|wc -l prints all lines plus total line??

 
Old 06-14-2006, 11:32 PM   #7
ppanyam
Member
 
Registered: Oct 2004
Location: India
Distribution: Redhat
Posts: 88

Rep: Reputation: 15
ls -l prints one line per file/dir, and the summary line. wc -l counts the number of lines in input.
 
Old 06-15-2006, 11:34 PM   #8
cyent
Member
 
Registered: Aug 2001
Location: ChristChurch New Zealand
Distribution: Ubuntu
Posts: 398

Rep: Reputation: 87
find . -type d | wc
 
Old 06-16-2006, 01:59 AM   #9
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
you mean -type f ?


Cheers,
Tink
 
Old 06-16-2006, 02:09 AM   #10
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Quote:
Originally Posted by ppanyam
I created 26 files in a new directory with names a,b,c,..z and tried your problem.

For ls | wc -l, the answeer was 1.

For ls -l| wc -l, the answer was 27.

ls writes more than 1 file name to output, if the output window width is sufficient. In my case, it could write all 26 file names in a single line.

In case of ls -l, it found 26 lines for 26 files and 1 summary line

ppanyam
Quote:
from info coreutils ls
Code:
   By default, the output is sorted alphabetically, according to the
locale settings in effect. (1) If standard output is a terminal, the
output is in columns (sorted vertically) and control characters are
output as question marks; otherwise, the output is listed one per line
and control characters are output as-is.
That clearly means that you must have done something
to your ls if it outputs files in columns when piping to wc.


Cheers,
Tink
 
Old 06-18-2006, 05:11 PM   #11
cyent
Member
 
Registered: Aug 2001
Location: ChristChurch New Zealand
Distribution: Ubuntu
Posts: 398

Rep: Reputation: 87
Foncusions, folders, files and directories...

Quote:
Originally Posted by Tinkster
you mean -type f ?
I think this whole thread is getting a tad confused from the word go...

In the windows world they decided, for some weird reason, to introduce the term "Folder" to mean the same thing as "Directory".

In the unix world, there are no folders, only files and directories.

Let me just check something quickly...

Fire up konqueror (I usually use command line, not gui stuff)

Aww cwap!! The KDE types have copied Windows and called directories folders. Grumble moan (irritating puppies) whinge (arthritis) growl (gout) hrrumph!

Anyhoo, a Folder === Directory. Directory is older name.

So, no I did mean...
find . -type d | wc
..to count ddirectories (folders)

And
find . -type f | wc
..to count ffiles.

Sigh!
 
Old 06-19-2006, 12:11 AM   #12
ppanyam
Member
 
Registered: Oct 2004
Location: India
Distribution: Redhat
Posts: 88

Rep: Reputation: 15
Quote:
That clearly means that you must have done something
to your ls if it outputs files in columns when piping to wc.


Cheers,
Tink
Sorry, I checked it on UNIX(SGI machine), for which "ls" means

Quote:
Multi-column output with entries sorted along columns
It may be different on different systems. Even in Linux, if you have 100 files in your directory, will "ls" show them in 100 rows?

ppanyam
 
Old 06-19-2006, 03:19 AM   #13
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Quote:
Originally Posted by cyent
I think this whole thread is getting a tad confused from the word go...

In the windows world they decided, for some weird reason, to introduce the term "Folder" to mean the same thing as "Directory".

In the unix world, there are no folders, only files and directories.

Let me just check something quickly...

Fire up konqueror (I usually use command line, not gui stuff)

Aww cwap!! The KDE types have copied Windows and called directories folders. Grumble moan (irritating puppies) whinge (arthritis) growl (gout) hrrumph!

Anyhoo, a Folder === Directory. Directory is older name.

So, no I did mean...
find . -type d | wc
..to count ddirectories (folders)

And
find . -type f | wc
..to count ffiles.

Sigh!
I think the problem that I have is that one guy in the thread is counting
directories, and the other one is creating files.


Thanks for that explanation of find, though, but I don't refer to
directories as folders, and know how to use find, I do Unix/Linux
for a living.
;}


Cheers,
Tink

P.S.: Is the weather in CHCH as horrid as it is in WLGT at the moment?
 
Old 06-19-2006, 04:37 PM   #14
cyent
Member
 
Registered: Aug 2001
Location: ChristChurch New Zealand
Distribution: Ubuntu
Posts: 398

Rep: Reputation: 87
I guessed you knew, but the OP was happily mixing the terms Directory and Folders so I wondered.

Weather, blooming freezing. Ice on the roads in car park on cars brrrr.

Mountains are really snowy, probably good skiing coming up.
 
  


Reply



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
Difference Between..... SmithSmith123 Linux - Certification 1 11-17-2005 02:51 AM
difference between df -k and du -s Prasun1 Linux - General 3 06-22-2005 06:07 PM
What's the difference? fannymites Linux - Software 4 12-01-2004 05:01 PM
is there any difference/use of su - twice? darkleaf Linux - Software 2 10-12-2004 04:54 PM
difference ... Iulian_B Linux - Networking 2 05-29-2002 04:23 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:21 PM.

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
Open Source Consulting | Domain Registration