Wednesday, November 10, 2010

Linux CLI (Command Line Interface) Part 2

(Note: This is a continuation of “CLI Part 1”. It would be best for beginners to review the first installment before continuing) CLICK ANY IMAGE TO ENLARGE

Checking System Stats

Like many users people what to know the status of their systems and hardware. So the first thing we are going to cover is how to check system information from the CLI. Up front I would like to mention the programs we are going to be using to check system stats. The commands are as follows: free, top, df, and ps. Before we begin I would like to break down what each of the previous stated commands are for. Free, is for checking RAM and Swap usage. Top, is like the “Processes” Tab in Windows Task Manager, It will show current applications in use and what resources they are taking in real-time. DF, shows mounted file-systems with information on their capacity and total amount used, such as you Hard-Drive or USB Flash Drive and others. Finally, PS, will list all current processes/services loaded with their current PID number, and will allow you to have the option of terminating a process or service if it is either hung, frozen, or in limbo. Keep in mind top can do the same thing but is a bit more difficult to use. I will get to that later.

Free is a great tool that to my knowledge comes with every flavor of Linux. As stated its used to check system memory used as well as swap space. Free is straight forward. Simply type “free” in the command line and you will be represented the following information:

As you can see on the “- / + buffers/cache” under the “used” and “free” columns I currently have 320,500 Kb (Kilobytes) of RAM currently taken, and 641180 Kb of RAM available. It's simple and easy to use. You don't really have to do a Kb to Mb (Megabyte) conversion to get the exact amount. There are 1024Kb in 1 Mb, so if you know that, you can get an estimate of how much is taken/available.

Top, unlike the ps command, allows you to watch resources and applications in real time. Just like ps, in Top you can kill processes from it, but it can be difficult to filter the list because there are more things running then you realize and using ps is just an easier way. Below is a picture showing what the interface of the Top command looks like.

If you notice across the top you are given more information then just applications. As I said everything in top is displayed in real time, so on a side note it will cause your CPU usage to spike. On the top you will find number of  users logged in, total amount of tasks, ram information, program states (sleeping, stopped, zombies), and more. It is a very useful tool. Along with just showing applications it shows their PID, owned user, percent of memory used, CPU cycles used and more. If you wish to kill off an application that is near the top of the list and is visible, press the “k” key to open the “kill” function. All you have to do is enter the PID number next to the process you wish to kill and that's it. Now lets move on to “df”.

It's always nice to see how much disk space you have available. Just to make sure you are not running out, an see if you are needing to get rid of some things or buy another hard-drive. With that you can run the simple command, df. DF will show all mounted file-systems, their total capacity and how much is taken. By viewing the below image you will see what I mean.

As you can see on the first line in the output area, you can see that my partition “/dev/sda1” (sda meaning the first drive and 1 meaning the first partition) is mounted on “/”. Like I said you can see the available space field, and used field which is given by a percentage. Other then that there is not a whole lot more use you can get out of it for just that basic purpose.

Now ps is a good command for finding running process if they need to be killed off. It's sad but true, that Linux isn't perfect. Applications do crash from time to time. Unlike Windows however, it can be recovered with no reboot necessary. There are different flags or arguments you can use with ps and I prefer “-ax”. The argument -a displays all w/ tty except session leaders and -x displays processes w/o controlling ttys. I won't get into what all that means at this time but I thought I would at least explain them. Executing the command in this fashion will look like this:

The list goes off the screen as you can see. If you a looking for a specific program you can pipe it into the grep command. If you are say looking for firefox it would look like this, “ps -ax | grep firefox”. Just on a side note to use the pipe symbol you press shift along with the “ \ “ key. I will go into using the pipe feature in a future CLI session. Anyways back to subject. As show in the picture, if I wanted to kill say the Gedit program that has the file “New Blog Post.txt” open I would type in “kill 1709”. You kill the PID associated with the target program and you find it by looking at the correct line so it was,
“1709 ? S 0:00 gedit /home/user01/Desktop/Next Blog Post.txt”. The first number on the line is the PID connected with that application.

Well that about wraps up system status tools so now we go on.

Finding and Installing Software
There are many different types of package managers out there and they are all different in their own way. For this session I will be covering “Apt”. This is used mainly in Debian based distributions. I may discuss managers like “yum” at a later date. First thing is first, I am going to cover how to find software before installing it. Kind of makes sense, no? I would like to point out at this time you will need some sort of higher privilege to use the apt program. An again I am going to assume you have “sudo” access. Sudo allows you to run a program as a higher privilege level. It will ask you for your personal user account password to continue to execute the command at a sudo level. I will warn you, don't use sudo for every command, only use it when it is absolutely required. Just a security precaution.

Now, when you are looking for a program you need to use key words. I will give an example. Say you want some video editing software, you are going to issue and apt command with some key words, such as “video” and “editing”. The command to look for such software is: “sudo apt-cache search video editing”. When you issue such a command you will get results like this:

After your search you will be given a list of packages, if any. On the far left is the package name and next to it is the package description. Now trying software and installing is easy thanks to the creation of package managers. When it comes to installing software it use to absolutely ludicrous. Having to install the correct version, finding all its dependencies, compiling, and linking everything properly was a headache but you can pet an thank your package manager for helping to fix all of that. To install a package its a simple as typing “sudo apt-get install (package-name)”. Just simply drop in the package name in the list displayed from your “apt-cache search” command and let apt do the rest.

Once its done you are set. You have successfully downloaded and installed your first package. If you ever wish to uninstall a package simply use "sudo apt-get remove (packagename)".

How to run Program from a Terminal

This section won't be very long so enjoy it. Running a program for a terminal is not difficult at all. For example, if you have say the Firefox Web Browser installed, all you have to type is “firefox” and press Enter. … … … That's it. Nothing more to it. Just like using the “Run” feature in windows. However you will notice that running a program in this fashion takes control of your terminal window. If you wish to run a program and still have terminal functions, use the “&” symbol at the end, like we covered in Part 1 of this series. To use it, the command would look like this, “firefox &”. Tada! You know have full control over your terminal window once again while staring a new application.

How to Use and Create Symbolic Links

First off you are probably asking, “What are symbolic links?” To put simply, they are almost like shortcuts in Windows. An you can create these symbolic links to have faster access to files, folders, or executables and it's not that difficult. For this tutorial we will be just making a symbolic link for a given folder. So lets get started. As shown in this picture below we will be creating a symbolic link in one folder to direct us to another.

As you can see in the first line we are using the command “ln”. It is used when creating symbolic links. We then follow it with a “-s” for soft-link. Not using this flag creates a “hard-link” and linking to a directory MUST be a soft-link. Again, in the first line I am linking a directory in my hidden wine (Windows API program) folder to my current location. When using ln your target file/directory is first and then you put your destination last. When I preform an ln, you can see our newly created link called “windows” in the Blog folder, our current location. Now when I cd (change directory) to our symbolic link “cd windows” it takes me right to “/home/user01/.wine/drive_c/Program\ Files/” without having to type in it's full path I just demonstrated. So again they are basically shortcuts to the specified folder or file.

Well that about wraps up Part 2 of the CLI HowTo. You get a bonus prize if you stayed with me for this long. See you next time. And,

Embrace the power of the CLI!

No comments:

Post a Comment