CompTIA Linux+ XK0-005 – Unit 08 – System Maintenance Part 7

  • By
  • August 10, 2023
0 Comment

60. grep

Alright, so Grep, as we talk about a little bit more, is a tool that we use to do a pattern matching. It uses what’s called regular expressions. You probably have heard a lot of Linux and Unix people talking about Grep all the time. In fact, when people are working with files in Microsoft, you always hear that Linux person saying, I bet you wish you had Grep, don’t you? It’s a very powerful log tool. Prints out to your screen all the matching lines and you print the patterns. In fact, the hardest part of Grepp is understanding regular expressions. How do I write a regular expression to find certain types of items? That’s something you’re going to practice, you’re going to have to get used to.

Then you have options like do I want this to be a case insensitive match?Do I want to find the whole word or is the word the expression? I used a part of a word? Like if you put in the word and find A and D, well, if you said whole word, it’s going to look for just the word. And if you didn’t, it would find sand and everything else that has A and D in it somewhere. You can also do the V, say, show me the lines that don’t match, which is another way of eliminating stuff that is in your way. So Grep is a great tool for searching through textual information to find the patterns that you’re looking for. And since we’ve been talking about logs, this is a great way for you to get through a log file that consists of thousands of lines to find the information that is pertinent to what you’re trying to investigate.

61. head and tail sed

Now, the head and the tail again, is pretty straightforward by default. It prints the first or last ten lines of the file. Head for the first, tail for the last. You have some options with the switch, which you could say you want it to display more or less lines. So you could say tail 20 and see the last 20 lines. It’s a great way, especially with log files, because let’s think about it. File. If I’ve got thousands of lines, the most recent thing that happened is always at the end of the file. It’s not written to the beginning. So you automatically tail 20 so you can see the last 20 lines, to see if perhaps what you’re looking for, whatever those events are, show up there in the last 20. So it’s a very commonly used tool that we like to have to go and find information, especially recent information.

62. Sed

Again, the said command. A stream editor is designed to be technically a search and replace for the most part. We’re not going to try to search and replace stuff that’s in our log file. We don’t want to alter it. Perhaps some people might call it search and destroy, but it will search and send the output to whatever standard output you set up. Standard output could be to a file name, could be to your screen. It’s whatever you wanted to do. But if you’d like, you can also delete lines with it as well. It is a very powerful editing tool to edit and work with your log files. For most of us, I’m hoping that we’re going to be looking for specific types of stuff and sending it to a file or to our screen to basically help us in reading information out of our files.

63. Demo – Searching and Displaying Text Files

Okay, we’re going to take a look at some text file manipulation, some kind of cool stuff we can do. And oh, look, I’m already in the VAR log directory. Let’s make sure. Good. All right, so what we’re going to do is we’re going to open up a file that I have to be allowed to do with pseudo or be the root. And it’s the D message log and the command tail of course, means show me the last hand lines. Now the nice thing about that is it makes it very easy to be able to see different parts of your entries without having to go through all of the other file information that might be there. Of course, I might want to go the other direction, which in this case I want to go to the headers with the dash four and say D message and it’s not minus four, I’m not subtracting four means show me four lines, don’t show me the ten.

And of course, providing that there is wrapping going on here as well, it looks like more lines but some of them are just really long. Okay. So I can move back and forth. And of course, let me clear this off. You might be interested in finding a specific bit of text inside of there. So the next pseudo command that I type in here would say, let’s display the contents of the D message and let’s use it, pipe it to look for with Greff e zero, ethernet card, zero. And there’s all of the information about the ethernet card, including the fact that it was up. All right, so again, that’s just ways of grabbing information and making it so much easier than actually just looking at the full outlook.

Let’s pseudo cat deed message, it’s actually not that bad of a file, but still looking through this manually would be a lot more work than using Grep to find those lines that are exactly what you want. Let’s try it again. Pseudo tail, this time with the authentication log. And you can see it’s pretty straightforward there. As far as me, the trainer has been pretty busy getting located on here. If you look up here, some other sessions are being opened by the user route. Some cron sessions, of course, that I created and did in some other examples. Last couple of things that I’ve done. Not only that, but again, I can set up more information, I can do some more manipulations.

Let’s do the pseudo, let’s do another tail of the auth log, but let’s pipe this thing out to the said command and let’s do the S or sorry, let’s do the S. And we’re going to look for slash trainer, which is me and Supercoolg. And this will be the last ten lines of the off log. But I’m going to replace the trainer with hopefully with the words super cool because that’s what S is. Remember, Said is a stream editor. So now when I do that. Look at that. Super Cool is now the name of the user instead of trainer. So you can have a lot of fun when you’re playing with some of these things. But just remember, if you can do it bad people, hackers can do this to you too.

In fact, this is what they love to do to your logs is start using things like a stream editor. Just blow away log entries. Change them so you don’t have any evidence they were over there. So be careful about these commands. Protect your root account and when at all possible, choose to send your log files to a separate computer so that you can actually have some backup if you would. Locations that you have better a better feeling about the integrity of them. Anyway, that’s searching and displaying texts and even having fun with stream editing.

64. awk Part

Now, AWK, as I said before, is a very complex editor. I don’t even want to call it an editor. It is like a scripting language. It allows you to create any number of patterns using if you wanted to see programming, like commands to do any number of things you want, you can use OC to just search for patterns and come up with commands that say, this is what I want you to do. Samples could be like an ox statement that says print. And what do you print? Well, what do you print? You print and then you put in an expression. I want you to print the words a quarter ends in. And then you’ve got a variable dollar sign three. Well, where does that come from? Well, you bring that out of a file, perhaps called month names, and it automatically searches that file, finds those months names, puts it in for that variable, and boom, you’ve got quarter ends in. And you see all that listings.

65. awk Part

It just goes on and on again. It’s a very powerful programming tool and in the early days, in my early Unix days, I remember that we constantly were making OC scripts to just do a variety of daily tasks like check my email, check a log file, whatever it was and then print out to the screen the results so I could see what happened. And we’d even scheduled these with Cron schedulers so they would run first thing in the morning or whenever we wanted them to run so that the output was ready for us when we logged in. It’s a great capability, it’s a great tool. And here’s what I’m going to tell you in five minutes. I can’t describe all that you can do with AWK.

There are books on AWK that are in the hundreds of pages of thickness to describe what can be done with OC scripting and OC commands. I would really encourage you to get if you want to become that super power user in Linux, I would encourage you to learn about AWK and see how you can make your life very nice in the world of Linux by doing all these scriptings and creating all these tools. I mean, you can create tools that can help you when you’re administration of your Linux box through AAC. It’s really cool and it’s just tough to give it to you in a five minute spiel but that’s what it can do. It works with variables, it works with files, it gives you screen output. It just is a super tool. And like I said in some of their commands it is very C programming like in some of the things that it can do.

66. Demo – Searching and Replacing with awk

All right, you’re going to watch a demo of how well I can type. So what I’m going to do is I’m going to open up this little editor text editor program g edit often the choice and I’m going to create a really cool text file that’s going to have the months. So let’s see how well I can spell january, Feb, brewery. The downside is as a text editor I don’t have the benefit of my spell check. So I’m really nervous knowing that you’re watching me spell these things. And even if I can remember all of those months and remember I’m talking to you while I’m typing this stuff. I wish I had a stunt double typist for me. So there we go. I’ve got four lines in, I think I’ve spelled all the months right and I’m going to save these and you’re probably wondering why on earth are you doing this? Well what we’re going to do is we’re going to use AUC to do a little bit of work in parsing through these files and I’m going to call it Month names.

And the way I set it up was that I just created the, the four quarters, three months per quarter and each one on their own line. So that’s something that we’re going to kind of play with when we get to AWK. OK? So now I’ve saved those, I saved them to my home directory. There they are as month names. So that’s good. And what we’re going to do is we’re going to come back to our prompt, I’m going to CD back to my home directory PWD make sure I’m there. Home trainer. So that’s good. And who am I? Good. I’m the trainer. So all is good there. And we’re going to, I’m going to clear off my screen and we’re going to type in an AWK command and the command is going to have some text that we’re going to deal with and we’re going to print, print means to the screen, not to a piece of paper.

And we’re going to say that a quarter ends in and end quotes comma and we use dollar sign three to represent the third line end of the single quotes of the file called month underscore names. I think that’s what I called that. And hit enter. And look at that. It looked at the actually that was the last column I should say, not the last line, sorry. The last column in each of those files with that option and it says March, June, September, December. So it looks like I just did a little bit of magic there. It read the third column in each line and columns by the way were done by the space being the delimiter between the different values. Okay. So the next thing we’re going to do is we’re going to try another little OC thing and you’re going to look to see how well I can type again as I create a new file.

In this case, I’m going to try to do some grades for three different people. And one more time I’m going to be nervous as I let you watch me type to see how well I do this. Of course you all don’t know if I’m spelling the names right, but we’re going to assume that their grades are based on a total of 100. So there’s some grades for Wanda and Dylan here. We’re not going to let Dylan perform as well as Wanda. And then here comes Ashley and Ashley, wow, really showed off on one test there. And again the space bar is my delimiter between everything that I’m doing. I’m going to save that as I think we’re going to save that as grades. So I’m just going to save it as that file name grades. And then what we’ll do is we’ll write another little OC script to deal with the grades but we’re going to see how we can do even a little bit more cool stuff with OC.

Now normally you actually save your OC script as its own text file and then just refer to it with this OC command, but we’re actually choosing to just kind of put it all here on display for you here. So again we’re going to print. Now remember printing is going to put it on the screen. First column was the name comma. And then we’re going to say quote has a or and I suppose some good English there options comma. All right, so the stuff in double quotes is exactly what’s going to be printed on screen. But remember what’s in the single quotes is really the actual command being run by OC. Now we’re going to put in here the variables for the dollar sign. Two, we want column two added to column three added to column four and we’re going to divide those by three comma. And that’s going to be the average and the average that we’re going to print for.

Again, the quotes around the actual word. We’re going to end the command with that little brace put in the ending quote, and then the name of the file called grades and I’m going to hit enter and there we see the averages that we did. So I’m not going to continue to do that and show you all these other little options. I think you’re getting the idea of what you can do with the AK scripting. Again, like I said, these lines could actually have been stored in a text file, could have been run right off with AWK. It’s really a cool program to especially if you’re going to create a permanent script that you can rerun anytime you want to. At the command line you actually just say OC of this command. You can run a bunch of OCS in a script. It’s just, it’s phenomenal the stuff you can do with it. So I really encourage you to play around and learn what else you can do.

67. Unit 08 Review

All right, so in this section, we talked about system maintenance. There’s a lot to be said about maintenance from backing it up, restoring the data, file compression, creating these little tape archives, scheduling tasks one time or multiple times, monitoring your system performance, what you should be looking for when you’re monitoring that performance, looking at your auditing capabilities, your logging user activity, system kernel. All of these things deal with maintenance, with the actual system. And it is a lot of work, fortunately, through scheduling jobs, through scripting languages like OC, through logging daemons running in the background, and you just configuring them.

Most of it is automated, making it very simple for you to almost set it and forget it. But remember, I have kind of gotten on my little soapbox, as I call it, saying to you, always use this proactively. Don’t just wait for something to die and then say, oh, I wonder what happened. You’re gathering the information for a reason. And I like to think that the reason is, is to keep the system running really well. So performance, logging, scheduling things appropriately, having backups and restores in case the worst happens, all a very crucial part of system maintenance.

Comments
* The most recent comment are at the top

Interesting posts

IBM Certified Data Scientist: Building a Career in Data Science

In today’s digital age, data is the new oil, driving decision-making and innovation across industries. The role of a data scientist has become one of the most sought-after positions in the tech world. If you’re considering a career in data science, obtaining the IBM Certified Data Scientist certification can be a game-changer. This certification not… Read More »

How to Balance Work and Study While Preparing for IT Certification Exams

Balancing work and study while preparing for IT certification exams can feel like an uphill battle. Juggling a full-time job and intense study sessions requires careful planning, discipline, and creativity. The pressure of meeting job responsibilities while dedicating time and energy to study can be overwhelming. However, with the right strategies and mindset, you can… Read More »

10 Highest Paying IT Certifications

In the ever-evolving world of information technology, certifications are more than just a feather in your cap – they’re a ticket to higher salaries and advanced career opportunities. With the tech landscape constantly shifting, staying updated with the most lucrative and relevant certifications can set you apart in a competitive job market. Whether you’re aiming… Read More »

Strategies for ISACA Certified Information Systems Auditor (CISA) Exam

Are you ready to take your career in information systems auditing to the next level? The ISACA Certified Information Systems Auditor (CISA) exam is your ticket to becoming a recognized expert in the field. But let’s face it, preparing for this comprehensive and challenging exam can be daunting. Whether you’re a seasoned professional or just… Read More »

Preparing for Juniper Networks JNCIA-Junos Exam: Key Topics and Mock Exam Resources

So, you’ve decided to take the plunge and go for the Juniper Networks JNCIA-Junos certification, huh? Great choice! This certification serves as a robust foundation for anyone aiming to build a career in networking. However, preparing for the exam can be a daunting task. The good news is that this guide covers the key topics… Read More »

Mastering Microsoft Azure Fundamentals AZ-900: Essential Study Materials

Ever wondered how businesses run these days without giant server rooms? That’s the magic of cloud computing, and Microsoft Azure is a leading cloud platform. Thinking about a career in this exciting field? If so, mastering the Microsoft Certified: Azure Fundamentals certification through passing the AZ-900 exam is the perfect starting point for you. This… Read More »

img