Now it's time to look at the last command, which
is wc. It is one of the last command for text
processors that in our lectures, that we are going
to cover. A wc stands for word count, and what
exactly it does. The command reads either standard
input or a list of files and generates newline
count, word count, and byte count. So if you just
run wc on a file, it will give you these three
output. Now let's get into our Linux machine. And
I have the first command, as you all know, is to
check the version and to get the help. So when I
get to my Linux machine, and again, I'm logged
into the PuTTY, and you guys know what we have to
run, we have to run, as usual the username, and the hostname,
and which directory we are in. And pretty much
once we confirm, perfect, clear the screen, and now do
wc, and I'm going to skip version. I know all of
you guys know this already by now, these are the
few options that it is it supports. And then the
one of the most used one that I use as
well, and a lot of people use is -l, which
we'll cover in a few. So now let's get into the
next command, which is wc file name. Let's clear
the screen. I'm going to go to my seinfeld
directory, and in there I have my seinfeld-characters. Let's
cat out, and these are the
contents. This is the content of my seinfeld-characters file.
Perfect. Now if I do, let's
clear the screen again, if I do wc seinfeld-characters,
hit enter, you're going to see it will
tell me exactly what's inside, meaning, it has 17
lines, has 34 words, and 236 bytes. So if I wanted
to know only the line numbers that's inside of
seinfeld-character, I could simply do wc, word
count, or sometimes people refer to as waca dash
l, and seinfeld-characters, you're going to see it
will give me only the number of lines, that is 17.
Similarly, I could do either w or b to get the
word count, and the byte count. So for the word, as
you see, it'll give me 34. For the byte count, I'll
replace it with b,
[No audio]
is it a byte count, why it's
not giving me the information, invalid option minus
b. For the byte count, hmm, that's new. Okay, let
me check why is that, wc --help, and we will
see, ah for the byte it's actually c. See, that's
why it's so important for you to run wc dash dash
help to find out the right option. Or, what's the
other one, the big one man wc, which will also
give you a lot of information about that specific
command. Anyway, my slide is showing -b, that
is okay, you could ignore it. I'll go back and fix
it later. But for now, for this lecture, we'll go
continue with wc -c for byte size. Also, a
lot of people confuse this with the -c,
meaning characters, it is not a character, it's exactly a
byte size. So the file name would be seinfeld.
Usually we saw a bytes and characters are
somewhat similar, maybe a few, not exactly a few,
two or three numbers up and down. But anyway,
focus on wc -c, if you wanted to get the
byte size. Alright, the next command I have the wc
DIRECTORY, which is NOT allowed. So if you trying
to do the wc on a directory, so let's say inside
of my seinfeld directory, there is no directory.
So I'll go one step back, and do ls -l to see
which directories are there. And now I will do wc
on seinfeld directory, hit Enter. It will say, wc:
seinfeld/: Is a directory. So it's not going to
give me any information that I'm looking for
regarding lines, or word count, or bytes. Anyway,
clear the screen. Let's go to the next one, and
which is the ls -l command, which we will run,
and that will give us the list of output. Again,
this is the command one. Before we were talking
about reading from the file, now this is reading
from the output of a command. So if you want to
see how many files that you have, let's say your
boss comes to you and says hey, listen, I need the
list of files we have in this user home directory.
Give me the number. You go in and you quickly run
ls -l, and wc -l, and you come back to him
and or her and tell your boss, hey, we got 15
files alright.
wc is working perfectly here, and it is giving you
the count of every line. But in fact you do not
have for 15 files or directories you have 14. Why
because, you see this total 280 here, this is
something that it is also counting. So, always
remember when you do ls -l, and wc -l,
whatever the results that you get, always minus it
with one. So, now you could say that I have 14
files and directories altogether. What if I want
to know only the list of directories in my,
in my home directory. So, to do that, you know, to list
the directories, you have to run the command, I
think ls -ld, or ls -ld /homes
/iafzal.
[No audio]
Okay, it is giving me,
[No audio]
all, okay, so this is not the right command. Anyway, I will
look it up for what is the right command, however,
I could still use another trick. See, that's why
Linux is so powerful. And you do ls -l, you
you notice that every directory starts with D. So
what I'm going to do is I'm going to do ls -l,
and grep for drw, because all of them, if you
notice starts with drw, drw. So just give me
all the directory. So by default,now, I have all
the directories. Anyway. So now if I want to know
how many directories I have, hit up arrow key, and
do wc -l. So it is telling me that I have 11
directories. Now, it is not giving me that total,
right here, you notice, because the total will
only give you when you only run ls -l. As
soon as you run ls -l, and pipe it that total
will go away, and then you will just grep for drw,
because you're only grepping for anything that
starts with drw. And the total does not start
with drw. Anyway, as an exercise for
you, go ahead and find out the command of ls -ld,
or whatever the command is and find out which
command will give you only the directories. I use
the grep, it works for me perfect. But anyway,
there is, I'm definitely sure there is a command
and I'm sorry, I didn't know that.
Anyway, that's for you to find out. So that's how
you know the number of files in your directories
in your home directory. And then the last command
that we will run is to grep for keyword in a
file and then get a list of it. So I'll go back to
my seinfeld directory. In the seinfeld directory, I
have a file seinfeld, as everybody know, so I'll
do cat seinfeld. And here I will grep for
seinfeld. So grep Seinfeld, in my seinfeld-characters
file, and you'll notice there are three
people with this last name Seinfeld. Now if I want,
if I had let's say 20 or 30 or 1000 of those names
that match my criteria, then how would I know how
many were there? So simply do pipe it wc and -l,
it will tell you there are three instances of
your search criteria, works, perfect. I hope you
understand now all the text processing commands, I
have explained it in very detail. Please please do
try them with different options and different
scenarios and see which ones works for you and
which ones gives you more fun to work with.
[No audio]