Humdrum: The Terminal

From CCARH Wiki
Jump to navigation Jump to search

The Humdrum Toolkit is intended to be used in a unix terminal. A terminal is usually considered to be a text-based program with which a user can interact with the computer by giving it commands, and the results generated by the computer can then be displayed in the same terminal.

The Humdrum Toolkit is intended to be used in a Unix-based terminal. Thus, the Humdrum Toolkit can be installed directly on Unixe-related operating systems such as Linux or Apple's OS X. Most people who use Linux will be familiar with the terminal, and access to it will depend on the particular version of Linux being used. For Apple's OS X, there is a program called Terminal.app which is located in the Utilities folder of the Local Apps folder.

For Microsoft Windows, you can download a unix-based terminal called Cygwin. This may be difficult to install for those not experienced with unix/linux.

File-structure commands in the terminal

After you have found a terminal program and figured out how to start it. Try some basic commands which are useful for working with files on your computer. If you are familiar with a File Navigator/Browser/Explorer program you can open up that program in parallel on the desktop display of a graphical interface to the computer. The terminal and the graphical file browser have similar access possibilities.

When you first open the terminal, there should be mostly blank screen which has a prompt. Here is a representative example:

Openterminal.png

Different terminals will have different prompts. In this of the default cygwin style, the prompt has two lines. The first one is craig@quisuk ~ contains three pieces of information: the user's name (craig), the computer's name (quisuk) and the current working directory (~). For those more familiar with graphical environments, a 'directory' is equivalent to a 'folder'. The tilde symbol (~) has a special meaning as a folder name on unix-based computers. It is a shorthand symbol which represents the home directory of the user. Typically when you start a terminal program, you will start in your "home directory". On the second line in the above example terminal is a dollar sign ($). This is indicating that the computer is waiting for you to give it a command to do something.

pwd command

To view the full path name of the home directory, try running your first 'command' in the terminal:

  pwd

this is an abbreviation which stands for 'present working directory', and the command will report where in the file structure your terminal is working from. In the follow illustration you can see that I am working with files in the "/home/craig" directory. Note that the computer prints another prompt which is has finished the command. This indicates that it is again ready for you to give it another command.

Terminalpwd.png


mkdir command

Go to your home directory in the graphical file browser on your computer so that you can watch the results of the next command. Type the command:

  mkdir newfolder

in the terminal. This is an abbreviation which means 'make directory'. In your file browser, you should see a new folder appear which has the named 'newfolder'. Later on this page you will learn how to view the files and directories/folders in the terminal we will do another command first.

Terminalmkdir.png

As an aside, you cannot type:

  mkdir New Folder

in order to create a directory/folder called "New Folder". This is due to how the grammar of commands work in the terminal. What you really mean is the following command:

   mkdir "New Folder"

Making an analogy to natural languages, the command 'mkdir' is like a verb, and "New Folder" is the direct object. If you did not use the quotes, then the computer would think that there were three words on the command line, and 'New' and 'Folder' would be two separate folders. By using quotes around "New Folder" you are telling the computer to treate them as a single word, like a compound word in English. In computer parlance, "New Folder" is called an argument.


cd command

The next thing to do is to move into that new directory/folder. In a graphical browser, you would click or double-click to go into the new folder. The terminal command which is equivalent to this action is called 'cd'. Type the following command to set the present working directory to 'newfolder':

  cd newfolder

and then type the command to display the present working directory:

   pwd

When I do this, the computer replies "/home/craig/newfolder" as illustrated below:

Terminalcd.png

echo command

Now that you know how to create a directory/folder with mkdir, let's try making a file. We will use the echo command to do this. The first thing to try is the echo command alone. Type in the terminal:

  echo This is a sentence.

when you press the return key, you will see the text "This is a sentence." echoed to the terminal, and then the computer waits for you to type another command:

Terminalecho.png

Now we will used the echo command to create file by saving what it echos into a file instead of having it displayed in the terminal. This is called file redirection since the output from the computer will be redirected into a file rather than reported back to the terminal. To do this you type the ">" character after the full command, and then the name of a file. Note that the ">" character looks like an arrow, and it is pointing to where the output from the command should be stored.

   echo This is a sentence. > newfile.txt

In this case a file called newfile.txt will be created, and it will contain the text "This is a sentence.". Nothing will be displayed by the computer in the terminal before it displays a prompt again, since the content which would normally be echoed to the terminal was redirected into newfile.txt.

If you wanted to create an empty file with no contents for some reason, you can type one of the following two commands:

  echo > newfile2.txt
  touch newfile3.txt

The newfile2.txt file will have one newline character in it, while the newfile3.txt file will have no characters in it at all.


cat command

Now, it might be interesting to see what is inside of the file you just created. You can double-click on the file in the graphical file browser and it will probably be opened in some text editor on your computer (try that). In addition, you can see the content of the file by using the cat command in the terminal (with cat be an abbreviation for concatenate):

   cat newfile.txt

This should display the contents of the file in the terminal:

Terminalcat.png

If the file contains a large amount of text, then you should the less command instead of cat. The less command will show one screen-full of text from the file at a time, and you would press the space bar or the down arrow to view more text; and type 'q' to quit from the display of the file.

  less newfile.txt

ls command

Finally, let's try the ls command which stands for directory listing. The ls command returns a list of all files and subdirectories/folders in the present working directory. Type:

  ls

This should result in the name of the newfile.txt file just created to be displayed:

Terminalls.png

As the next exercise, let's try using an option with the ls command. In unix-based terminals, options are letters or strings which are preceded by a dash (-) character. This function similar to adjectives in natural languages. Try using the -a option with ls. The -a option is used to show all files/directories in the current directory, even if they are hidden. Hidden files in unix-based systems are files which start with a dot. These are usually system-related files which are not of general interest, so they are hidden by default. Type:

  ls -a

In this case you should see three items: ".", ".." and "newfile.txt":

Terminallsa.png

The "." entry is a special directory name which means the current directory. The ".." entry is also a special directory name which means the parent directory. If the current working directory is /home/craig/newfolder, then the parent directory is /home/craig. Every directory will contain these two special directories (which are usually also hidden in a graphical file browser).

Sometimes, it is useful to get more information about the files/directories in the present working directory. For example, how would you know if a file is a directory or a file? For example, make a new directory:

  mkdir anotherdir

if you type ls without options, you cannot distinguish between files and directories. In a graphical file browsers, the icon for files and folders are different. Likewise, the ls can display the distinction between them if you use the -F. The -F option is used to display the function of the file or directory. In the case of directories, a slash (/) will be added at the end of the directory name to indicate that it is a directory. Regular files will have no extra marking character displayed after it. If the file is a command, it there will be a star (*) after the filename. There are several other types of marks which will be displayed, but these are mostly esoteric and not interesting for beginning users. Type:

  ls -F
TerminallsF.png

If you want to use two options at once, there are two ways of doing it. In this particular case, both -a and -F options do not have optional parameters. So the two options can be merged into a larger option string. Try typing any of the following commands which are all equivalent:

  ls -a -F
  ls -aF
  ls -F -a
  ls -Fa
Terminallsfa.png

Another useful ls option is -s option which will show the size of the files. The -s option shows the size in blocks which are usually about 1 Kilobyte, but could be 512 bytes or 4 Kilobytes as well. Therefore there is also a -h option which can be used to display the size of the file in a more human-readable form:

  ls -asF
  ls -ashF
Terminallsashf.png


alias command

If you like one of the particular optional variations for ls but do not want to continually type the same command options over and over again, you can create and alias. To do this, a slight complication may occur. Most terminals are of the bash type. However, others are of the csh type, such as tcsh. Terminals are text windows which interface with the computer via a program called a shell. There are numerous shells (the first one being called sh for shell), but they mostly fall into two categories: the bash shell (Bourne-Again shell) which is an elaboration on the original sh shell; the csh shell (C shell -- although it does not really have too much to do with the C programming language). The most common current csh-like shell is tcsh. The bash type of shell is more common, unless you are perhaps at MIT, where the csh shell was created.

To determine what type of shell you are using, you can type the command:

   echo $SHELL

This tells the computer to display the contents of the $SHELL variable. For me, the shell is /bin/bash:

Terminalshell.png

Therefore, I can type this command to create an alias:

  alias lash="ls -ashF"

Then I can type:

  lash

and the shell will translate the alias to "ls -ashF" before running it:

Terminallash.png

If you are running a C shell, such as tcsh, you instead create an alias with this syntax:

  alias lash "ls -ashF"

Note that the alias command is built into the shell, unlike most command which are stored as files in the computer (cd being another exception).


man command

If you want to learn how to use a particular unix command, you should definitely remember to use the the man command, which is an abbreviation for manual. This command will display documentation for nearly all of the commands which you can used in the terminal. For example, the ls command has many options. Most of these options will rarely be needed, so you do not need to memorized all of them. Instead you can just type:

  man ls

To see a list of the options and a description of how to use them with the program:

Terminalman.png

To navigate through the ls manual, press the space bar to continue to the next page of the manual. Press 'q' to quit from the manual. The manual (at least on my computer) is displayed with the more command which is similar to the less command described above. The more command was the first muti-page display program which displays a file's contents one page at a time. The less program has additional improvements which allow you to scroll backwards in the file contents.

Try running the man program to find more information about other commands discussed on this page:

  man pwd  
  man mkdir  
  man cd  
  man echo  
  man cat  
  man less
  man alias  
  man man



Back to the Humdrum lab page.