Wc

From Unix SME

Command Name:

     wc

Description:

    The wc command is quite simple, but is able to do a good number of things.  Without extra options, it will count the number of lines, words, and characters in a given file, then subsequently print them to the terminal.  However it also has options that will allow it to count the number of bytes the file takes up, the maximum length of the longest line in the file, or the number of files in a directory.  It can also print all of these options individually using it's options.  This command can also take multiple files, and list their data both separately and combined.

Synopsis:

    wc [options] [file_name]
    wc [file_name]                
  • Runs with default wc options

Options:

    -c, --bytes              
  • Only displays total byte count contained within file
    -m, --chars              
  • Only displays total character count contained within file
    -l, --lines              
  • Only displays total newline count contained within file
    -L, --max-line-length    
  • Display the length of the longest line in the file
    -w, --words              
  • Display total word count contained in the file
    --files0_from={filename} 
  • Read null-terminated filenames from a file filename
    --help                   
  • Display the command help text and return to prompt
    --version                
  • Display the current command version and return to prompt


Use Case:

    This command can be used for quickly counting all of the files in a directory instead of manually listing them and counting them yourself. It can also be used to count and find the number of times a phrase or word appears in a file or number of files.  It can also compare the data between multiple files.


Examples: 1. [user@host ~]$ wc filename

  • Lists the number of lines, words, and characters in a file

2. [user@host ~]$ grep -o word filename | wc -l

  • Lists the number of times 'word' appears in a file

3. [user@host ~]$ wc -w firstfile secondfile

  • Compares the word count of two files, and displays them combined as well

4. [user@host ~]$ ls -l | wc -l

  • Lists the number of files in a directory

5. [user@host ~]$ wc -m filename

  • Lists the number of characters in a file

6. [user@host ~]$ wc -c filename

  • Lists the number of bytes a file takes up

7. [user@host ~]$ wc -wl filename

  • Lists both the number of words and lines in a file

8. [user@host ~]$ wc -mw firstfile secondfile

  • Compares both the number of words and characters of two files

9. [user@host ~]$ wc -l filename

  • Lists the maximum line length of a file

10. [user@host ~]$ wc -c firstfile secondfile

  • Compares the number of bytes of two files

11. [user@host ~]$ wc -cw filename

  • Displays only byte and word counts

Additional Notes and Concepts:

    When used in bash scripts, this command can be exceptionally powerful. For example, this command could be used
    to count the length of the smallest line in a file, and can also be used to calculate file sizes (perhaps even
    binary files, and certainly source code.) When used with the bash scripting language, this command could also be
    used to compare multiple directories together instead of comparing files together.