#13 - Crash Course on Common Commands

About Episode - Duration: 11 minutes, Published: 2013-08-20

In this episode, I will give you a crash course on how to use common commands for getting around the filesystem, reviewing running processes, looking at disk utilization, and editing files.

Download: mp4 or webm

Get notified about future content via the mailing list, follow @jweissig_ on Twitter for episode updates, or use the RSS feed.

Links, Code, and Transcript


In this episode, I will give you a crash course on how to use common commands for getting around the filesystem, reviewing running processes, looking at disk utilization, and editing files.

Lets jump over to a CentOS 6.4 64-bit virtual machine where we can have a closer look. First lets list the directory contents of slash, by running “ls /”, this will show us the root directory structure, which should look pretty familiar after reviewing the filesystem hierarchy standard covered in episode #12.

ls /

The ls utility shows us the directory contents, but there are many command line options for the ls utility that we can use, to find out what they are, we can use this extremely handy utility for reviewing the documentation or manual pages, it is called “man”.

So, we can run the “man” command, and then the utility that we want to review the manual page for, for example, lets run “man ls”, to review the manual page for the “ls” utility.

man ls

I use the page up and page down commands to move around the document. The man pages are great for learning about what all these utilities can do. You can hit the letter “h”, to review the help page, or the letter “q” to quit the man page.

So, we already know about the ls, and man commands, there is also a command to show us the directory we are currently in, it is called pwd, which stands for “print working directory”, after running it, you will see it outputs the directory we are currently in.

pwd

We can change directories, by using the cd command, which stands for change directory. So, we are currently in the slash directory, we can just type “cd”, without any destination, and it will put us into the home directory for the currently logged in user, in this case, root. We can verify this by running, pwd again. We can also run the “cd” command with a destination, something like “cd /root”, and then run, “pwd” to verify it worked.

cd /root

What else? Well, I like to know general things about a system I am on, like what mounts it has, and what processes are running. Lets look at the “top” command, which will display currently running tasks or processes, lets take a look by running, “top”.

top

This will tell us about the uptime of the system, how many users are logged in, what the load is on the system, along with cpu and memory usage. Then down here, you will see a listing of processes using the cpu. Top is very handy, and there are lots of options, I suggest checking out the man pages for it. You can hit the letter ‘q’ to quit.

If you just want a output of the currently running tasks, you can also run the “ps” command, which will give you a snapshot of the currently running tasks, lets run “ps aux”. Lets just scroll up, and let me explain a little bit about the output, the first column displays the user who owns this process, next the process ID # or PID, next we have percentage of cpu and memory, then the last column here displays the command.

So, lets take a look at the last line here, this is the root user, the process ID #, cpu, memory, and then we have the command, which was executed, in this case, we ran “ps aux”. The “top” and “ps” commands are useful for peaking into what is running on a particular system.

ps aux

Okay, so now we know about the ls, man, cd, pwd, top, and ps commands. What about filesystem mounts and disk storage?

To see what mounts a system has, like to see how much storage is free, etc, we can run the “df” command. I am going to run “df -h”, which will return the output in a human readable format. Let me explain what you are look at here, we have the mount device, size, used space, free space, percentage used, and mount point. Lets take the second to last line as an example, the device /dev/sda1 is 485 megabytes large, we are using 32 megabytes, with 429 free, giving us a utilization of about 7 percent, and it is mounted at /boot.

df -h

This is great for mount points, but what about directories? The du command is useful for getting a handle on what a particular directory is using, space wise, lets run “du -hs /usr”, so you can see that the slash user directory and all sub directories are taking up about 409 megabytes of space.

du -hs /usr

I just want to cover a couple additional commands for working with files and directories. First, lets “cd” to root’s home directory. We can do this by simply running “cd” or “cd /root”, then, lets list the files by running, “ls”.

Okay, looks like there are three files in here. We can use the “cp” command to copy files, lets run “cp anaconda-ks.cfg anaconda-ks-new.cfg”. Lets run “ls” again, to verify that it indeed created a copy, cool, looks like it worked.

cp anaconda-ks.cfg anaconda-ks-new.cfg

You might have noticed something strange happen when I was entering that copy command, like the text almost appeared instantly, or that I’m a really fast typist. There is this great featured called command-line completion or tab completion. It works by, typing a couple letters of the command, file, or directory you are looking for. For example, lets type “cp” again, then I’ll type a couple letters of the “install.log” file, and then I’ll hit that tab key, notice how it auto completes the word! This can be really handy for completing commands and working with files. There is a great wikipedia page on command-line completion, which you should check out, I have linked to it in the episode notes below.

Okay, now that we know about copying files with the “cp” command, and about tab completion, lets see how we move files. Lets move our newly created anaconda-ks-new.cfg to a file called testing.cfg. Lets run, “mv anaconda-ks-new.cfg testing.cfg”, so you can think of this like the origin and destination names. Then, lest run “ls” again to verify our changes, yes, looks like it worked.

mv anaconda-ks-new.cfg testing.cfg

What about removing files? Well, we can use the “rm” command for that. Lets type “rm te”, and then hit tab, notice how it tab completes for us! We are prompted, asking if we really want to delete this file, lets hit the letter ‘y’, for yes, we want to remove it.

rm testing.cfg

You can also make and remove directories, by running the “mkdir” and “rmdir” commands. Lets try an example. Lets run “ls”, so we have an idea of what is in the directory. Okay, lets run “mkdir testing”. Then, run “ls” again, cool looks like our directory exists, I’ll just cd and ls in here to see what is looks like. Okay, we can remove directories too, lets run “rmdir testing”, then “ls”, and it is gone. One interesting thing to note, is that this only worked on empty directories. You would have to run something like “rm -rf testing” if the directory had anything in it. “rm -rf” is dangerous, since you can wipe out system bits easily, so be careful.

mkdir testing
rmdir testing

This pretty much covers everything except working with files. I just want to show you a couple quick commands for looking at, and editing, text files. There is a great tool for looking at text files called “less”, for example, lets say we want to look at the anaconda-ks file here, lets run “less anaconda-ks”, and hit enter. You will notice that we can use the same commands from the man page viewer earlier, so page-up and page-down, moves us around, the letter ‘h’, show us the help page, and the letter ‘q’ existing the viewer. Easy enough, right.

less anaconda-ks.cfg

There is also a command to simply dump the contents to our terminal window, lets run “cat anaconda-ks”, you’ll notice that this is just printed on the screen. We can just use the scroll up and down functions of the terminal to review the documents, note, that this is not good for longer files, this is where the less command comes in handy.

cat anaconda-ks.cfg

Okay, to conclude this episode, lets look at a simple editor, which we can use to view files, and make changes. First, lets create a copy of this anaconda-ks.cfg file, and call it testing.cfg. We will use the “vi” command, so lets type “vi testing.cfg”. Vi is extremely powerful, and there have been books written about it, so, anything I say here will not do it justice, but lets look at a basic way to edit a file.

vi testing.cfg

Lets hit the “insert” key, over by the page up and page down buttons. You will notice that down in the bottom left hand corner, that a insert keyword appears, this means we are in interest mode, and can just type freely. So, we can enter new lines, or change testing easily.

# 
# to exit insert mode, hit Esc, then to save the file, we need to 
# type (without the ''), ':wq'
#

To exit “insert mode”, we hit the Esc key, then to save the file, we need to type, without quote, “:wq” and then enter. Lets type this. So, lets hit Esc, notice that the dash dash insert dash dash is now gone, then lets type a colon, notice that it is displaying our input here at the bottom, then type ‘wq’, this stands for write and quit. Done, we have updated the file, lets use the “less” command to review our edit, so type “less testing.cfg”, great looks like it worked!

less testing.cfg

I know we have covered a lot in this episode, but armed with these commands and an understanding of the filesystem layout, you are be well equipped to explore linux based systems. You should also check out the man pages for the commands mentioned in this episode, if you want to create a virtual machine, check out episode #1, where I talk about VirtualBox and CentOS.