File Creation and Text Editing (Demo)

Video Activity
Join over 3 million cybersecurity professionals advancing their career
Sign up with
Required fields are marked with an *
or

Already have an account? Sign In »

Time
21 hours 25 minutes
Difficulty
Intermediate
CEU/CPE
21
Video Transcription
00:00
>> Hey there, Cybrarians.
00:00
Welcome back to the Linux plus course here at Cybrary.
00:00
I'm your instructor Rob Goelz.
00:00
>> In today's lesson,
00:00
>> we're going to be discussing
00:00
file creation and text editing.
00:00
Upon completion of today's lesson,
00:00
you're going to be able to use the
00:00
touch command to create files
00:00
>> and we're going to work with two text editors,
00:00
>> vim and nano, to modify files.
00:00
The touch command is generally what we use if
00:00
we want to create a file and what
00:00
you need to know is that every file has
00:00
three timestamps: access,
00:00
modified, and changed.
00:00
The touch command can also be used to
00:00
modify the timestamps on a file.
00:00
You could use touch -a to modify the access timestamp,
00:00
you could use the -m option to
00:00
>> change the modified time,
00:00
>> or you can actually use -d and date to get a
00:00
specific timestamp set on the file.
00:00
Now you can use ls with
00:00
the list command to view timestamps.
00:00
You can use ls -lu to see the access timestamp,
00:00
or just plain old ls -l to see the modified timestamp,
00:00
ls -lc to see the changed timestamp.
00:00
But it's generally just easier to simply use
00:00
the stat command if you want to
00:00
see all timestamps at once.
00:00
You can see over here the screenshot of Ubuntu,
00:00
we see all three of those timestamps: access,
00:00
modify, and change timestamps
00:00
all at once by just running a stat on the file.
00:00
Once we created a file with touch,
00:00
what do we do? What's next?
00:00
Well, how do we put content into it?
00:00
The most common way to do that is to use a text editor.
00:00
Let's go ahead and jump into how we do
00:00
that and cover two of these texts editors,
00:00
nano and vim, with some demo time.
00:00
>> Here we are in our CentOS environment and
00:00
>> let's go ahead and just create a file.
00:00
We'll call it test.
00:00
We can do a touch on test.
00:00
Now in this directory,
00:00
>> we can look at the modified time.
00:00
>> We can do lc to look at the change time,
00:00
we could do lu to look at the updated time,
00:00
or we can just save ourselves
00:00
time period and just do stat on test.
00:00
We can see all three of these timestamps.
00:00
This is great, but it's boring.
00:00
>> Look, all these timestamps have
00:00
>> the exact same time because I just created the file.
00:00
How about we play around with messing with them
00:00
in text editors and have a little bit of fun?
00:00
Let's clear the screen. I'm going to type clear.
00:00
You can also hit Control L to do this
00:00
>> and then we're going to open up nano.
00:00
>> The way that you open up nano is to just type nano
00:00
and then the file that you want to work
00:00
and it will say test.
00:00
Now the cool thing about nano is you
00:00
can just start typing immediately and it sees,
00:00
>> I am typing in nano and it is awesome.
00:00
>> Then if you wanted to save what
00:00
you did or save what you wrote in this file,
00:00
you can just hit Control O, as in Oscar.
00:00
It's going to prompt you for
00:00
where you want the file to be written.
00:00
We're just going to accept the default.
00:00
We're going to save it in the test file we created.
00:00
Then once we've done that, to get out of the file,
00:00
we can hit Control X
00:00
and it brings us back to our command prompt.
00:00
But what if we don't save first?
00:00
What if we forget and we just want to
00:00
X out with Control X?
00:00
Let's see what that looks like.
00:00
Let's reopen the file with
00:00
nano test and we'll
00:00
just go to the end of the line and type something else.
00:00
I am typing more stuff.
00:00
Then if we hit Control X,
00:00
it's going to actually prompt us
00:00
>> to save modified buffer.
00:00
>> What this is basically saying is hey,
00:00
I see changes in this file.
00:00
You've made a modification.
00:00
Do you want to save it before you exit?
00:00
We can say yes and tell it
00:00
>> we want to write back the test
00:00
>> again and hit Enter and
00:00
we get dropped back to the command prompt.
00:00
Really with nano, you can't go wrong.
00:00
It's helpful to save what
00:00
you're working on every so often,
00:00
save often and early.
00:00
Hitting Control O will allow you to save in nano,
00:00
but beyond that, if you hit Control X,
00:00
it's going to prompt you to save any changes anyway.
00:00
Just be assured that as
00:00
long as you do one of those two options,
00:00
you'll be able to save your changes,
00:00
unless your machine shuts down in which case
00:00
you probably would have wanted to do a Control O Center.
00:00
Let's move on to vim next.
00:00
vim is something of a deserved reputation for
00:00
being difficult and having a learning curve.
00:00
But if you can just understand
00:00
a couple of commands in vim,
00:00
you'll be all set.
00:00
Let's go ahead. We're just going to hit vim.
00:00
Then upon opening this file,
00:00
we can't immediately start typing.
00:00
If I just start typing stuff,
00:00
generally nothing happens unless you hit a certain key.
00:00
Let's say if I hit I,
00:00
I'm in insert mode and I can
00:00
replace the I that I just took out.
00:00
Then if I hit Escape on my keyboard, I'm back here.
00:00
If I just start typing other stuff,
00:00
>> you hear the keys going here,
00:00
>> I'm just hitting H and D and nothing's
00:00
being typed on the screen.
00:00
That's because there are certain specific characters
00:00
that vim recognizes.
00:00
I is what will set put you into insert mode.
00:00
If I go to the end of the line here and I hit I,
00:00
now I could start typing something else.
00:00
There's t in something.
00:00
Here we go. Now,
00:00
if we wanted to get out
00:00
of this mode and see what we're doing,
00:00
we can hit Escape.
00:00
This puts us into what's called command mode in vim.
00:00
To save our changes and get out of this file,
00:00
we have to hit colon and then WQ for write and quit.
00:00
Now, colon indicates that we're running a command.
00:00
That's what happens when you go into command mode.
00:00
You hit a colon to run the command you want to run.
00:00
You type W to write the file out and
00:00
then Q to quit and
00:00
then we'll be dropped back to our console,
00:00
just like we were with nano.
00:00
Just remember, I to insert, begin typing,
00:00
to save, hit Escape,
00:00
then hit colon WQ.
00:00
That's how you can get out of vim.
00:00
Escape from vim, no one's favorite game.
00:00
Those are two very basic steps of working with vim,
00:00
but vim is very powerful and
00:00
I would be remiss not to show you more.
00:00
Also keep in mind vim and VR are
00:00
installed on most Linux systems by default.
00:00
Knowing them will serve you
00:00
better than knowing nano alone in
00:00
case it's not on the particular system
00:00
that you have to support.
00:00
Let's go ahead and open our file again.
00:00
We're going to go back here and we're
00:00
going to reopen vim test.
00:00
That just tells us we want to open this file in vim.
00:00
Let's play around a little bit more.
00:00
There's more than one way to enter insert mode in vim.
00:00
We can hit the letter O
00:00
>> and what it'll do is it'll enter
00:00
>> a new line below of
00:00
the screen here and we can say new line.
00:00
>> Then if we hit Escape,
00:00
>> we can also hit O,
00:00
so Shift O and it'll start a new line above,
00:00
>> New Line 2.
00:00
>> If we want to undo both
00:00
of these lines that we just typed,
00:00
we can hit the Escape key and then hit U
00:00
>> and then U again.
00:00
>> U is the key for undo.
00:00
Now, if we wanted to append something,
00:00
we can hit A and it will allow us to append
00:00
immediately after where the cursor is something else,
00:00
something else, something else for everyone.
00:00
If we hit A,
00:00
no matter where we are,
00:00
so Shift A,
00:00
it brings us back to the end of the line and
00:00
we can append the end the line here as well.
00:00
>> We can also copy and paste the lines.
00:00
>> Let's hit Escape as well
00:00
>> and go back to the command mode.
00:00
>> If we want to copy a line,
00:00
we can hit the letter Y twice.
00:00
Y stays for yank. Don't ask me why.
00:00
But y for yank and then we could hit
00:00
the letter P and that will paste the line below.
00:00
If we wanted to paste the line above,
00:00
we can go back here up to the first line, for instance,
00:00
and hit Y twice and then hit Shift P.
00:00
>> It'll paste the line above.
00:00
>> We can just do this on and on.
00:00
Finally, if we want to delete all the lines we created,
00:00
we can just hit the letter D twice.
00:00
D, D,
00:00
>> D, D,
00:00
>> and there we go.
00:00
>> We're back to this, the two lines we
00:00
had before we got back into this file.
00:00
Then just to save and close or get out of here, again,
00:00
we hit Escape, colon, and WQ.
00:00
That's what you basically needed to know about vim.
00:00
We've reached the end of this lesson.
00:00
In this lesson, we covered creating files
00:00
and modifying file timestamps,
00:00
which we can do with the touch command.
00:00
We also talked about using text editors,
00:00
such as nano and vim.
00:00
Thanks so much for being here,
00:00
>> and I look forward to seeing you in our next lesson.
Up Next