Questions tagged [wc]

`wc` utility counts newline, word, and byte for file(s) or standard input.

189 questions
49
votes
6 answers

List files sorted by the number of lines they contain

How can I list the number of lines in the files in /group/book/four/word, sorted by the number of lines they contain? The ls -l command lists the files but does not sort them.
Ken R
  • 529
  • 1
  • 5
  • 6
46
votes
4 answers

Why does "ls | wc -l" show the correct number of files in current directory?

Trying to count number of files in current directory, I found ls -1 | wc -l, which means: send the list of files (where every filename is printed in a new line) to the input of wc, where -l will count the number of lines on input. This makes…
Kirill
  • 985
  • 3
  • 10
  • 20
26
votes
3 answers

Byte count of "ls -l " versus that of "wc -c "

Is there any possible situation when ls -l file.txt is showing not the same number of bytes as wc -c file.txt In one script I found comparison of those two values. What could be the reason of that? Is it even possible to have different byte…
Rokas.ma
  • 563
  • 6
  • 14
25
votes
17 answers

How to find line with least characters

I am writing a shell script, using any general UNIX commands. I have to retrieve the line that has the least characters (whitespace included). There can be up to around 20 lines. I know I can use head -$L | tail -1 | wc -m to find the character…
Matthew D. Scholefield
  • 1,009
  • 1
  • 10
  • 17
20
votes
5 answers

how to count total number of words in a file?

I am looking for a command to count number of all words in a file. For instance if a file is like this, today is a good day then it should print 5, since there are 5 words there.
Richard
  • 879
  • 2
  • 8
  • 19
19
votes
2 answers

Why is wc so slow?

Why is the wc utility so slow? When I run it on a large file, it takes about 20 times longer than md5sum: MyDesktop:/tmp$ dd if=/dev/zero bs=1024k count=1024 of=/tmp/bigfile 1024+0 records in 1024+0 records out 1073741824 bytes (1.1 GB) copied,…
Johnny
  • 2,110
  • 17
  • 17
18
votes
3 answers

How to live update `wc -l`?

I have a command | wc -l, it works fine, but the takes a while, which means I don't get the final line count until a few minutes later. Is there a way to have the output of wc -l update in real time?
Foobar
  • 273
  • 2
  • 9
18
votes
3 answers

Why are wc -m and wc -c different?

As a C programmer, I was surprised to see that wc -c (which count the number of bytes), and wc -m (which counts the number of characters) output very different results for a long, text file of mine. I had always been told that sizeof(char) is 1…
qdii
  • 916
  • 1
  • 11
  • 17
14
votes
3 answers

Why does "wc -c" print a value of one more with echo?

When running echo abcd | wc -c it returns 5. But the word abcd is only 4 characters long. Is echo printing some special character after the word abcd? And can I prevent echo from printing that?
user78050
  • 1,045
  • 2
  • 11
  • 16
14
votes
4 answers

How do I get a single total of lines with `wc -l`?

I've added a git alias to give me the line counts of specific files in my history: [alias] lines = !lc() { git ls-files -z ${1} | xargs -0 wc -l; }; lc However, wc -l is reporting multiple totals, such that if I have more than ~100k lines, it…
Ehryk
  • 1,822
  • 3
  • 20
  • 22
13
votes
2 answers

Need something that is faster than "wc -l"

For a really big file like 1GB wc -l happens to be slow. Do we have a faster way calculating the number of newlines for a particular file?
prosti
  • 998
  • 1
  • 7
  • 18
13
votes
8 answers

How do I get the character count of words in a particular column?

I have a CSV file like this: abd,123,egypt,78 cde,456,england,45 How can I get the character count of only the 3rd column words? I can't figure out how to get wc to do this.
user3116123
  • 539
  • 1
  • 6
  • 11
12
votes
4 answers

How to skip the first line of my output?

Here is the issue, I would like to count the number of jobs I have in the hpc, but it is not one of the readily provided features. So I made this simple script squeue -u user_name | wc -l where squeue prints all the jobs like the following > squeue…
zyy
  • 271
  • 1
  • 2
  • 10
12
votes
2 answers

Why does wc <<<"$string" show a one-byte-longer length than printf "$string" | wc?

Accidentially, I found out that wc counts differently depending on how it gets the input from bash: $ s='hello' $ wc -m <<<"$s" 6 $ wc -c <<<"$s" 6 $ printf '%s' "$s" | wc -m 5 $ printf '%s' "$s" | wc -c 5 Is this - IMHO confusing - behaviour…
rexkogitans
  • 1,319
  • 11
  • 17
12
votes
8 answers

Count the number of blank lines at the end of file

I have a file with blank lines at the end of the file. Can I use grep to count the number of blank lines at the end of the file with the file name being passed as variable in the script?
1
2 3
12 13