Questions tagged [wc]

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

Filter by
Sorted by
Tagged with
43
votes
4answers
122k views

Why does “ls | wc -l” show the correct number of files in current directory? [duplicate]

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 ...
36
votes
6answers
27k views

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? ls -l command lists them down but does not sort them
26
votes
3answers
1k views

Byte count of “ls -l <random file>” versus that of “wc -c <random file>”

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 ...
25
votes
17answers
6k views

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 ...
19
votes
5answers
90k views

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.
18
votes
2answers
5k views

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 ...
15
votes
2answers
6k views

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 ...
13
votes
2answers
4k views

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?
13
votes
3answers
8k views

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?
13
votes
4answers
12k views

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, ...
12
votes
8answers
12k views

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.
11
votes
2answers
3k views

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 $ ...
11
votes
8answers
8k views

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?
11
votes
4answers
34k views

How to use wc and piping to find how many files and directories are in a certain directory?

How can I use word counter (wc) and piping to count how many files or directories are in the /usr/bin directory?
10
votes
4answers
10k views

How to skip the first line of my output? [duplicate]

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 ...
9
votes
7answers
4k views

How to efficiently split up a large text file wihout splitting multiline records?

I have a big text file (~50Gb when gz'ed). The file contains 4*N lines or N records; that is every record consists of 4 lines. I would like to split this file into 4 smaller files each sized roughly ...
8
votes
5answers
10k views

Get the number of spaces characters in a string?

How can I check how many white spaces (' ', \t) there are in the first line of a file?
8
votes
3answers
8k views

Get correct number of lines in diff output

I want to get the correct number of lines in the output of diff(specifically with -y and --suppress-common-lines options). Using a simple wc -l does not work, because if both files end without a ...
7
votes
3answers
2k views

wc command counts extra characters

cat > file Amy looked at her watch. He was late. The sun was setting but Jake didn’t care. wc file 1 16 82 file Can somebody explain why wc command returns 3 extra characters in this ...
6
votes
3answers
9k views

How does word count work for newline (-l)? Number of newlines or lines

I wonder how does wc work? Most times it seems to return the number of lines, but sometimes it seems to be newlines? In the man page its newlines. But: [jiewmeng@JM textFiles]$ echo -e "\n\n" | wc - ...
6
votes
3answers
7k views

How to get the number of bytes in just one line of a file?

I am wondering how I can get the number of bytes in just one line of a file. I know I can use wc -l to get the number of lines in a file, and wc -c to get the total number of bytes in a file. What I ...
6
votes
1answer
2k views

Finding number of lines using find command

Consider the R source code repository found at https://cloud.r-project.org/src/base/R-3/R-3.4.4.tar.gz . I unzipped the repository into a folder. Now, I wish to find out how many total lines are ...
6
votes
3answers
2k views

How to count the number of lines in a UTF-16LE/CR-LF/BOM file?

The immediate thought is wc, but then the next not-so-immediate thought is... Is *nix's wc purely for *nix line endings \x0a?... It seems so. I've semi-wangled my way around it, but I feel there ...
6
votes
3answers
14k views

Count number of characters per listed filename

Given the following ls result: $ ls Desktop Documents Downloads Music Pictures Public Templates Videos This will give the total number of characters for all files: $ ls | wc -m 67 But how ...
6
votes
9answers
478 views

Position, by word count, of all repetitions of a word in a text file

I want to find where a word appears in a text file — as in the number of words into the text that a word occurs — for all instances of that word, but I'm not sure even where to start. I imagine I'll ...
6
votes
2answers
4k views

Character count in Unix wc command

When I give wc command on a file with contents given below, it gives the number of characters as 30. Does it include the end of file character? Since including the space and newline there are only 29 ...
6
votes
3answers
5k views

Running external commands in vim

It seems that if I run the following inside the vim: :w !wc -w I get the word count of the file. But I don't understand the syntax. How does this work and how would I specify that I want the ...
6
votes
4answers
354 views

Count lines of non-terminating input

Is there any way to count the lines of a non-terminating input source? For example, I want to run the following for a period of time to count the number of requests: ngrep -W byline port 80 and dst ...
5
votes
3answers
16k views

Counting the characters of each line with wc

Is it possible to use wc to count the chars of each line instead of the total amount of chars? e.g. echo -e foo\\nbar\\nbazz | grep -i ba returns: bar bazz So why doesn't echo -e foo\\nbar\\...
5
votes
2answers
2k views

Counting lines without text in a file

I am using Ubuntu 13.10 for a while now, programming in C++ and Python. I have some question that I can solve by making a program, but maybe there are easier ways, so here is the first question. I ...
5
votes
1answer
2k views

command like wc but with tee behaviour

I want to backup a database using psql and COPY command. here is my script: psql "user=${USERNAME} host=${HOSTNAME} dbname=${DBNAME} password=${PASSWORD}" -c \ "COPY (SELECT * FROM tbl) ORDER BY id ...
5
votes
4answers
2k views

Count lines matching pattern and matching previous line

I want to count the number of times that a role is successfully deleted. The problem is that one line of the log file will show that the script is about to delete the role: Prepare to remove role X ...
5
votes
5answers
6k views

List files with line count and date

I want to be able to list files showing the number of lines the each file has and the date. I can happily get the line count using wc -l *. Not a problem. I can get the date using ls -l. Is there a ...
5
votes
3answers
28k views

Run wc and display only the word count

I have been trying to create a file called ~/dictwords.txt, which contains the number of words found in the file /usr/share/dict/words. I already created the file then I removed it because I did it ...
5
votes
2answers
9k views

Why does wc -l tell me that this non-empty file has 0 lines?

xclip/xsel are utilities that allow you to pipe clipboard output to the shell on Ubuntu. Unfortunately, they seem to fail in combination with the wc command. Try this. Open a new file and type: ...
5
votes
6answers
4k views

How can I split a large text file into chunks of 500 words or so?

I know there's the command split which can split a file into several chunks based on file size, and then there's the command wc which can count words. I just don't know how to use the two together.
4
votes
6answers
1k views

unix: get characters 10 to 80 in a file

I have a file containing line-separated text: GCAACACGGTGGGAGCACGTCAACAAGGAGTAATTCTTCAAGACCGTTCCAAAAACAGCATGCAAGAGCG GTCGAGCCTAGTCCATCAGCAAATGCCGTTTCCAGCAATGCAAAGAGAACGGGAAGGTATCAGTTCACCG ...
4
votes
1answer
2k views

wc -l not returning correct value

I have a .txt file that has 10781 lines in it. When I run wc -l on the file I get a return of 10780. If I add a blank line at the end of the .txt I get the correct result of 10781. Can someone ...
4
votes
3answers
2k views

Copy a specific percentage of each file in a directory to a new file

For example, we have N files (file1, file2, file3 ...) We need first 20% of them, the result directory should be like (file1_20, file2_20, file3_20 ...). I was thinking use wc to get the lines of ...
4
votes
4answers
13k views

count number of files in directory with a certain name

I found this code here https://www.tldp.org/HOWTO/Bash-Prompt-HOWTO/x700.html which nicely gives me the number of files in my directory. ls -1 | wc -l but I only want to know how many of those files'...
4
votes
4answers
4k views

Extra space with counted line number?

I count the number of lines of my file with this command on OSX: nl=$(wc -l < ~/myfile.txt) Say, nl turns out to be 100. Now, I wish to use the result nl in another command, but weirdly, echo 1-$...
4
votes
2answers
3k views

Number of lines outputted by ls

When ls is called, it outputs all the files/directories in the current directory, attempting to fit as many as possible on each line. Why is it that when passed to wc -l, it outputs the number of ...
4
votes
2answers
394 views

Is there a way to count the number of lines of text in a file including non-delimited ones?

The POSIX wc command counts how many POSIX lines in a file. The POSIX standard defines a line as a text string with the suffix \n. Without \n, a pure text string can't be called a line. But to me, ...
4
votes
2answers
3k views

wc -c gives me one extra character count

So I made a script that adds users to the system, and I wanted to force the length of the usernames to 8 characters or below. #!/bin/bash # Only works if you're root for ((a=1;a>0;a)); do if [[ ...
4
votes
5answers
5k views

get file size and line count at the same time

I'm looking for a way to list all files in a directory, with both their size, and a line count. Right now I'm using stat -c \"%s %n\" /directory/* to get file names and sizes, and I know I can use ...
4
votes
2answers
171 views

Difference between wc -c and ${#}?

I've created script, where I encoded string 28 times with base64. Then I wanted to echo count of chars in outputted string. First I used ${#var} which gave me 34070. Then I tried on the same string ...
4
votes
3answers
6k views

How to make wc interpret standard in as a file list

I know that there are other ways to go about this, but I'm looking to be able to make wc interpret stdin as a file name or list of file names. For example, ls JP*/std* | wc would work the same as ...
4
votes
1answer
8k views

The simplest method to count lines matching specific patterns, including '0' if line is not found?

I have very big logs (several gigabytes per day), that can (but do not need to) contain specific lines. I have to count the number of occurences of every one of these lines on a daily basis. I have a ...
4
votes
1answer
2k views

Send random data over TCP for a while, and count how many bytes were sent

To test data throughput, I want to 1) for X seconds 2) send random data 3) over TCP, and 4) to know afterwards exactly how many bytes were transmitted. My best attempt (which is not much) 1) timeout ...
4
votes
3answers
1k views

Time required to do pipe output to head/tail [duplicate]

There are lots of txt files in a directory. If I do time wc -l *.txt | head it takes real 0m0.032s user 0m0.020s sys 0m0.008s If I do time wc -l *.txt | tail it takes real 0m0.156s ...