Basic Linux Terminal Commands

Practice basic Linux commands by doing the following: Open a command shell and cd to your home directory, like so:

cd ~

Use the pwd command to show the path to the folder you are currently working in:

pwd

Use the mkdir command to create a folder named testFolder:

mkdir testFolder

Change directory into the folder you just created:

cd testFolder

Use the touch command to create a new file called sample.txt in the testFolder:

touch sample.txt

Use the nano editor to add some lines of text to the sample.txt file:

nano sample.txt

Notice that when using nano, you can write the contents into the file by typing into the terminal. Add a few lines, you can enter whatever you want. When you are done press ctrl+x to exit and then press 'y' to save your changes.

Here's a trick that we'll use later... in nano you can use ctrl+w to search for text within a file (maybe you should re-open the sample.txt file with nano and try it out). When you are done messing around, save the file and exit by entering ctrl+x.

Next, use the mv command to move the file you just edited to your desktop:

mv sample.txt ~/Desktop/sample.txt

Change directories to your desktop:

cd ~/Desktop

Note: in the linux terminal, if a path starts with ~ (tilde) it means the path starts with the current users path to their home directory. For example, if you were logged in as bob, then the tilde would be short for this path: /home/bob/.

Use ls to list the contents of your desktop folder, you should see the file you just moved:

ls -al

Note that the a option/switch will include hidden files ('all' files) and the l option will display the output in a list that includes additional information. Most of this additional information is about how the permissions are set for each file and folder listed. We won't get super deep into permissions, but just keep in mind that they are critically important, especially for live servers. Many apps fail to work because a permission for a file or folder is not set properly. Permissions are also vitally important in securing apps and the the data that they work with. If you want to have a discussion about permissions in Linux, please let me know in class.

The rm command removes a folder or file. To remove the testFolder that is in your home directory:

rm -rf ~/testFolder

WARNING - You must be very careful when deleting files and folders because you may not be able to recover them in linux.

In order to run some commands in linux, you must do so by using the administrater's account (you may be familiar with 'run as adminsistrator...'' in Windows). To run a terminal command as admin, you prefix the command with sudo.

In order to install the latest updates on your Ubuntu machine, you must use sudo before running the command that starts the update.

Uyse the apt command to run a system update (you must use sudo to run this command 'as admin').

This command will fetch all the latest updates from the Ubuntu repository:

sudo apt update

You'll be prompted to enter the root password before the command executes.

Now that your system knows about the latest updates that are available, you can run this command to install of them (again, sudo is required):

sudo apt-get upgrade

This may take a while, if you get bored take a look at some of the text that scrolls past the screen.

Practice using the terminal