Linux Therapy - Vitamin "L" for the brain


Developer/Linux Consultant
BuSo Pro
Digital Strategist
Sep 23, 2014

OK, so I chose a compellingish (< this word is for sale, serious inquiries only) thread title. I also got in touch with my "Inner DJ" and picked an appropriate song for you to listen to so when the "Vitamin L" hits, you'll be ready.

I dunno, I think the song/video is relevant, don't you agree? I mean, it's called "Therapy" and Ozzy is in it (the irony is intentional). The title of the song also contains a keyword in the thread title. 10 points will be awarded to Gryffindor.

Bet some of you are even checking out the video and going "Damn, he's right, Ozzy really is in that, and also that dude from Suicidal Tendencies". So you came here probably wondering WTH the title meant, and now you're checking out the video. Why? Cause marketing.

But we're here to talk about Linux, and more specifically, some commands/concepts that I would like to share. Some are basic, others aren't. So please step this way. Before we proceed, I'll have to ask you all to move at a steady but even pace, and most importantly, and I can't stress this enough, "KEEP YOUR HANDS ON THE KEYBOARD AT ALL TIMES UNTIL THE THREAD COMES TO A COMPLETE STOP". Obviously, this is for your protection and ours. There were talks with management about this, and it was agreed that an official statement needed to be made, so here you go. The use of caps, red, bold, italic and underline has been added per request. An exclamation mark at the end was considered but found not to be necessary. My NDA will not allow me to speak further on this, so we'll just leave it at that.

Let's get basically basic, basically (Hello, grammar police? Come to BuSo...)
  • So you have a bunch of "stuff" on your console? Clear the screen with CTRL+L
  • Change to your home directory: Just run the "cd" command by itself or "cd ~"
  • Quick look at system stats: Run the "top" command. When you do that, it will loop forever, showing you system load, uptime, memory usage, etc. To break away from this madness, use CTRL+C
  • You can "top" your "top" command. Install "htop" with "apt-get install htop" (on Ubuntu) and tell "top" that you just want to be friends
Kinda basic but not as basic as the basically basic, basically.
  • Make a quick backup of a file with a single command. Let's say you have a file in a directory called "critical_config.conf" and you want to make a backup of it in the same directory before you start working on it. You would run the following command:
  • cp critical_config.conf{,.bak}
*This will make a file called "critical_config.conf.bak". For extra credit you could then do a "chmod 000 critical_config.conf.bak" to help ensure that the backup file will not be written to.
  • Use "nano" instead of "vim" for editing files: I like to use the "vim" text editor, but I also understand that it's a bit confusing to learn at first. If you see config examples and they call for editing a file and the command looks like "vi whatever_file.conf", you can safely substitute "vi" with "nano" and use the nano editor for editing files. Nano is a bit easier to use than vim.
Basically Intermediate

So we have a file called "results.txt", and we are interested only in lines containing the word "good-lead". We can do something like this:

cat results.txt | grep "good-lead"

Time to turn on the disco ball cause this will send the results flying to the screen. That's lovely and might save an otherwise boring night, but let's divert our attention (pun alert) to the use of the pipe "|". The pipe is one of the most fundamental concepts you'll ever learn. You wouldn't think so by looking at it. Just a little "|", against the world. No amount of "enhancement pills" are going to make "|" any bigger. Thankfully in the world of *IX, we don't have to worry about such things as "pipe envy". While we are thinking about what the pipe does, just remember (queue Kung-Fu type music, after the song is over of course) "Without the pipe, the water has no direction" /guru_sounding_mode off

This is clearly a case where "size doesn't matter", it's all about how you "use the pipe". What does it do? Let's deconstruct the command we were entertaining ourselves with before.

The first part of the command "cat results.txt" will print the file to stdout, otherwise known as "the screen". That's what it's designed to do and when we tell Linux to just "cat results.txt", we're going to get the whole file. This might be useful if that's what you want, but there are a lot of times we don't want the whole file.

Let's pretend this file contains information that provides a crude "rating" indicator for the leads. If our system decided a lead was good, it inserts "good-lead" into the file somewhere to let us know. If a lead isn't good for some reason (e.g. incomplete data), the system inserts "bad-lead" into the file. We only want the lines that contain "good-lead", so what we're going to do is use the pipe "|" to send the output (pipe the output) of "cat results.txt" to the "grep" command which is a command used to find strings of text or even regular expressions (if you enjoy hell). So when we this do this:

cat results.txt | grep "good-lead"

What we are saying is "print the output of the file called results.txt, take that output and send it through a pipe to the input of the grep command that will find the good leads". Again, this prints the results to the screen. How about saving the results to a file? Try this on for size (lol, I used a pun again):

cat results.txt | grep "good-lead" > good_lead_results.txt

OMG, what have we done?! Well, if "results.txt" were a real file (hey, files cost money...), we could do an "ls" command and see that "good_lead_results.txt" now exists in the current folder. What we have done can be explained like this: Send the output of cat results.txt as input to the grep command to find lines containing "good-lead", then redirect the output to a file called "good_lead_results.txt". Now we have 3 commands being used on the same line that pull all this off for us. We have also invited ">" to the party and have given him a 3 drink limit (and not those long island iced-teas either). Again, this is for your safety.

Our friend ">" would like to say hi. Just call him ">", there is no last name

Here's what ">" does. When you issued the command that printed your "good leads" to the screen while using the pipe, you were doing all of this with "stdout", that's why it went to your screen. stdout gonna stdout and send whatever to the screen, so we need a way to redirect that output somewhere else, in this case a file called "good_lead_results.txt". That's what ">" does. When you added ">" to the end of the command, you are redirecting the results of the "cat" and "grep" commands to the "good_lead_results.txt" file. To better state it, you are redirecting stout from cat and grep to a file. Pipe sits between the commands, well, doing pipe things.

See what's happening here? I've just shown you how to output pretty much any command that prints to the screen, to a file. Because you've been so patient and I don't normally get many visitors, I'm going to hand out some examples, free of charge. If you had any doubts that I was less than sane, now you know.

One of the first commands you learn is the "ls" command. The "ls" command is going to show you a list of the files/directories in a directory. When we run "ls" (*pro tip, use "ls -lah" for better results), it's printing to the screen, which we know to be stdout. I don't know about you (I really don't), but for me, sometimes it's useful to have a file that contains the info that "ls -lah" outputs to the screen. Hey, we know how to do that!

How about a .txt file that contains the files/folders in a default apache root dir ( /var/www ) on Ubuntu. Easy, let's do this:

sudo ls -lah /var/www > apache_root_dir_list.txt

*I ran the above with sudo because sometimes file permissions/ownership will prevent a non-root user from listing files they don't own or can't read.

If you do a "ls -lah" you'll now see the file called "apache_root_dir_list.txt". You can open this file with vi or nano and take a look at the file, or you can just do a "cat apache_root_dir_list.txt" to print the contents to the screen.

Now that you know how this works, you can apply it any way you like. Maybe you want to have a list of the files/folders in certain WP folders so you can compare the results later? No problem, we got this. We're like that old cartoon "He-Man" or "Thundercats", it's all about that sword man. Voltron concurs as well. Remember the old Voltron cartoons? When it was time to "form blazing sword", we all knew shit was gonna get real. Why they didn't try it first is the reason the cartoons lasted 30 minutes, just so you know.

The End

I checked with my TOO (Team of one) and we didn't have room in the "closing content" budget to allow for a proper closing statement. While this is embarrasing, I'd still like to thank you all for reading and joining me on our tour today. If this helps some of you, please hook a BuSo brotha up with some kind words. Till next time
Dec 12, 2014
Been using linux for a few years. It's really the best... but don't do any of that advanced stuff u got there.


BuSo Pro
Boot Camp
Oct 6, 2014
Somehow you took a boring as hell topic and made me read it.

Bravo good sir. Bravo.