{"id":11484,"date":"2019-03-14T08:53:18","date_gmt":"2019-03-14T08:53:18","guid":{"rendered":"http:\/\/www.appservgrid.com\/paw92\/?p=11484"},"modified":"2019-03-14T08:53:18","modified_gmt":"2019-03-14T08:53:18","slug":"6-wc-command-examples-to-count-number-of-lines-words-characters-in-linux","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/03\/14\/6-wc-command-examples-to-count-number-of-lines-words-characters-in-linux\/","title":{"rendered":"6 WC Command Examples to Count Number of Lines, Words, Characters in Linux"},"content":{"rendered":"<p>The\u00a0<strong>wc<\/strong>\u00a0(<strong>word count<\/strong>) command in Unix\/Linux operating systems is used to find out number of\u00a0<strong>newline count<\/strong>,\u00a0<strong>word count<\/strong>,\u00a0<strong>byte and characters<\/strong>\u00a0count in a files specified by the file arguments. The syntax of\u00a0<strong>wc<\/strong>\u00a0command as shown below.<\/p>\n<pre># wc [options] filenames<\/pre>\n<p>The following are the options and usage provided by the command.<\/p>\n<pre><strong>wc -l<\/strong> : Prints the number of lines in a file.\r\n<strong>wc -w<\/strong> : prints the number of words in a file.\r\n<strong>wc -c<\/strong> : Displays the count of bytes in a file.\r\n<strong>wc -m<\/strong> : prints the count of characters from a file.\r\n<strong>wc -L<\/strong> : prints only the length of the longest line in a file.<\/pre>\n<p>So, let\u2019s see how we can use the \u2018<strong>wc<\/strong>\u2018 command with their few available arguments and examples in this article. We have used the \u2018<strong>tecmint.txt<\/strong>\u2018 file for testing the commands. Let\u2019s find out the output of the file using\u00a0<a href=\"https:\/\/www.tecmint.com\/13-basic-cat-command-examples-in-linux\/\" target=\"_blank\" rel=\"noopener\">cat command<\/a>\u00a0as shown below.<\/p>\n<pre>[root@tecmint ~]# cat tecmint.txt\r\n\r\nRed Hat\r\nCentOS\r\nFedora\r\nDebian\r\nScientific Linux\r\nOpenSuse\r\nUbuntu\r\nXubuntu\r\nLinux Mint\r\nPearl Linux\r\nSlackware\r\nMandriva<\/pre>\n<h3>1. A Basic Example of WC Command<\/h3>\n<p>The \u2018<strong>wc<\/strong>\u2018 command without passing any parameter will display a basic result of \u201d<strong>tecmint.txt<\/strong>\u2018 file. The three numbers shown below are\u00a0<strong>12<\/strong>\u00a0(<strong>number of lines<\/strong>),\u00a0<strong>16<\/strong>\u00a0(<strong>number of words<\/strong>) and\u00a0<strong>112<\/strong>\u00a0(<strong>number of bytes<\/strong>) of the file.<\/p>\n<pre>[root@tecmint ~]# wc tecmint.txt\r\n\r\n<strong>12<\/strong>  <strong>16<\/strong> <strong>112<\/strong> tecmint.txt<\/pre>\n<h3>2. Count Number of Lines<\/h3>\n<p>To count number of newlines in a file use the option \u2018<strong>-l<\/strong>\u2018, which prints the number of lines from a given file. Say, the following command will display the count of newlines in a file. In the output the first filed assigned as count and second field is the name of file.<\/p>\n<pre>[root@tecmint ~]# wc -l tecmint.txt\r\n\r\n<strong>12<\/strong> tecmint.txt<\/pre>\n<h3>3. Display Number of Words<\/h3>\n<p>Using \u2018<strong>-w<\/strong>\u2018 argument with \u2018<strong>wc<\/strong>\u2018 command prints the number of words in a file. Type the following command to count the words in a file.<\/p>\n<pre>[root@tecmint ~]# wc -w tecmint.txt\r\n\r\n<strong>16<\/strong> tecmint.txt<\/pre>\n<h3>4. Count Number of Bytes and Characters<\/h3>\n<p>When using options \u2018<strong>-c<\/strong>\u2018 and \u2018<strong>-m<\/strong>\u2018 with \u2018<strong>wc<\/strong>\u2018 command will print the total\u00a0<strong>number of bytes<\/strong>\u00a0and\u00a0<strong>characters<\/strong>respectively in a file.<\/p>\n<pre>[root@tecmint ~]# wc -c tecmint.txt\r\n\r\n<strong>112<\/strong> tecmint.txt<\/pre>\n<pre>[root@tecmint ~]# wc -m tecmint.txt\r\n\r\n<strong>112<\/strong> tecmint.txt<\/pre>\n<h3>5. Display Length of Longest Line<\/h3>\n<p>The \u2018<strong>wc<\/strong>\u2018 command allow an argument \u2018<strong>-L<\/strong>\u2018, it can be used to print out the length of longest (<strong>number of characters<\/strong>) line in a file. So, we have the longest character line (\u2018<strong>Scientific Linux<\/strong>\u2018) in a file.<\/p>\n<pre>[root@tecmint ~]# wc -L tecmint.txt\r\n\r\n16 tecmint.txt<\/pre>\n<h3>6. Check More WC Options<\/h3>\n<p>For more information and help on the\u00a0<strong>wc<\/strong>\u00a0command, simple run the \u2018<strong>wc \u2013help<\/strong>\u2018 or \u2018<strong>man wc<\/strong>\u2018 from the command line.<\/p>\n<pre>[root@tecmint ~]# wc --help\r\n\r\nUsage: wc [OPTION]... [FILE]...\r\n  or:  wc [OPTION]... --files0-from=F\r\nPrint newline, word, and byte counts for each FILE, and a total line if\r\nmore than one FILE is specified.  With no FILE, or when FILE is -,\r\nread standard input.\r\n  -c, --bytes            print the byte counts\r\n  -m, --chars            print the character counts\r\n  -l, --lines            print the newline counts\r\n  -L, --max-line-length  print the length of the longest line\r\n  -w, --words            print the word counts\r\n      --help\t\t\tdisplay this help and exit\r\n      --version\t\t\toutput version information and exit\r\n\r\nReport wc bugs to bug-coreutils@gnu.org\r\nGNU coreutils home page: &lt;http:\/\/www.gnu.org\/software\/coreutils\/&gt;\r\nGeneral help using GNU software: &lt;http:\/\/www.gnu.org\/gethelp\/&gt;\r\nFor complete documentation, run: info coreutils 'wc invocation'<\/pre>\n<p><a href=\"https:\/\/www.tecmint.com\/wc-command-examples\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The\u00a0wc\u00a0(word count) command in Unix\/Linux operating systems is used to find out number of\u00a0newline count,\u00a0word count,\u00a0byte and characters\u00a0count in a files specified by the file arguments. The syntax of\u00a0wc\u00a0command as shown below. # wc [options] filenames The following are the options and usage provided by the command. wc -l : Prints the number of lines &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/03\/14\/6-wc-command-examples-to-count-number-of-lines-words-characters-in-linux\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;6 WC Command Examples to Count Number of Lines, Words, Characters in Linux&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-11484","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/11484","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/comments?post=11484"}],"version-history":[{"count":1,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/11484\/revisions"}],"predecessor-version":[{"id":11485,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/11484\/revisions\/11485"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=11484"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=11484"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=11484"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}