Linux Commands – wc
Hello Everyone
Welcome to CloudAffaire and this is Debjeet.
In the last blog post, we have discussed watch command in Linux which is used to run a command repeatedly, displaying its output and errors (the first screenful).
https://cloudaffaire.com/linux-commands-watch/
In this blog post, we will discuss wc command in Linux. wc command counts the number of bytes, characters, whitespace-separated words, and newlines in each given FILE or standard input and outputs the result in standard output. wc command prints one line of counts for each file, and if the file was given as an argument, it prints the file name following the counts. The counts are printed in this order: newlines, words, characters, bytes, maximum line length. By default, wc prints three counts: the newline, words, and byte counts.
Linux Commands – wc:
You can use wc command to print total numbers of newline, words, and byte in a file. If more than one FILE is given, wc prints a final line containing the cumulative counts, with the file name total.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
######################### ## Linux Commands | wc ## ######################### ## Prerequisites: One Unix/Linux/POSIX-compliant operating system with bash shell ##--- ## wc ##--- ## wc [option]... [file]... ## create a file with 100 lines > myfile; i=1; while [ $i -le 100 ]; \ do echo "$i" >> myfile; ((i++)); done ## create two copies of the file cp myfile myfile1 cp myfile myfile2 ## newline words byte wc myfile ## returns 100 100 292 myfile wc myfile myfile1 myfile2 ## returns 300 300 876 total at the end ps -ax | wc ## also works with pipe |
You can use wc -c or –bytes options to print only the byte counts.
1 2 3 4 5 6 7 |
## wc -c or --bytes options ## newline words byte filename wc myfile ## returns 100 100 292 myfile ## byte filename wc -c myfile ## returns 292 myfile |
You can use wc -m or –chars options to print only the character counts.
1 2 3 4 5 6 7 |
## wc -m or --chars options ## newline words byte filename wc myfile ## returns 100 100 292 myfile ## character filename wc -m myfile ## returns 292 myfile |
You can use wc -w or –words options to print only the word counts.
1 2 3 4 5 6 7 |
## wc -w or --words options ## newline words byte filename wc myfile ## returns 100 100 292 myfile ## words filename wc -w myfile ## returns 100 myfile |
You can use wc -l or –lines options to print only the newline counts.
1 2 3 4 5 6 7 |
## wc -l or --lines options ## newline words byte filename wc myfile ## returns 100 100 292 myfile ## newline filename wc -l myfile ## 100 myfile |
You can use wc -L or –max-line-length to print the length of the longest line.
1 2 3 4 5 6 7 8 9 10 11 12 |
## wc -L or --max-line-length options ## newline words byte filename wc myfile ## returns 100 100 292 myfile ## max-line-length filename wc -L myfile ## returns 3 myfile echo "hello world" >> myfile ## append a new line of length 11 ## max-line-length filename wc -L myfile ## returns 11 myfile |
You can use wc –files0-from=FILE option which disallow processing files named on the command line, and instead process those named in file FILE; each name being terminated by a zero byte (ASCII NUL). This is useful when the list of file names is so long that it may exceed a command line length limitation. In such cases, running wc via xargs is undesirable because it splits the list into pieces and makes wc print a total for each sublist rather than for the entire list. One way to produce a list of ASCII NUL terminated file names is with GNU find, using its -print0 predicate. If FILE is ‘-‘ then the ASCII NUL terminated file names are read from standard input.
1 2 3 4 5 6 |
## wc --files0-from=FILE option ## print wc for all files under /etc with .conf extension sudo find /etc -name '*.conf' -print0 | wc --files0-from=- rm my* |
Hope you have enjoyed this article. In the next blog post, we will discuss the date command in Linux.