Using the Terminal

This course relies heavily on unix-style command terminal. All instructions and examples use the bash terminal, which is available in every Linux distribution, OS X, and on Windows using Cygwin or MinGW.

Your first step is to install and/or open a terminal application for your computer. Here are some resources to help on various platforms:

Accessing a Remote Server with ssh

In this course we have one server for remote access:

You should receive a username and password in Canvas. Your username is derived using letters from your human name, followed by a random number. Your password is a random string.

You can also use your A-number and campus password to access the ECE Linux Lab computers:

To access these computers, you first need to activate USU’s Virtual Private Network (VPN). Follow instructions at this link.

Once you are logged into the VPN, launch your terminal application and run the ssh command:

ssh <username>@<servername>

In this tutorial, we will use a dummy account named na480 and a FAKE server named rando. You should replace <username> with your own username. In place of rando, you can login to miranda or one of the lab computers, or you can work on-site in the Linux Lab in EL103.

Basic Terminal Features

Command prompt

The terminal is a text interface. When launched, a window appears containing a command prompt. A typical prompt looks like this:

[na480@miranda ~]$

In this prompt, na480 is the username, miranda is the machine’s name, and ~ is the working directory. In most unix/linux terminals, ~ is a shorthand for the user’s HOME DIRECTORY.

Following the context information, the $ symbol indicates the beginning of your command input.

For more information, you can customize the bash prompt as described in this How-To Geek article.

Tab completion

When typing a long command or filename, you can enter the first few letters, then type <tab> to automatically complete the name. If there are multiple completiong, type <tab> twice to see a list of matching names. If there are no matches, the list will be empty.

Command history

To access a previously used command, use the “up” and “down” arrows to navigate through the command history. You can retrieve a previous command and edit it before running again. This is especially useful if your command fails due to a typo, you can retrieve it from the history, fix the typo, and try again.

You can obtain a full command history by using the history command. This is useful if you want to remember what you did during a terminal session, perhaps save it as a session log for future reference.

Useful hotkeys

Terminals vary in their hotkey support, but most support these basic key combinations:

Depending on your personal terminal application, you may need to find a Preference setting like “Send Alt as Meta” to make these work properly. Experiment with your terminal to figure out which combinations work for your setup, as these are extremely useful hotkeys.

Filesystem Commands

Commands described in this section:

Listings and Directories

Linux filesystems are organized in directories, another word for folders. The ls command performs a file listing:

[na480@miranda ~]$ ls
perl5/

In this example, the user types ls, then Enter. The command responds with a file listing (there is only one file, a sub-directory named perl5).

A more complete listing is obtained with ls -al:

[na480@miranda ~]$ ls -al
total 28
drwx------.  7 na480 na480  231 Jan  8 15:09 .
drwxr-xr-x. 65 root  root  4096 Jan  8 15:09 ..
-rw-------.  1 na480 na480    5 Jan  8 15:09 .bash_history
-rw-r--r--.  1 na480 na480   18 Mar 31  2020 .bash_logout
-rw-r--r--.  1 na480 na480  193 Mar 31  2020 .bash_profile
-rw-r--r--.  1 na480 na480  231 Mar 31  2020 .bashrc
drwxrwxr-x.  3 na480 na480   26 Jan  8 15:09 .cache
drwxrwxr-x.  3 na480 na480   26 Jan  8 15:09 .config
-rw-r--r--.  1 na480 na480  334 Nov 27  2019 .emacs
-rw-r--r--.  1 na480 na480  172 Mar 31  2020 .kshrc
drwxrwxr-x.  3 na480 na480   27 Jan  8 15:09 .local
drwxr-xr-x.  4 na480 na480   51 Nov  3 18:05 .mozilla
drwxrwxr-x.  2 na480 na480   10 Jan  8 15:09 perl5

The string -al is referred to as a command argument, and the letters a and l are called flags. Flag a means “list all files, including hidden ones”. In Unix/Linux, hidden files start with a dot (period), as in .bashrc. Flag l means “long format,” revealing detailed information about each file.

The long-format has several columns. For more information about the ls command and its output, please read this explanation from How-To Geek.

Creating and Navigating Directories

Create a directory with the mkdir command:

mkdir some_directory

Notice that this directory name contains no spaces or special characters. This is important for *nix files, since command arguments are separated by spaces, and special characters like &, -, ^, $, ~, etc often have reserved meanings in the bash shell. Consequently it is advised to use only letters, numbers, and underscores (_) in your filenames.

Now verify your directory by running ls with no arguments. You should see it appear in the file list, like this:

[na480@miranda ~]$ ls 
perl5  some_directory

Next, change to the directory using the cd command:

cd some_directory

Now do a directory listing using ls -al. You should see two files, ./ and ../. There are no other files because you just now made the directory.

Next, use the pwd command to figure out where you are. It stands for “present working directory”:

pwd

On miranda, the result is: /home/na480/some_directory

Now let’s go back to the home directory. There are two ways to do this. The first option is to go “up” one directory:

cd ..

If I then run pwd, the result is /home/na480

Another option is to run cd with no arguments:

cd

Using cd with no arguments always returns you to your home directory.

Now let’s delete some_directory/ using the rmdir command:

rmdir some_directory

Now do ls again, and you should see that some_directory is gone.

Working with Files

Now create some_directory again using mkdir, and navigate into it using cd some_directory where we will create some files.

Quick text file using cat

Create a text file named my_file.txt by running

cat > my_file.txt

After you press Enter, the terminal will direct any text you type into the named file. Type a few lines of text. When done, press Ctrl-D to close the file. The cat command is very limited, it is not a text editor; it just dumps whatever you type into the file.

The > character is called a redirect operator in bash. It redirects bytes from the keyboard into the named file. To append more text to the end of your file, use a double-redirect >> like this:

cat >> my_file.txt

Type a few more lines, then press Ctrl-D.

You can also use cat without any redirect operator to dump the contents of a text file:

cat my_file.txt

This dumps all the file text into the terminal, without any pauses. For large files, this is usually not the most convenent text reader, but it works well for very short files.

Reading text with less

To read a longer text file, you can use the less command:

less my_file.txt

Within less, you can use the arrow keys to move up and down in the file, press space to page down, > to skip to the end, < to jump to the beginning, and q to quit.

Commands like less are called terminal pagers, since they organize long text into pages that can be browsed within the terminal. There are several pager applications, such as an older pager named more, and a more advanced pager named most. The most command is installed on the miranda server, but is not yet universal in linux distributions.

Editing text in the terminal: nano, micro, tilde, ne, emacs, and vim

There are lots of terminal-based text editors in linux distributions. One of the simplest is nano. Use it to edit your text file:

nano my_file.txt

This initiates a simple word processor within the terminal. Edit your text as desired. Some Ctrl commands are listed along the bottom. Type Ctrl-O to save the file, and Ctrl-X to exit the editor.

More advanced text editors are micro, tilde, and ne. Most beginning students will probably find tilde the most intuitive editor. Try it:

tilde my_file.txt

Within tilde, you can access the top menu items by using Alt- key combinations, e.g. Alt-F for the File menu. There are also a number of hot-key combinations like Ctrl-S to save and Ctrl-Q to quit.

Power users may prefer to use an advanced editor like emacs or vim. These tools can be considered terminal-based Integrated Development Environments (IDEs). Some terminal tools use vim by default to receive user input, so we will review a couple of basic commands.

Run vim to edit your text file:

vim my_file.txt

This initiates the vim editor. By default, vim opens in a read-only command mode, so you can’t make accidental edits. To modify text, press i. A status bar at the bottom of the window changes to -- INSERT --, indicating that you are now in insert (write) mode. Edit some text, then press ESC to return to read mode. The status bar message disappears.

To save your file in vim in command mode, type the sequence :w and press Enter. To quit vim, type the sequence :q and press Enter.

Most power Linux users work extensively with either vim or emacs. I personally use emacs for virtually everything. Both these editors are loaded with tools, macros, hotkeys, and customizable features. For more information, try one of these tutorials:

Copy a file with cp

Now make a copy of your text file using the cp command:

cp my_file.txt a_different_file.txt

This creates an exact duplicate of the file.

Compare text files with diff

Open the new copy a_different_file.txt in a text editor and add a couple of new lines, then save and exit from the editor. To compare the original file with its modified copy, use diff like this:

diff my_file.txt a_different_file.txt

The output looks like this:

7a8
> I just added this line using the text editor.

The first line gives a location code 7a8, indicating that the change began at line 7 in my_file.txt; the letter a indicates that lines were added, and the change continues until line 8 in a_different_file.txt.

The diff command is fairly important since we will use git extensively, and git uses diff to track and report file changes. In fact, most development tools use diff extensively to track code revisions.

For more information and instructions on using diff, spend some time with a diff tutorial like this one or this one.

Remove a file with rm

To delete a file, use the rm command:

rm a_different_file.txt

After removing the file, run ls to verify that it’s gone.

Search files with find

Eventually you will have many directories. To find a file within your directory tree, use the find command like this:

find ~ -name my_file.txt

This requests a search, beginning from your home directory ~, to locate all files named my_file.txt. The result:

/home/na480/some_directory/my_file.txt

You can also use wildcard search with * like this:

find ~ -name "*.txt"

This will find all of your files that end in .txt. In our example, the user has only one .txt file, so the previous result is repeated.

Rename a file with mv

Let’s give some_directory a more descriptive name. Run the commands below to navigate to your home directory, rename some_directory to terminal_basics, then list the directory:

cd
mv some_directory terminal_basics
ls

Now the listing looks something like this:

[na480@miranda ~]$ ls
perl5  terminal_basics

Working with Archives

A collection of files is often archived into a ZIP, GZIP, TAR, or similar type of file. In the linux world, tar and gzip are most frequently used, but zip is also available.

Compress/extract with zip and unzip

To make a zip archive of your terminal_basics directory, run

zip -r terminal_basics.zip terminal_basics/

The output should list all files added to the archive:

updating: terminal_basics/ (stored 0%)
updating: terminal_basics/my_file.txt (deflated 23%)

To extract the archive, run

unzip terminal_basics.zip

The command warns that a file will be overwritten. In this example, I said y:

Archive:  terminal_basics.zip
replace terminal_basics/my_file.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename: y
  inflating: terminal_basics/my_file.txt

Compress/extract with tar and gzip

In the *nix world, the combo of tar and gzip is standard instead of zip. To make a tar.gz archive, you run:

tar -czf terminal_basics.tar.gz terminal_basics/

The tar command creates a file system archive. Three flags are given:

After the -czf flags, the target filename is given, followed by a list of files to be compressed in the archive.

To **extract* the archive, run

tar -xzf terminal_basics.tar.gz

Here the three flags are:

By default, the tar command doesn’t warn about overwriting extracted files. There are a couple of flags that control overwrite behavior.

The -w flag causes tar to pause to ask about every file:

tar -wxzf terminal_basics.tar.gz

The --skip-old-files argument causes tar to assume “no” without asking or printing any messages.

tar --skip-old-files -xzf terminal_basics.tar.gz

The -v flag causes tar to print everything it’s doing. These flags and arguments can be combined as needed for a particular situation:

tar -xvzf terminal_basics.tar.gz
tar -xvwzf terminal_basics.tar.gz
tar --skip-old-files -xvzf terminal_basics.tar.gz