SYSTEM STATUS AT A GLANCE - Learning Ubuntu: A Beginners Guide to Using Linux (2016)

Learning Ubuntu: A Beginners Guide to Using Linux (2016)

CHAPTER 5

SYSTEM STATUS AT A GLANCE

Log into your server, with the new user that we created in the last chapter. From here, through the rest of the book, we will not be using the root user id.

When you first log in, you see some useful information, which will give you an idea of how much system resources you are using.

On the left you will see the following:

· System Load – Average usage of computational work the system has performed over a period of time.

· Usage of / - Total amount of storage used

· Memory Usage – Percentage of memory being utilized.

· Swap usage – Amount of swap memory being used.

o This is virtual memory from your hard disk.

On the right we can see the following:

· Processes – Number of system processes running.

· Users logged in – Number of users on the server

· IP Address for eth0 – In this case it is our public IP

As those numbers increase, your systems performance may start to lag. A good systems engineer monitors these statuses and scales more resources before that occurs.

NAVIGATING, MAKING DIRECTORIES, AND FILES

Some of this is going to recap earlier chapters, but this repetition is important to remembering the commands that you will be using frequently in daily usage.

Now that we have logged in, use the pwd command to see our current directory.

Find Your Current Directory Location

Command

pwd

Result

System will output the directory that you are in.

CLI Display

milo@ubuntu:~$ pwd

/home/milo

Notice that our directory this time is different than it was when we logged in with the root user. Last time we were at /root, which was the home directory for root. This time we are at /home/milo, which is the home directory for the user milo.

Tip: If you ever need to get back to your home directory use cd, to get back to your users home folder.

Now that we are in our home folder, let’s make a new directory called ‘documents’.

Making a New Directory

Command

mkdir documents

Result

System will create a child directory called documents

CLI Display

milo@ubuntu:~$ mkdir documents

milo@ubuntu:~$

Use the ls command to list the contents of our home directory and see if the directory ‘documents’ is there.

List Files

Command

ls

Result

Lists the files in the current directory

CLI Display

milo@ubuntu:~$ ls

documents

From using ls, we now see that our directory is empty outside of the directory ‘documents’.

Let’s use what we have learned from the previous chapters to practice our navigation skills and go into the documents directory.

Try using the command: cd Documents

You may notice that it returns, “cd: Documents: No such file or directory.” Linux is case sensitive, which means that ‘Documents’ and ‘documents’ are two separate things. You will want to keep that in mind as you use Ubuntu, since using the correct case matters.

Now let’s do it the right way.

Change to a Child Directory

Command

cd documents

Result

Changes to the documents directory

CLI Display

milo@ubuntu:~$ cd documents

milo@ubuntu:~/documents$

Use the ls command to list the files in the directory, you’ll notice that it is empty.

Now let’s go back home by using, cd.

Change to User Home Directory

Command

cd

Result

Changes to user’s home directory

CLI Display

milo@ubuntu:~/documents$ cd

milo@ubuntu:~$

If we use pwd we will find that we are back at, /home/milo.

Now, use cd with the full path to the documents directory. Remember, that Linux is case sensitive.

Change Directory Using a Full Path

Command

cd /home/milo/documents

Result

Changes to specified directory

CLI

milo@ubuntu:~ $ cd /home/milo/documents

milo@ubuntu:~/ documents$

If we wanted to get the parent directory of this folder we can also use: cd ..

Move Up A Directory Level

Command

cd ..

Result

System moves us to the parent folder

CLI Display

milo@ubuntu:~/ documents$ cd ..

milo@ubuntu:~$

You should be getting the hang of it now!

Experiment some more and in the next section we will create some files using the built in text editor, nano.

CREATING AND EDITING FILES WITH NANO

Nano is a useful text editor for the command line interface (CLI) in Linux systems. Since we are using the CLI, nano is keyboard oriented with control keys, rather than using a mouse. By learning some of the control keys, you will be able to quickly edit files from the nano editor. Once you pick this up and learn more about using Linux by the command line you can then move onto more advanced editors like vi and, which are great, but often have a steeper learning curve.

Nano is easy to use and great for beginners. It is often installed by default in Ubuntu and other distros and works very well with sudo from the command line. It’s a WYSIWYG editor; “what you see is what you get.”

If you built your Ubuntu server with DigitalOcean or another VPS provider it is already installed. In some circumstances if you installed Ubuntu manually, if may not be installed.

We will talk about installing packages to Ubuntu more in a later chapter, but in case you do not have nano, you can install it by using the command: sudo apt-get install nano

Again we will discuss installing packages in more detail later, but that command, if you don’t already have nano, will install it to your server.

Let’s go ahead and create a file using nano.

Creating a file in nano

Command

nano notes.txt

Result

System launches nano for new file notes.txt

CLI Display

milo@ubuntu:~$ nano notes.txt

Now, you will notice that we are in a simple text editor. Since we do not have a mouse we can get a list of the control keys by pressing control and G at the same time.

See a complete list of nano command keys in the table below:

Nano command keys

^ represents the control ‘Ctrl’ key.

^G (F1) Display this help text

^X (F2) Exit from nano

^O (F3) Write the current file to disk

^J (F4) Justify the current paragraph

^R (F5) Insert another file into the current one

^W (F6) Search for a string or a regular expression

^Y (F7) Go to previous screen

^V (F8) Go to next screen

Nano command keys (cont.)

^K (F9) Cut the current line and store it in the cutbuffer

^U (F10) Uncut from into the current line

^C (F11) Display the position of the cursor

^T (F12) Invoke the spell checker, if available

M-\ (M-|) Go to the first line of the file

M-/ (M-?) Go to the last line of the file

^Y Prev Page ^P Prev Line ^X Exit

^V Next Page ^N Next Line

In the nano editor, enter the following text:

Nano notes: Nano is a useful text editor for the command line interface (CLI) in Linux systems. Since we are using the CLI, nano is keyboard oriented with control keys, rather than using a mouse.

Once you have the text entered, use the exit command keys:

Control + X

You then will be prompted if you want to “Save modified buffer?” Press Y then Enter to save the file.

Now let’s see what is in our folder by using the ls command.

List Files

Command

ls

Result

Lists the files in the current directory

CLI Display

milo@ubuntu:~$ ls

documents notes.txt

EDITING, DELETING, MOVING, AND COPYING

With our notes.txt file created, we can now learn how to move, edit, delete, and copy files.

To get started, let’s copy the notes.txt file to a new file called test.txt. To do this we will use the cp command, which will allow us to copy one file to another. To use cp, we need to also provide the file we want to copy, and also the file we want to copy into. See the table below:

Copy to New File

Command

cp notes.txt test.txt

Result

System copies notes.txt and creates a new file, test.txt with the contents.

CLI Display

milo@ubuntu:~$ cp notes.txt test.txt

milo@ubuntu:~$

By using the ls command, we can check to see if the file was copied.

List Files

Command

ls

Result

Lists the files in the current directory

CLI Display

milo@ubuntu:~$ ls

documents notes.txt test.txt

Now that we have two files, we should open test.txt in nano and make a few changes.

Open a File in Nano

Command

nano test.txt

Result

System will open test.txt in nano.

CLI Display

milo@ubuntu:~$ nano test.txt

When you open the file, you will notice that it has the same text as our notes.txt file. To the beginning of this document, let’s add a line: This document is a test for learning Linux.

Once that is added. Save the file as we did earlier.

With the cp command, the file is copied. When we move a file, it is like using cut in Windows as the original is deleted.

We will now move test.txt into the documents directory. By using the mv command we can move files. After mv, we put the name of the file we want to move and then the location we want to move it to.

Move a File

Command

mv test.txt documents/test.txt

Result

System will open test.txt in nano.

CLI Display

milo@ubuntu:~$ mv test.txt documents/test.txt

milo@ubuntu:~$

If you change your directory to documents and use ls you will find the test.txt file is now located there.

Change Directory to Documents and List Files

Command

cd documents; ls

Result

System will change to documents and list files.

CLI Display

milo@ubuntu:~$ cd documents; ls

milo@ubuntu:~/documents$

test.txt

You may notice above that I used a semi-colon after cd documents. This allows us to combine two commands. In this example the semi-colon allowed us to change directory and then list the files in the new directory.

Now we can also use mv to rename a file. Using the command below, we can rename the file and list the files in the directory.

Using mv to Change File Name

Command

mv test.txt newtest.txt; ls

Result

System will change the file name to newtest.txt and list the files in the directory.

CLI Display

milo@ubuntu:~/documents$

mv test.txt newtest.txt; ls

newtest.txt

Hopefully you are not too attached to this file as in the next table, we will learn how to delete a file, using rm.

Using rm to Delete a File

Command

rm newtest.txt

Result

System will delete newtest.txt

CLI Display

milo@ubuntu:~/documents$

rm newtest.txt

milo@ubuntu:~/documents$

If want you can use ls, but you will find that the directory is empty.

Now let’s go back to our parent directory.

Move to Parent Directory

Command

cd ..

Result

System will move up to parent directory

CLI Display

milo@ubuntu:~/documents$ cd ..

milo@ubuntu:~ $

When deleting files or directories, be very careful as one misstep could delete the entire system. Avoiding using the root user helps with this.

Sometimes you may want to delete a directory, but to delete a directory we must add a flag.

If we use rm documents, we will get an error that it is a directory.

To delete a directory, we must use the command in the table below.

Delete a Directory

Command

rm –r documents

Result

System will delete the directory named documents.

CLI Display

milo@ubuntu:~$ rm –r documents

milo@ubuntu:~$

To delete a directory, we have to use the –r flag.