We can use wc to count characters, words (anything surrounded by whitespace) and lines:
[user@host shell-training]$ cd docs
[user@host shell-training]$ ls
123.dat abc.txt cake.txt def.txt r2d2.bot xyz.txt
abcde.txt ab.txt cheesecake.txt food.txt some-maths.txt
[user@host docs]$ wc abc.txt def.txt xyz.txt
10 52 168 abc.txt
7 61 394 def.txt
11 99 589 xyz.txt
28 212 1151 total
wc cake.txt
- counts lines, words and bytes for the filewc -w cake.txt
- counts wordwc -c cake.txt
- counts characterswc -l cake.txt
- counts linesIf you are dealing with record oriented data, wc -l will display the number of records.
We can give wc the names of the files we want to inspect as arguments.
[user@host docs]$ wc *.txt
13 119 683 abcde.txt
10 52 168 abc.txt
8 83 454 ab.txt
26 206 1332 cake.txt
11 105 656 cheesecake.txt
7 61 394 def.txt
13 85 561 food.txt
6 12 56 some-maths.txt
11 99 589 xyz.txt
105 822 4893 total
*
is a wildcard that matches zero or more characters.
Bash automatically expands *.txt
into a list of filenames matching that pattern, for example:
[user@host docs]$ ls a*.txt
abcde.txt abc.txt ab.txt
?
is also a wildcard. It matches a single character.
[user@host docs]$ ls ???.txt
abc.txt def.txt xyz.txt
Square brackets will match any one of the characters listed inside them.
[user@host docs]$ ls [fx]*
food.txt xyz.txt
In these examples we have used the file extension .txt, it is important to note that these extensions are a convention not a rule with bash.
Next activity: Reading - Using Nano.