[No audio]
Text to files or redirects.
Here we will learn how can we add text or digits
or letters, or how can you populate an empty file.
We created some of the empty files using touch command and
touch is just a way to create an empty file.
But now how can we add text to those files
or anything that you like to add to it?
There are three simple ways to add text to a file
and the basic main way is to add text to files.
The first one is through the vi editor.
vi is the editor.
It's a command that is used in Linux to
create a file and add text messages to it.
Then the next one is redirect command output using the
right direct and the two right directs to a file.
And in this lesson, we'll cover these redirects
through a command output into a file.
And the second way we will learn through echo,
when you want to echo whatever that you
type, you could output to a file.
That's how the file gets created or that's
how the file gets populated with information.
So let's look on our
Linux machine and run these commands.
[No audio]
So here we have our Linux machine.
Let's make sure we are in our home directory.
Yes, we are in our home directory.
Let's clear the screen and let's check what are
the different files we have in our home directory.
Okay, we have all those different files
and directories that we created earlier.
Now we wanted to add something
to a file, any empty file.
Because if you see here, these are the few
files that we added and all of them have
zero bytes in it because these are empty files.
So when you want to do echo and you type
hi my name is Imran, and you hit enter, it
brings you back the exact same thing that you type.
So the command echo actually echoes
back what you shouted out.
So how about if we output this to a file?
So I will say let's say, echo "Jerry seinfled
is the main character in the show Seinfled".
So this is the text that I wanted to type,
I wanted to add into the file called jerry.
And how we're going to do it? We're going do it by redirecting, the redirect,
which is a key right above the dot,
you type that with the shift and then you
specify the name of the file which is jerry.
Hit enter, you'll get your prompt back.
Now when you do ls-l and you hit enter, you're
going to see jerry has 58 bytes inside of that file.
Now all other files are 0 because
we didn't put anything in there.
Now that's good.
We added something to our first file.
This is amazing.
Okay, now how do we know what's inside of this file?
How do we check it? How do we verify it?
We do know that the cd gets into our directory,
lists all the files, and pwd tells us where we are.
But how are we going to see
what's inside of directory in Windows? How do you do it?
How do you open up a file?
Well, you do a double click and it opens up. In Linux,
there is no double click. Then how do you do it?
There is a command called cat. cat is a
command to read the content of a file.
We'll cover the cat command a little
more in detail in the next lessons.
But for now that we want to know what's inside
of the file, we will use the command cat, followed
by the name of the file which is jerry.
When you hit Enter and there you see, you have
Jerry seinfeld is the main character in the show Seinfeld.
Now this is what we typed right here in our
echo and we outputted that to a file called jerry.
Now what happened if we do, echo and
[No audio]
echo,
and do,
and we wanted to type another line in that same file.
So let's say Jerry show was created
[No audio]
in 1989,
and you output,
[No audio]
to a file jerry.
Remember why we are using two output arrows here,
it is because if you use one arrow like this,
it will overwrite your last file and remove this
line that we added into this file.
You'll remove this line and create a new file
with the content of this Jerry show was created in 1989.
Now, we don't want to remove it.
We want to keep this line, the first line,
and we want to add a second line.
If you want to add a second line,
then you need to use the double redirect
signs. When you do that, hit Enter.
How do you verify what's inside or whether
it went inside the file or not?
Again, I want you to use up arrow key twice.
Because the first up arrow key
brings you the last command.
The second time you hit up arrow key brings
you the command to cat the jerry file.
Hit Enter and there you go.
Now you have two lines in your file.
When you do ls -l, last time we had 58 bytes.
Now we have 89 bytes because the file keeps growing.
Now this is the simple way to add contents to
a file, if we running the same command again.
[No audio]
There you go. Jerry seinfeld is the main character in the show.
If you take out that and we just
do Jerry seinfeld is the main character, right?
And now we want to use one
redirect and we want to redirect jerry.
Now remember this, it will wipe out
all the contents of the file jerry.
Basically it will wipe out the entire file
jerry, recreate the file with the new text
message text that I am typing here. Try it.
Let's say hit Enter.
Do cat jerry, enter, and there you go.
Jerry seinfeld is the main character.
It does not have all the typing that we did
earlier, it took the latest one, the latest typing, the
latest echo we did in the file, and it added
into the jerry file. The same way
I want you to add a few things
about anything that you know about these characters.
By the way, these characters are actually taken from
those hit shows, american hit shows of Seinfeld, Simpsons,
and of course, the main movie Superman.
If you know a little bit about any
of these characters, please do write inside of
these files with using the echo command.
And this would be a very good exercise
for you, how you could populate each file
with different things that you know about.
Again, remember, if you do one single
output, that will overwrite the file.
If you do double, that will append to the file.
Okay, now, this is about echo.
What if you wanted to do, let's create a new file
called touch,
and this time we'll call it,
[No audio].
listingoffile.
No, listingofdir.
Let's say we call this, right.
You hit enter, verify, the file is created. Yes.
listingofdir.
It's just the name I picked.
You could pick any name.
Now, how do I populate that file
with whatever I want to type in?
Of course, you know, that way you could
do it is through echo and whatever you
want to type and output to a file.
But now I want to show you a second way that you
could populate through the output of a command, which is, if I
do ls -l, hit enter or ls -ltr, let's say,
which brings me the latest one at the bottom,
I wanted to have this entire
output. Starting from here to here,
I wanted to output this entire result
of my ls -ltr into the file
I created listingofdir.
So we will do ls -ltr, output to listing.
Hit tab, it auto completes your file,
and now you hit enter.
[No audio]
You see what just happened?
It did not give you the output
of ls -ltr on your screen.
What it did, instead of giving you the
the screen, it is outputting to a file,
you call listingofdir.
Now ls -ltr, shows you that the total
number of bytes this file has is 1140.
Okay, awesome.
Now, what's inside of this
file? You could check by typing the command
cat listing, tab, and hit enter.
You see guys, it's the same output that you
have as you would have it on this one.
The only difference that you see this
output gives you a different color because
it is actually running the command.
Whereas this output cat of this listing is just
the simple text messages, text inside of a file.
You could change it, you can update
it, whatever you want to do.
If you want to do date, and you want to append
that date, by the way, the output of date is today's date.
And if you want to append that
to the file jerry, hit enter.
Now verify jerry, you see, it has the first line that we
type through echo command, and then it has the second
line that we got it from the date.
So these two, funny characters, the two outputs, appends to
your existing file, and does not delete your file.
So, again, these are the two ways that we learn.
One through the echo and redirecting, and one through the
output of a command and redirecting to a file.
And, of course, there's another way that
we'll cover later in the lessons.
But now, right now, you have to practice these
two ways of adding or appending to a file.
[No audio]