Before you attempt this activity, it is best that you review lecture video two from week one. You can find the video and slides on the module page.
For this activity, you will be using the Terminal window in the Linux environment. A shortcut to open the terminal window is Ctrl+Alt+T. During this lab activity, you will be presented with various Linux commands to enter into the terminal console. These will be shown in the following format:
$ command
The dollar ($) symbol part of the command should not be typed in. This is included to identify that this type of command is to be executed in a shell/terminal window. For example, if you are presented with:
$ mkdir 4061CEM
In the terminal window you shall enter mkdir 4061CEM without the dollar symbol. In some cases, you may be presented with a command, and then an output directly below it. This may look something like:
$ pwd
/home/ian
In this instance, the pwd is the command that was entered into the terminal window and the /home/ian below (and indented) is the outcome of the command. In most cases, the outcome that follows a command may not match up to the result that you achieve; this is to be expected.
When you open a fresh terminal window, you will be located in your home directory. To determine the path of your home directory, you can use the following command:
$ pwd
The outcome of this would be something like this:
$ pwd
/home/ian
Where the name, in this instance ian at the end would be the username of the account you are currently signed in to. The home directory is sometimes known as the working directory (hence pwd, where p is print). The working directory is where all your personal files are stored. You will use this folder for completing all tasks in this guide.
Sometimes it is useful to understand the contents of a folder you are in. when you first open a terminal window you will be located in your working directory. To determine/understand the contents of the files/directories that may be in this file you can use the following command:
$ ls
Desktop Downloads Music Public Templates
Documents IdeaProjects Pictures snap Videos
This command will list all the directories and files for the current working directory. It will not show the hidden files by default. Hidden files on Unix-based systems will have file names that begin with a dot (.). To list all files in the working directory, including the hidden files, you can use the -a flag/option with the ls command:
$ ls -a
. .cache .gnupg .mozilla Public
.. .config IdeaProjects Music snap
.android Desktop .ipython .npm .ssh
.bash_history Documents .java Pictures .sudo_as_admin_successful
.bash_logout Downloads .jupyter .pki Templates
.bashrc .gitconfig .local .profile Videos
To make a directory called 4061CEM inside your current working directory, type the following command:
$ mkdir 4061CEM
You can remember this command by the mk being the abbreviation for make and then dir being an abbreviation for directory. To verify you have created the directory, you can use the list command:
$ ls
This will then list the newly created directory alongside the others in the current working directory.
$ mkdir 4061CEM
$ ls
4061CEM Documents IdeaProjects Pictures snap Videos
Desktop Downloads Music Public Templates
The command cd enables you to change the directory. However, there are a few different use-cases to how this command can be used.
To change to a new directory, you can use the command cd followed by the name of the directory. For example:
$ cd 4061CEM
This will traverse you to the directory you created earlier (4061CEM).
To return to a previous directory, you can use the following command:
$ cd ..
Note the .. in this command. This will traverse you back a single directory. For example, if you were in the following path:
/home/ian/4061CEM
Then using the command cd .. will take you back a single directory, in this instance it will be:
/home/ian
However, you can traverse many directories at once. If you were to use the following command:
$ cd ../../
and you were located in /home/ian/4061CEM you will end up in:
/home
To return directly to your home directory, regardless how deep into the filesystem you are, you can use the following command:
$ cd
or
$ cd ~
The tilde symbol (~) in Unix-based systems represents your home directory.
For the purpose of this activity, it is recommended that you are located in the 4061CEM directory that you created earlier. You can traverse to this directory using the following command:
$ cd ~/4061CEM
There are various methods for creating a file on a Unix-based system. For the purpose of this module, you will learn two commands cat and touch.
The cat command is short for concatenate. It can be used for viewing a file, but it can also be used to create a file. In this use-case you are concerned with the creation of a file. To create a file, you can use the following command:
$ cat > hello_world_file
This will create the file called file1 but you will be stuck in the terminal command and unable to execute other commands. This is because with this command it will create a file and then let you insert some data/content into the file. For the purpose of this example, enter the following text:
Hello World
After entering the sentence push Ctrl+C on the keyboard. This will break you out of the command and save the contents to the file.
The touch command is often used to create, change or modify the timestamp of a file; but it can also be used to create a new file. The file that is created using this command will be empty, meaning that there is no content inside it; useful if you do not need to store any content in the file at the time of creation. Creating a file is achieved by the following command:
$ touch empty_file
This will create a file called file1 without any contents.
For the purpose of this activity, it is recommended that you are located in the 4061CEM directory that you created earlier. You can traverse to this directory using the following command:
$ cd ~/4061CEM
Inside this directory, you are required to create a new file called file1. It is not important that this file contains any content. Once you have created this file, you can create a copy using the cp command:
$ cp file1 file2
The command above will create a copy of file1 with a new name, called file2. This will be a direct copy of the file, so if the file were to contain any content such as, Hello World, then this would also be copied across.
You can also use relative or absolute paths to identify the files. For example, if you were to copy file1 from the current working directory and save it as file2 in a directory a level-up from where you are, then you can use the following command:
$ cp file1 ../file2
This will copy the file from /home/ian/4061CEM/ to /home/ian/. However, you can also copy the file to a completely different folder if you wanted to. This can be done with the following command:
$ sudo cp file1 /opt/file2
This will copy the file file1 to an admin-only access folder /opt and called file2.
When you need to perform some sort of admin-only command, i.e. copying to the /opt folder, then you must prefix the command with sudo. This will raise the command to admin privileges and require you to enter your account password.
This same process can also be applied to directories. For example, traverse to your home directory using the command:
$ cd ~
This should take you to /home/
$ cp -r 4061CEM 4061CEM_copy
This will create a copy of the directory and rename it as 4061CEM_copy. You may notice the -r flag/option after the command cp. This stands for recursive and essentially, it means that the files within the folder are copied alongside with any directories that may be within it too.
For the purpose of this activity, it is recommended that you are located in the 4061CEM directory that you created earlier. You can traverse to this directory using the following command:
$ cd ~/4061CEM
Inside this directory, you should have now created a few files and also have a copy of this directory called 4061CEM_copy. Now, you are going to move some of these files; but before you proceed with that there is one useful feature you can perform with the command mv. Not only does it move files, but it can also rename files or folders.
For example, you have a file called file1 in your current working directory. You can rename this file by using the following command:
$ mv file1 renamed_file
This will change the name of file1 to renamed_file; however, it has not actually renamed this file; essentially, it has moved the file from its location in the folder back to itself, but under a different name.
Ultimately, the purpose of the mv command is to move a file or folder from one directory, to another. Using the file you have just renamed, lets move it to 4061CEM_copy. This can be achieved with the following command:
$ mv renamed_file ../4061CEM_copy
As you can see from the command above, the .. prefix you were introduced in the section on changing directories was used. What this command does is it moves the file renamed_file to the directory 4061CEM_copy that is inside the home directory. You could also achieve this using the following command:
$ mv renamed_file ~/4061CEM_copy
Moving directories can also be achieved in a similar fashion. Instead of using a file name, you can just use the name of the directory instead. For example:
$ sudo mv 4061CEM_copy ../
This will move the file from its current directory (/home/ian/) to /home/. Once again, the sudo command was used here as the folder is being copied to an admin-only access area (/home/).
For the purpose of this activity, it is recommended that you are located in the 4061CEM directory that you created earlier. You can traverse to this directory using the following command:
$ cd ~/4061CEM
Inside this directory will reside a lot of files you have created whilst following this tutorial. You shall now begin to tidy up this folder using a command that can delete files and/or folders.
To delete a file, it can be achieved using the rm command. For example, if you were to delete the file file1, you would use the command:
$ rm file1
However, if you had multiple files that consisted of the prefix file i.e. file1, file2, file3, ... file_n
then you can use a wildcard, otherwise known as an asterisk (*
) to delete all these files. For example:
$ rm file*
The same command (rm) can also be used to delete directories, with an additional flag/option: -r. You were introduced to this flag when copying folders earlier, and essentially the -r stands for recursive. Therefore, it will delete the folder and all files inside the folder.
An alternative method to deleting a directory is using the rmdir command. For example, if you wanted to remove the 4061CEM_copy folder you can use the following command:
$ rmdir 4061CEM_copy
However, this will only remove an empty directory. Any files that are located in the directory are required to be deleted beforehand.
Any files you delete using these commands are not recoverable. Unlike a desktop interface, they do not go to a rubbish bin. Instead, they are removed completely from the file system. You will not be questioned on your choice, i.e. Are you sure?. There is also no command to undo this command if you deleted something accidentally. Therefore, if you are suspect about deleting something accidentally, then you should use the -i flag/option on the rm command.
For the purpose of this activity, it is recommended that you are located in the 4061CEM directory that you created earlier. You can traverse to this directory using the following command:
$ cd ~/4061CEM
There are a variety of text-editors that you can use in the terminal window, i.e. vi, emacs, and nano. For the purpose of this module you will be introduced to vi and nano.
vi
Using the vi command you can create a document called latin_text by using the following command:
$ vi latin_text
Note, that a file has been created without a file extension. You may add the extension .txt to the end of the filename; otherwise Unix-based systems will treat latin_text as a text file. Upon executing this command, you will be diverted to an empty terminal window, with some menu options at the bottom. Inside this window, you can copy and paste the following text:
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed vestibulum semper eleifend. Phasellus mollis mauris quis gravida tincidunt. Fusce sed neque sit amet urna tincidunt imperdiet sed pulvinar leo. Aenean posuere urna a tortor ultrices, eu finibus eros sagittis. Morbi pharetra quam quis aliquam aliquet. Cras tempus faucibus pretium. Duis in accumsan justo. Pellentesque hendrerit nisi mollis justo sagittis, facilisis euismod lorem pharetra. Etiam sit amet pharetra urna. Sed est ligula, tempor eget efficitur eget, rhoncus in nunc. Maecenas sit amet accumsan felis.
Once you have inserted the text, you can save the file by typing :x and pushing Enter on the keyboard. This will then save the file to the system.
nano
Using the nano command you can create either an untitled document, or a document with a name. Solely using nano would create an untitled document. Whereas to create a document called latin_text you can use the following command:
$ nano latin_text
Note, that a file has been created without a file extension. You may add the extension .txt to the end of the filename; otherwise Unix-based systems will treat latin_text as a text file. Upon executing this command, you will be diverted to an empty terminal window, with some menu options at the bottom. Inside this window, you can copy and paste the following text:
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed vestibulum semper eleifend. Phasellus mollis mauris quis gravida tincidunt. Fusce sed neque sit amet urna tincidunt imperdiet sed pulvinar leo. Aenean posuere urna a tortor ultrices, eu finibus eros sagittis. Morbi pharetra quam quis aliquam aliquet. Cras tempus faucibus pretium. Duis in accumsan justo. Pellentesque hendrerit nisi mollis justo sagittis, facilisis euismod lorem pharetra. Etiam sit amet pharetra urna. Sed est ligula, tempor eget efficitur eget, rhoncus in nunc. Maecenas sit amet accumsan felis.
Once you have inserted the text, you can save the file by using the following key combination: Ctrl+X. This will then prompt you whether you would want to save the file. Note, that if you have created an untitled document, then it will ask you for a name. When prompted whether you want to save the file, enter Y and then push Enter on the keyboard. This will then save the file to the system.
For the purpose of this activity, it is recommended that you are located in the 4061CEM directory that you created earlier. You can traverse to this directory using the following command:
$ cd ~/4061CEM
Viewing the contents of a text file can be done using the less command. The command will output the contents of a file to the terminal window, one page at a time. To view the contents of the latin_text file you created earlier you can use the following command:
$ less latin_text
This will display the contents of the file inside the terminal window. To navigate through the file you can use the following keyboard shortcuts:
There may be a command you are unfamiliar with and Unix-based systems have a useful command that will help you read the manual/documentation of a command. The command in question is man; using this command in conjunction with another will show the manual/documentation. For example, to view the manual of the python3 interpreter you can use the following command:
$ man python3
This will display the manual/documentation inside the terminal window. To navigate through the file you can use the following keyboard shortcuts: