wcCount lines, words and bytes
wc counts lines, words, characters and bytes in files or in whatever you pipe into it. wc -l at the end of a pipeline is the standard way to answer 'how many?': errors in a log, files in a directory, connections, users, rows in a CSV.
wc [-l] [-w] [-c] [-m] [-L] [FILE]...You want to know how big a log is in lines, and compare files at a glance.
grepSelect the lines you want to count.
wc reads its input in large blocks and counts as it goes; nothing is kept in memory, so it handles files of any size. -l counts newline bytes (coreutils uses a vectorised fast path for this, so wc -l runs at disk speed); a final line without a trailing newline is therefore not counted. -c counts bytes, and for a regular file with -c alone wc can simply take the size from fstat without reading. -w counts words, sequences of non-whitespace characters. -m counts characters, which depends on the locale: in UTF-8 'é' is one character but two bytes. With several files wc prints a line per file and a total, padding the numbers into columns; when it reads standard input it prints no file name, which is why wc -l < file is the idiom for getting a bare number in scripts. Output order is always lines, words, characters, bytes, whatever order you give the options.
-lCount lines (newline characters)-wCount words (whitespace-separated)-cCount bytes-mCount characters (multibyte-aware, locale-dependent)-LLength of the longest line< fileRead stdin: print the number only, no file name--files0-from=FRead NUL-separated file names (from find -print0)ss -tn | wc -l or ps aux | wc -l as the countBoth commands print a header line, so the count is off by one.
ss -tnH | wc -l, ps aux --no-headers | wc -l, or tail -n +2 firstecho "$var" | wc -c to measure a stringecho appends a newline, so the result is one too high (and wc -c counts bytes, not characters).
printf '%s' "$var" | wc -m, or ${#var} in bashCOUNT=$(wc -l file) and then using $COUNT in arithmeticWith a file argument wc prints '120 file', so $COUNT contains the file name too.
COUNT=$(wc -l < file)find . -name '*.log' | wc -l with file names that contain newlinesEach newline in a name is counted as an extra file.
find . -name '*.log' -printf '.' | wc -cTry these on a Linux machine of your own. Each task has a hint and a solution.
How many lines are in ~/logs/access.log?
Count the ERROR lines in ~/logs/app.log, printing only the number.
Count the data rows (excluding the header) in ~/data/orders.csv and store the number in a variable.
Show the line count of every file in ~/logs plus a total.
wcCount lines, words and bytes
wc counts lines, words, characters and bytes in files or in whatever you pipe into it. wc -l at the end of a pipeline is the standard way to answer 'how many?': errors in a log, files in a directory, connections, users, rows in a CSV.
wc [-l] [-w] [-c] [-m] [-L] [FILE]...You want to know how big a log is in lines, and compare files at a glance.
grepSelect the lines you want to count.
wc reads its input in large blocks and counts as it goes; nothing is kept in memory, so it handles files of any size. -l counts newline bytes (coreutils uses a vectorised fast path for this, so wc -l runs at disk speed); a final line without a trailing newline is therefore not counted. -c counts bytes, and for a regular file with -c alone wc can simply take the size from fstat without reading. -w counts words, sequences of non-whitespace characters. -m counts characters, which depends on the locale: in UTF-8 'é' is one character but two bytes. With several files wc prints a line per file and a total, padding the numbers into columns; when it reads standard input it prints no file name, which is why wc -l < file is the idiom for getting a bare number in scripts. Output order is always lines, words, characters, bytes, whatever order you give the options.
-lCount lines (newline characters)-wCount words (whitespace-separated)-cCount bytes-mCount characters (multibyte-aware, locale-dependent)-LLength of the longest line< fileRead stdin: print the number only, no file name--files0-from=FRead NUL-separated file names (from find -print0)ss -tn | wc -l or ps aux | wc -l as the countBoth commands print a header line, so the count is off by one.
ss -tnH | wc -l, ps aux --no-headers | wc -l, or tail -n +2 firstecho "$var" | wc -c to measure a stringecho appends a newline, so the result is one too high (and wc -c counts bytes, not characters).
printf '%s' "$var" | wc -m, or ${#var} in bashCOUNT=$(wc -l file) and then using $COUNT in arithmeticWith a file argument wc prints '120 file', so $COUNT contains the file name too.
COUNT=$(wc -l < file)find . -name '*.log' | wc -l with file names that contain newlinesEach newline in a name is counted as an extra file.
find . -name '*.log' -printf '.' | wc -cTry these on a Linux machine of your own. Each task has a hint and a solution.
How many lines are in ~/logs/access.log?
Count the ERROR lines in ~/logs/app.log, printing only the number.
Count the data rows (excluding the header) in ~/data/orders.csv and store the number in a variable.
Show the line count of every file in ~/logs plus a total.