Besides performing basic file operations
like reading, writing, and moving,
we can also manipulate files so that they're more useful.
So, we're just going to go over a few ways
to accomplish this, as well as get metadata about the files.
So the first thing we're going to do is we're going to just look
at a software list.
So, we're just going to use this n option just
to see the number of lines
and going to look at this really quick.
So you see, we have a clear view of the software list
and I added the n option
so we can see the number list
and that's going to be more useful later on.
What we're going to do next
is we're going to use the sort command
And what that is going to do
is sort the file alphabetical order.
And we're going to use the K option
which refers to the column.
So, column one of the software list.
Now, we see this isn't perfect.
There's more options available to make sure
we're not including the header, but you see
that it just data dumps pretty much the entire file
and sorts it from A to Z.
Now, we also do the same thing for the second column
which is going to be pretty much all Ns and a Y.
Next thing we want to do is get some metadata
from this file.
So, we're going to do the word count command.
Now, we're going to do it raw by itself.
So, we're going to use software list3 again.
And if you look at the output,
the first output is the amount of lines in the file,
the second is the amount of words,
and the third is the amount of characters.
Now, this includes all characters, non-printable characters,
anything that the computer uses to process.
So, if we want to see that view so we can see again,
n, software list3, and we see it has 18 lines there,
it's 54 words, 242 characters.
Now again, characters are anything
that's processed by the computer.
So if you look at this display, you'll see that space, tabs,
all these things are characters
and they're encoded to the operating system
so that the computer knows what to do with these things,
how to display that on the screen.
Of course we might not see it,
but this is what the computer sees.
So, all these characters is what's counted
by the word count command.
And again, we can make this very simple
if we just want the word count.
So, we do the w option, software list3 again
and just give us the exact amount of words that we need.
And of course, more options
for lines or characters themselves.
Again, sticking with these same files,
we can also use a command called diff.
So remember, it's going to give us the differentials
between those files.
So, pretty much it's going to let us know which lines
should be added or subtracted
from the first file to match the second file.
So, let's try this out really quick.
So I have a software list I created earlier.
One has less of the software than the other.
So, software list1 is going to have list,
then software list three
'cause I can find these earlier.
And now, what this is telling us
is if you look at the arrows, those arrows pointing
to the right indicate what lines need to be added
to the first file in order to replicate the second file.
So, if you see here, 12, what that means is
after line 12 in software listing one,
add lines 13 through 18 to software listing one
so they can be equivalent.
So again, that's where we've used this cat feature
right with the count
just to verify the correctness of this.
So, we'll see software listing one
has 12 lines and software listing three has 18 lines.
So, it's telling us from lines 13 through 18,
add those to software listing one
in order for them to be equivalent.
Again, this is mostly used when we have a different version
of the same file
and it quickly gives us the differences between the two.
Another command we're going to use is grep.
And we're going to use an example to be able
to pinpoint key information that we would need for a search.
Okay, so we're going to look at this file
and we're going to use the one for the logs,
for the login files.
Okay?
Now we see it gives us a plethora of information,
but sometimes we want to look at specific types of failures
or specific type of errors within a log.
So, we're going to try to look up a failed password attempt.
So, I'm going to try to, I'm going to switch to a user
and I'm going to log in.
I'm going to just try to log in with the wrong password
and I want to pinpoint that in the log.
Okay, so we see you have an authentication failure
when we're trying to log into the ariley account.
So, what we're going to do is we're going
to find this failure in the log.
So, we use the grep failed and we're going to go clear,
log, secure and we can see there towards the bottom
that password check failed for user ariley.
So again, this is a quick way to search through large logs
and get the key information that you need.
Next we're going to go over linking.
Now, there's two ways to do this.
We can either create a hard link from one file to another
or a symbolic link or soft link.
So for example, first thing we want to do,
we're going to make some directories really quick
into the backup folder.
So, we're going to do the backup,
the log, we'll make an authentication one
and we're also going to make up one for just the current year.
All right.
So, we're going to make a copy
of the secure log into our own folder
that we just created in the backup portion.
So, we're going to do sudo copy var log secure.
I want to make sure we put it in our backup log order.
Okay?
Now we're going to go to the backup log, list it out,
make sure that was created.
So, by default without any options,
when you use the link command it's going to create a hard link.
So, let's do that first.
We're going to do the hard link
for the secure folder that we just secure file.
I mean, that we just copied over.
And going to link that to the 2022 secure file.
Now, a hard link again,
is a full copy of the target destination
and a symbolic link is going to be a reference
to that target.
So if that target gets deleted,
then that symbolic link no longer has any information.
Let's just verify that that was created.
So, we're going to do a list.
2022 folder.
And we can see that blue around it indicates
that it's a link.
Now, let's just verify that the secure file
in the authentication folder is the same
as the secure file in the 2022 folder.
We're just going to look at the bottom portion of this.
And do the same thing for the 2022 folder.
Secure file.
And we can see that they're identical.
So, the next thing we want to do to verify this hard link
is we're going to add some information
to the secure file under the authentication folder
and see if it reflects to the secure file
in the 2022 folder.
So, let's do that really quick.
We're going to nano.
Now, we're going to say some simple information like,
use this month, simple enough.
Going to do control O to save this
and then control X to get out.
So, let's verify this really quick by looking
at the first 10 lines of the 2022 folder, the secure file.
And now we see that it has that begin 06, 2022 line
that we input earlier.
Again, just to double check, go to the authentication folder
and we'll see that successfully linked
and remember that these are copies and not just links.
So, we're going to go ahead and remove this file.
Now, we can verify that even
though we deleted the authentication secure file
that the 2022 secure file is still there.
So, let's just list out that folder
just to make sure it's still intact.
The 2022 folder.
Okay, we see our secure file is still intact.
Now we're going to go to our home directory.
Now, this time what we're going to try to do is see
if we can create a link
between the backup file system
and one into our home directory.
We can go sudo link.
And honestly, we should get a error at this point.
Now with hard links, one thing that we have to take account
is the type of file system.
For a symbolic link, we can span different file systems.
For a hard link, these file systems have to be the same.
So, let's check out what the current file systems are.
If you look at the centos root,
so sda2, the root, the mount point, the root folder,
you see the type of file system, it's an XFS.
And the ones we wanted to reference, the backup log,
so look at the mount point all the way to the right
and at the very bottom you'll see the backup log.
The file system type is EXT4.
So, they're incompatible.
So, we will not be able to create a hard link,
but we can create a soft link.
Now we're going to do the same thing,
but again, we're going to create the S option.
And now if we list out our folder,
we see the off auth dash log file as created.
Now we're going to just verify
that that link actually has the information
that we actually want to see from this file.
All right, looks like we have made
a successful symbolic link to that file.
So, here we're going to show the difference
between the hard and soft links
by removing the secure file that's in our 2022
under the backup and see if we're able to still access it
in our off log and our current working directory.
So, let's do that.
All right, that's removed.
Now, let's just try to view the contents of this log.
And we see it says no such file or director exists.
Again, 'cause it's referencing the secure file
in the 2022 folder.
So if we list it out, we'll see we have a broken link.
That's what the flashing indicates.
So again, these soft links reference an existing file.
The hard links create a copy
of the existing file and constantly updates
every time the target file gets updated as well.
This goes for even
if the file name is changed or the location is changed,
the link is not automatically updated .
so you're going to have to go in
and reassociate that link to the new file location
or the new file name.
Let's sum things up.
We went over how to view different files,
sort them out in alphabetical order,
word count, of course getting information
about the amount of lines in the text,
the amount of characters, also the word count as well.
We also went through how to search things with grep.
And also, last but not least, symbolic versus hard linking.
Thank you and I'll see you in the next lesson.