File and Directory Operations Part 1

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 Cybrarians.
00:00
>> Welcome back to the Linux+ course here at Cybrary.
00:00
>> I'm your instructor Rob Goelz.
00:00
In today's lesson,
00:00
we're going to start with the first part of
00:00
our two-part series on file and directory operations.
00:00
Upon completion of today's lesson,
00:00
you're going to be able to understand the importance
00:00
of these operations that we do on text
00:00
and files and directories and we're going to operate
00:00
on files and directories using the command ls,
00:00
make directory, move and copy.
00:00
Let's go ahead and get to it with some demo time.
00:00
Here we are back in our CentOS environment
00:00
and we're going to cover the list command first day.
00:00
I know we've used the list command already,
00:00
but it is important, so we're going to
00:00
expand upon it a bit in this lesson.
00:00
The ls or list command is
00:00
just used to list the contents of a directory,
00:00
for instance, we're in my home directory.
00:00
Let's do an ls and we can see
00:00
all the files and directories
00:00
that live in this directory.
00:00
To get more information about any of these files,
00:00
we can add the - L option,
00:00
which gives us the long listing.
00:00
This gives us all of the information about all of
00:00
the files that are in here, displays the permissions.
00:00
We see the permissions right here.
00:00
We see the ownership.
00:00
The user and group owner is me.
00:00
We can also see the file size as
00:00
well as the timestamp and the file or directory name.
00:00
But notice when we're looking at these file sizes,
00:00
they're hard to read.
00:00
This whole column is just a bunch of numbers.
00:00
Well, what do they mean?
00:00
Well, we can fix this by adding the - H options.
00:00
We'll do an ls-l for long listing,
00:00
H in this directory.
00:00
Now we can see that the files
00:00
actually have appropriate sizes.
00:00
Instead of this being 4096,
00:00
we see that as 4k in size.
00:00
Now, you probably didn't notice,
00:00
but if you look here,
00:00
what don't we see?
00:00
What are some very important files
00:00
that we covered previously that we don't see in here?
00:00
Well, we don't see the bash files.
00:00
We don't see any of those.
00:00
How do we see the files like bash RC and bash history?
00:00
We fix this by adding the a option for all.
00:00
Now what this will display is any hidden files.
00:00
Hidden files in Linux
00:00
are files that have a period before them.
00:00
If you do ls-alhon my home directory where we are,
00:00
we can see that we now see bash history and bash profile,
00:00
bash RC, bash logout, all that good stuff.
00:00
We're going into this because you're going to
00:00
use the ls command constantly,
00:00
especially when you're looking at file permissions,
00:00
you'll use this command more than
00:00
the other command in Linux.
00:00
It's very helpful to know,
00:00
even just to know it for
00:00
professional use beyond the exam.
00:00
Let's go ahead and move on to the next command.
00:00
The next command we're going to look at is
00:00
the make directory command,
00:00
which is actually abbreviated as mk dir.
00:00
We've previously covered how to create files,
00:00
but we haven't really gone into creating
00:00
directories and the way that we do this,
00:00
by using the make directory command.
00:00
For instance, in this directory in my cd home,
00:00
so we're in home rob.
00:00
We'll start here. We'll make a directory and we'll just
00:00
call this directory somedir.
00:00
Now we do an ls- al.
00:00
In my system here,
00:00
we can see down here that we have somedir here.
00:00
Notice also when we create the directory that
00:00
it has a d over here in the permissions,
00:00
but right below at some file doesn't have that.
00:00
This d indicates it's a directory that we created.
00:00
We can also create a whole directory structure.
00:00
We can create a directory with empty subdirectories
00:00
underneath it if we know
00:00
the directory structure we want to create.
00:00
We can do all that from scratch,
00:00
but we have to use the - p option,
00:00
p stands for parents,
00:00
and provide the entire directory tree structure.
00:00
If we wanted to do this in my home directory,
00:00
we can make directory dash p and we'll create
00:00
an other directory with a sub-directory,
00:00
dir 1, and another sub-directory,
00:00
dir 2 and another,
00:00
subdirectory dir 3 and there we go.
00:00
Now if you do an ls-alr,
00:00
R is for recursive on home rob other dir.
00:00
What we're going to see is home rob
00:00
other dir with a sub-directory dir
00:00
1. Well, what's in dir 1?
00:00
Home rob of the dir 1 has a sub-directory
00:00
of dir 2 and so on and so forth as we go down there.
00:00
Now if we wanted to actually move files around,
00:00
we can use the mv command.
00:00
MV is short for move and it's used to
00:00
move or also to rename files.
00:00
If we move a file in place without actually
00:00
specifying a different location
00:00
to move it too, it'll rename it.
00:00
The syntax for this is move file
00:00
and then the location that you want to move it to.
00:00
[NOISE] Location.
00:00
If you don't specify location here,
00:00
you just give it another name, it'll give it a new name.
00:00
Let's see this in action.
00:00
Let's go ahead and do an ls here,
00:00
and let's touch a file called original.
00:00
Do another ls, and now we see
00:00
the file right here called original.
00:00
If we wanted to move this
00:00
or actually we're not going to move it, let's rename it.
00:00
We can do move original, new.
00:00
Since we're not specifying a new location,
00:00
we're not specifying a path, its just going to
00:00
rename that file in the directory that it's in.
00:00
If we do an ls, where we saw original before,
00:00
now we see new and that's what you need to do with that.
00:00
What we can also do is move the file by
00:00
specifying new location. Let's do real quick.
00:00
Let's look in temp. In the temp directory,
00:00
we see a bunch of stuff, but these are all directories.
00:00
These aren't actually files.
00:00
We just see a bunch of directories here.
00:00
We see some hidden files too, but that's fine.
00:00
What we can do is we can move our new file that lives
00:00
here into temp by doing
00:00
move new and then specifying temp.
00:00
Now we do an ls on temp,
00:00
we can see that we have the new file
00:00
and that's been moved there.
00:00
If you do an ls on home,
00:00
rob or just do an ls on
00:00
tilter I can see
00:00
that the new file doesn't live here anymore,
00:00
it now lives in temp because we've
00:00
picked it up and moved it.
00:00
We'll see how we can copy things here in just a minute.
00:00
But we can also move
00:00
that file back and rename it at the same time.
00:00
For instance, if we want to move the file
00:00
back from temp new,
00:00
we want to move it back to home rob original,
00:00
so we're removing it, we're also
00:00
renaming it to original when we want to move it.
00:00
When we move it over, we can do this.
00:00
Now when we do an ls on home rob,
00:00
we can see the file has been moved back
00:00
and it's been renamed to original.
00:00
The last command we're going to look
00:00
at today is the copy command.
00:00
The copy command is used to copy files and directories.
00:00
CP is the copy command. That's short for copy.
00:00
The syntax is copy file or directory,
00:00
and then the place that you want to copy it to.
00:00
For instance, we can go ls
00:00
home rob again and we see all our files here.
00:00
If we want to copy the original file back to temp,
00:00
let's just do this cp home,
00:00
rob original and then
00:00
we're going to specify we're
00:00
copying it to the temp directory.
00:00
Now if you do an ls on temp, we see it there.
00:00
If we do ls on home rob,
00:00
it's also still there because we
00:00
just copied it, we haven't moved it.
00:00
If we want to copy a directory though,
00:00
we got to do the -r option.
00:00
If we do an ls on home rob some directory,
00:00
ls-al home, rob some directory.
00:00
We can see that that is there and it is
00:00
a directory file and this is the contents of it.
00:00
If we want to copy it all we have to do copy - r,
00:00
that's the recursive option and we'll do home rob
00:00
somedir to temp and keep in mind,
00:00
before we do this, let's actually look at the timestamp.
00:00
Let's say Control C and do an ls on somedir.
00:00
I'll do an ls-l on subdir
00:00
there and we can see the timestamp on
00:00
it and we see
00:00
that the timestamp on this subdirectory recruiter is 744.
00:00
Now if we try and do a copy-r of this directory from
00:00
home rob somedir over to the temp directory.
00:00
Now we do an ls-ald on temp, somedir.
00:00
We see that that's there but
00:00
look at the timestamp has changed.
00:00
It changed from 1744 to 1748.
00:00
The way that we get around this is by adding
00:00
the a option to the copy.
00:00
What we'll do now is we'll copy
00:00
the other directory instead of sub directory.
00:00
You do copy rob other dir to temp.
00:00
Before we do that let's
00:00
Control C out of this and we'll do
00:00
an ls-ald on home rob other dir.
00:00
We can see that was also created at 1744.
00:00
Now when we do a copy-ar on home rob other dir to temp,
00:00
we do an ls dash, ald on temp,
00:00
other dir, we can see that
00:00
the timestamps stays the same.
00:00
Copy-ar retains the permissions,
00:00
ownership and timestamps when you copy
00:00
files and that is sometimes very important.
00:00
But with that, we've reached the end of this lesson.
00:00
In this lesson, we covered performing operations on
00:00
files and directories and we use the commands ls,
00:00
make directory, move and copy.
00:00
Thanks so much for being here and I look
00:00
forward to seeing you in our next lesson.
Up Next