Adding text to files or redirects here, we will learn how can we add a text or digital 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 ways to add text files.
The first one is through the VI.
Ed VII is the editor.
It's a command that is used in the next to create a file and add text messages to it.
Then the next one is redirect command output using the the write direct and the two redirects to a file
and in this lesson will 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 date you type, you could output to a file.
That's how the file gets created, or that's how the file get populated with information.
So let's look at another Linux machine and run these commands.
So here we have our Linux machine.
Let's make sure we are in our home directory as 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.
OK, 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 wanted 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 that 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 Seinfeld.
Is the main character in the show.
Sign fell, so this is the text that I wanted to type, I wanted to add into the file called Jerry and
how are we going to do it?
We couldn't do it by redirecting the redirect, which is a key right above your the dot you typed 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 X minus L and you hit enter, you're going to see Jerry has fifty eight bytes inside
of that file.
Now all the forces here because we didn't put anything in there.
Now that's good.
We added something to our first file.
This is amazing.
OK, 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.
Let's list all the files and 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 cat man to read the content of a file will cover the 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 happens if we do?
Go and do it, and we wanted to type another line in that same file, so let's say Jerry Schill was
created.
In 1989.
And you output.
Two of Jerry, I'll remember why we are using to output arrows here 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 show was created in 1989.
Now, we don't want to remove it.
We don't 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 and
enter.
How do you verify what's inside of it, whether it went inside the file or not?
Again, I want you to use up our key.
Twice, because that's the first approach he brings you, the last command, the second time you hit
up, Biraki brings you the command to get the Sherifa hit enter and there you go.
Now you have to fly two lines in your file and you do this mysel last time you have eight bytes.
Now we have 89 bytes because the file is keep growing.
Now, this is the simple way to add content to a file effet running the same command again.
There you go, Jerry Seinfeld is the main character in the show, if you take out that and you just
do.
Jerry Seinfeld is a main character, right?
And now we want to use one redirect and we wanted to redirect Jerry.
Now, remember this.
It will wipe out all the contents of the file.
Jerry basically will wipe out the entire file.
Jerry, recreate the file with the new text message text that I am typing here.
Tried, let's say hit.
Enter Dukat, Jerry.
Enter and there you go, Jerry Seinfeld is the main character, it does not have all the type things
that we did earlier.
It took the the latest one, the latest hyping, the latest echo we did in the fall, 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 right inside of these file 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.
OK, 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.
Listing off while no listing off directory, let's say we call this right, you hit, enter, verify,
the file is created.
Yes, listing of Dyax is just a name I picked.
You could pick any name.
Now, how do I populate that file?
With what?
What I wanted 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 X minus L could enter or else minus L tr 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 want it to output this entire output, entire result of my atlas myself here into the file I create
a listing of directory so we will do ls minus l tr output to listing hit tab.
It completes your file and now you hit enter.
You see what just happened, it is it did not give you the output of myself here on your screen, what
it did, instead of giving you on the screen, it is outputting to a file you call the listing off directory.
Now, Ellesmere's Elter shows you that the total number of bytes this file has is eleven.
Forty.
OK, awesome.
Now, what's inside of this director of this file?
You could check by typing the command cat listing or tab and hit enter.
You see, guys, it's a 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 updated whatever you wanted to do.
If you want to do date and you want a pen that date, by the way, the output of date is today's date.
And if you wanted to appended to the file Jerry hit enter.
Now verify it, Jerry.
You see, it has the first line that we typed to echo command and then it has the second line that we
got it from the date.
So this these two funny characters, the output, 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 is another way is a VI that will cover later in the lessons.
But now, right now, you have to practice these two ways of adding or appending to a file.