Terminal

Table of Contents

1. Introduction to the Terminal

In the early days of computing computers were large devices that were maintained centrally. An early innovation that greatly increased their usage was to permit multiple users to talk to the mainframe at the same time. The hubs used for this communication were called "terminals", and the venacular has been retained even though the implementations are very different. Today a terminal refers to software that simulates that interface and lets you speak to your computer in the same direct way via a command line interface. Graphical User Interfaces (GUIs) seem convenient in the beginning as you can visually parse them and make a reasonable guess how it is to do what you want done, but once you move beyond one off tasks and adjustments you will likely find the keyboard a much more efficient way of interacting with your laptop. It also permits you to write small bits of code that lets you script actions. For example, a few lines of a shell script would permit you to resize and rename all the files in a directory that were .jpgs. For example, if you wanted to create a directory of thumbnail images to accompany all your thousands of high resolution photos. Imagine doing that one at a time by pointing and clicking. In this module we will get some experience what a terminal is and some very basic usage. Hopefully you will continue to explore the terminal as the course progresses by looking for ways to do what I suggest via the terminal interface.

2. Find the terminal

Da : Terminal Introduction (i2c4p) from Britt Anderson on Vimeo.

A brief introduction to the terminal material.

Different operating systems refer to the terminal differently. In Windows the CMD.exe 1 command is an approximation to a terminal as is the Power Shell 2.

For OSX you navigate to you applications, find the folder "Utilities" and look in their for the terminal application.

For Xubuntu click the icon in the upper left of the screen and type terminal in the search bar.

2.1. Terminology

While they do not mean exactly the same thing, you will often find the following terms being used relatively interchangeably.

  • terminal
  • shell
  • command line

What they have in common is the idea of a text based interface to communicating with the operating system. What this means is that instead of opening a gui (gui: G raphical U ser I nterface) to navigate your file tree you do this with a text based system of commands.

2.2. An example - your directory tree

ls

Typing this command in your terminal will list the files and directory.

ls -la

Typing this command will get you a l ong version of the same list, plus all the hidden files that can be hard to see from a conventional file manager display.

2.3. An Historical Aside

In the early days of computing people wrote their programs on punch cards. Some see the inspiration as the Jacquard machine. There are still programmers alive who can tell you their horror stories of tripping and falling and scattering their punch cards everywhere. Want to know if your program worked? Take it to the main frame data center, drop it off, and come back the next day to get your print out. Terminals came along as an alternative to communicating with big central processors. There was a screen and keyboard. By typing you could send input to the computer that returned the output to your screen. The terminals we have today are not true terminals, but emulators. Though few people refer to them as such. We emulate this old way of communicating to the processor because it works and is efficient.

3. Why use a terminal?

You can get more done. You can get it done more quickly. Once you learn to do one hard thing you never have to figure out how to do it again, because you can easily script it. That is why you want to learn to use the terminal

Terminals are ubiquitous. They are low in their resource usage. They permit remote logins without the need for sending graphics back and forth. In fact, the remote computer need not have a system installed for displaying windows or even a physical screen attached (called headless).

Knowing how to use a terminal will allow you to use ssh to connect to remote hosts. It will allow you to quickly and efficiently navigate your system, and it will make it easy for you to do things that used to take ages.

3.1. A scripting example

Want to convert and compress a large directory of videos as I did for this course. No need to open up each in an application and click a bunch of mouse clicks. Just write a bash script to invoke a command line program to do all the work for you. Go get a cup of coffee and come back when the job is done.

for i in *.MP4; 
do name=`echo "$i" | cut -d'.' -f1`; 
echo "$name"; 
ffmpeg -i "$i" -c:v libx264 -b:v 1.5M -c:a aac -b:a 128k "${name}S.mp4";
done

Almost all of this I copied off the Internet where it appeared as an answer to a question from someone else wanting to do essentially the same thing I did. It took me a while to tweak it to my particular use case, but when that was done my problem was solved, forever. Every new batch of videos I just put in their own directory and run this script from the command line. Note that this script uses a for loop, one of the constructs introduced in the Loops section of the chapter introducing programming concepts.

What learning the terminal does is allow you to leverage the knowledge of more people. Thus you get to think about the interesting part of your problems.

4. Resources

There are a great many resources on how to use the terminal effectively, but don't go out and read them all. One of the skills to learn in learning to use the computer is to develop your own set of links and resources you can go to when need arises. Don't try to learn everything at once. You will get overwhelmed and discouraged. Instead you learn what you need when the need arises. And if you need to know something more than once, then you spend the time to dive deeper. There are a great many things about using the terminal that I do not know, but I know the ones I use often, and I know where to find more when I need to know more. You should do the same. Here are a few online resources to get you started.

  1. The command line
  2. A Short Series of Terminal Lessons from the UbuntuWiki
  3. Some Scripting Basics Most of what you want to do at the command line, at least in the beginning, you can do with typing directly into the terminal. But at some point you will want to write a file, a script, that has all the commands typed into it. They you can run that script from the terminal. This blog post has some basic background for how to get started. Note the use of the term "BASH". This stands for the Bourne Again Shell. Your terminal can use different shells. So while you can use the terms interchangeably most of the time, they don't mean exactly the same thing. The default shell in use by the terminals in Xubuntu is BASH. Another shell favored by many advanced programmers is zsh. You can change the shell for your terminal if you like.
  4. Another Scripting Introduction
  5. A More Detailed Treatment of Shell Scripting

5. Terminal Games

Db : Terminal Games (i2c4p) from Britt Anderson on Vimeo.

I demonstrate in this video some of the exercises under the heading of the Terminal Games in the topic file for this segment.

In order to give you some structure for your self-directed learning, I have organized a few activities you might try and a few questions you might ask yourself.

  1. ls -la /home/<username>
    • What does all this output mean?
    • What changes when you leave out the -la?
    • What does the hyphen do?
  2. Can you find the location of your desktop folder in your terminal?
  3. Can you change to that directory? cd
  4. Find out where you are? pwd
  5. Can you find out who the computer thinks you are, your user name? whoami
  6. Find out how much free space you have on your computer disk. df -h
  7. How do you get help for most of these commands? Usually command --help or (-h)
  8. How do you find the manual? man ls
  9. Navigating
    1. Paths: absolute and relative.
    2. What do those "dots" mean?
    3. What do those slashes mean?
    4. Tab is your friend.
    5. Try the up arrow too.
  10. File ownership
    1. Make a text file from the command line. touch /home/yourname/Documents/testText.txt
    2. Who owns it?
  11. Make a directory mkdir /home/britt/Documents/myFirstDir/ Spaces are the enemy. Never use them, but if you have to, escape (\) them.
  12. Want more practice? Try some of the resources above.

6. Assessment: Terminal Assessment

Dc : Terminal Assessment Explained (i2c4p) from Britt Anderson on Vimeo.

What do I recommend you do in this exercise and how do you get started on writing your first shell script.

6.1. Goal

Demonstrate basic knowledge about and usage of the terminal (aka: command line or shell).

6.2. Tasks

Note This assessment has two parts and asks for two separate files.

6.2.1. Submit a file that answers the following questions:

  1. What is a terminal?
  2. What shell is your version of Xubuntu using?
  3. What is one other shell you might use?
  4. Give two reasons (not more) why one might want to use the terminal.
  5. I mentioned ssh. What does ssh stand for and how does it relate to the concept of a shell?
  1. File Requirements
    1. Make sure it has your name and student number on it.
    2. Make sure it is in .org format. To do this you will want to,
      1. open Emacs.
      2. open a new file
      3. save as (you can try C-x C-s) with a suitable name and the extension ".org".
      4. Give each question its own heading (start the line with a single "*" and a following space).
      5. Put your answer underneath this heading.
      6. Save.
      7. Upload to the "Terminal" dropbox on Learn.

6.2.2. Submit a file that does the following:

  1. Can be run as a shell script.
  2. Echos your name to the terminal.
  3. Prints out the current directory.
  4. Name this file as <yourLastName>_<yourStudentNumber>.sh
  5. Upload to the "Terminal" dropbox on Learn.

6.2.3. Last Thoughts

Enjoy the power the terminal provides, but don't go overboard. https://xkcd.com/196/

(video: assessment) terminal.org

Footnotes:

Date: 2023-02-14 Tue 00:00

Author: Britt Anderson

Created: 2023-04-23 Sun 11:53

Validate