File and Directory Operations Part 2

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
and welcome back to the Linux
00:00
Plus course here at Cybrary.
00:00
I'm your instructor, Rob Goelz.
00:00
In today's lesson, we're going to move on with
00:00
our two parter on file and directory operations.
00:00
In today's lesson, we're going to cover
00:00
the importance of text and directory operations;
00:00
moving on to play around with the commands remove,
00:00
remove directory and diff.
00:00
Let's go ahead and get to it with some demo time.
00:00
Here we're back in our CentOS environment,
00:00
and we're where left off right from the previous lesson.
00:00
We're going to move forward,
00:00
and the first command we're going to look
00:00
at is the remove command,
00:00
which is rm; rm is short for remove.
00:00
The basic syntax of rm is rm file,
00:00
or we can also remove a directory
00:00
with this command and we'll see how to do both of those.
00:00
For instance, let's go back to our temp directory.
00:00
Let's cd into temp and do an ls.
00:00
In here we can see our original,
00:00
we can see the other directory, and
00:00
we can see some directory.
00:00
Let's go ahead and remove the original files;
00:00
do an rm on original,
00:00
and that removes the file.
00:00
No muss, no fuss.
00:00
But let's go ahead and try and remove a directory.
00:00
This is where it's going to get a little bit trickier.
00:00
If we do our rm on other dir,
00:00
it's going to say it can't remove that
00:00
because that is a directory;
00:00
it has content, it has other things in it.
00:00
If it's a directory, we have to recursively remove it.
00:00
We can do that by doing rm minus
00:00
r on the directory
00:00
and it will remove all the files in it.
00:00
For our example, let's actually do
00:00
an rs-r some directory.
00:00
Then it'll remove that and all of the stuff in it.
00:00
If you do an ls slash ALL,
00:00
we'll see that our directory
00:00
or subdirectory and our original file are gone.
00:00
Now the only thing that we've
00:00
seen here is the other directory.
00:00
If you're not comfortable removing files this way,
00:00
what you can do is you can specify the I option,
00:00
which is for interactive.
00:00
For instance, let's go ahead and just
00:00
copy back -ar of home,
00:00
rob, somedir,
00:00
we'll copy that back to temp.
00:00
Now lets do an ls, we see somedir again.
00:00
If we want to do this interactively,
00:00
we can do rm minus ir on temp,
00:00
somedir, and this will
00:00
prompt us before it does anything.
00:00
Sorry, that was actually a force command.
00:00
That's a force of habit too.
00:00
Let's not do that. Let's copy this
00:00
back again, and let's do an ls.
00:00
we'll see someder as well,
00:00
We're going do rs minus ir,
00:00
and this will remove the directory
00:00
interactively and prompt us if we want to remove it.
00:00
We say yes and hit "Enter".
00:00
Now we do an ls,
00:00
that's some directories on and all we're left
00:00
with is the other directory.
00:00
Now another and remove command that you should
00:00
know about is the rmdir command.
00:00
rmdir is removed directory and
00:00
it's used to remove an empty directory.
00:00
If we do an ls here,
00:00
we have our other directory,
00:00
and this other directory isn't empty.
00:00
If we try and run rmdir on other directory
00:00
is going to tell us can't remove it, it's not empty.
00:00
Why would we use this when we already have
00:00
our M remove directory is good when you want to
00:00
be very safe and very careful about removing you
00:00
directory and you want to make
00:00
sure that it's an empty directory.
00:00
I generally will run rmdir
00:00
first on it just to make sure that I'm
00:00
removing empty directory and it doesn't have any content
00:00
in it that I accidentally might remove.
00:00
What we could do here, for example,
00:00
is we can descend into this other directory.
00:00
You just see the other directory,
00:00
then this inside of here we see dir 1,
00:00
and we can recursively remove dir 1
00:00
by doing rm minus r under one.
00:00
If we're really gun shy about it,
00:00
we can do rm minus ir dir 1,
00:00
and it'll say descent into directory,
00:00
yes; descending directory dir 2,
00:00
yes; descended directory dir 3, yes. Remove this.
00:00
Remove this, yes. There we are.
00:00
We're back, now we have nothing and other directory.
00:00
Then from there we can see the cd up,
00:00
which is cd dot dot slash,
00:00
that just takes you up one directory being doing an ls,
00:00
and we see other directory.
00:00
Now we know that this is directory is empty so we can go
00:00
rm other directory and
00:00
removes the directory and we're good to go.
00:00
The last thing we're going to look at today is
00:00
something called the diff command.
00:00
Diff is short for difference;
00:00
it just shows you the difference between two files.
00:00
Let's go ahead and go back to
00:00
my home directory and do an ls,
00:00
and we're going to diff file 4 and file 4.
00:00
So the diff on file 3 and file 4.
00:00
Diff just takes in the names of
00:00
the two files that you want to get a difference between.
00:00
It shows you, on the left-hand side,
00:00
the difference between the first file that you give it,
00:00
and on the right-hand side,
00:00
the difference between the second file that you give it.
00:00
The arrows that point left show you
00:00
that these are the differences in the first file,
00:00
file 3, the arrow
00:00
is pointing right to show you that these
00:00
are the differences that came from the
00:00
second file, file 4.
00:00
You can also use the _y option to compare
00:00
these files line by line and
00:00
display the difference between the files in columns.
00:00
We can see that line 1,
00:00
of file 3 is different from line 1 and file 4.
00:00
The only difference is just
00:00
the numbers file 3 versus file 4.
00:00
This is line 2 in file 3,
00:00
and this is line 2 in file 4.
00:00
With that, we've reached the end of this section.
00:00
In this section, we covered performing operations
00:00
on files and directories using ls make dir,
00:00
move, copy,
00:00
and in this lesson we covered remove,
00:00
remove directory and diff.
00:00
Thanks so much for being here and I look
00:00
forward to seeing you in the next lesson.
Up Next