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:
Konsole or Terminal or Terminal Emulator from the Application
Menu (there are several different terminal programs with slightly
different features, any will do).sshIn this course we have two main servers for remote access:
genesys.bluezone.usu.edurando.bluezone.usu.eduleft.engr.usu.eduYou should receive a username and password via email. Your username is derived using letters from your human name, followed by a random number. Your password is a random string.
To access any of these servers, you first need to be
on-campus or to activate USU’s Virtual Private Network (VPN).
USU currently uses the GlobalProtect VPN service with gateway address
gp.usu.edu.
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 the rando server. You should replace <username>
with your own username, and replace rando with a random choice between the
three server options.
The terminal is a text interface. When launched, a window appears containing a command prompt. A typical prompt looks like this:
[na480@rando ~]$In this prompt, na480 is the
username, rando 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.
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.
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.
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.
Commands described in this section:
ls – list directory
ls -a – list
all files, including hiddenls -l – list
files with long formatmkdir <dirname>
– make a new directorycd <dirname>
– change director
cd – change to home
directory ~cd .. – change to parent
directoryrmdir <dirname>
– remove (empty) directoryLinux filesystems are organized in directories,
another word for folders. The ls command performs a file
listing:
[na480@rando ~]$ ls
gitclones/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 gitclones).
A more complete listing is obtained with ls -al:
[na480@rando ~]$ 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
drwxrwxr-x. 2 na480 na480 10 Jan 8 15:09 gitclonesThe string -al is called
an argument string, and the letters a and l are called
arguments, options, or
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.
Create a directory with the mkdir command:
mkdir some_directoryNotice 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@rando ~]$ ls
gitclones some_directoryNext, change to the directory using the cd command:
cd some_directoryNow 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”:
pwdOn rando, 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:
cdUsing cd with no
arguments always returns you to your home directory.
Now let’s delete some_directory/
using the rmdir
command:
rmdir some_directoryNow do ls again, and you
should see that some_directory
is gone.
Now create some_directory
again using mkdir, and navigate
into it using cd some_directory
where we will create some files.
catCreate a text file named my_file.txt by running
cat > my_file.txtAfter you press Enter, the terminal will direct any text you type
into the named file. Type a few lines of text. When done, press
C-d (i.e. the Control key and the ‘d’ key together) 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.txtType a few more lines, then press C-d.
You can also use cat without
any redirect operator to dump the contents of a text file:
cat my_file.txtThis dumps all the file’s 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.
lessTo read a longer text file, you can use the less command:
less my_file.txtWithin 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 genesys and rando servers, but is not yet
universal in linux distributions.
nano, emacs, and vimThere 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.txtThis initiates a simple word processor within the terminal. Edit your text as desired. Some Ctrl commands are listed along the bottom. Type C-o to save the file, and C-x to exit the editor.
More advanced text editors are emacs and vim. Some terminal tools use vim to receive user input, so we will
review a couple of basic commands.
Run vim to edit your text
file:
vim my_file.txtThis 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:
There are several other text editors with menu interfaces that may be easier to learn:
micro – a bigger nanone the Nice Editor – a bigger
microfresh – many functions, simple
interfaceAll these editors are installed on the servers and you can try them
out and see what suits you. The fresh editor is probably the
“freshest” in terms of fashionable features.
cpNow make a copy of your text file using the cp command:
cp my_file.txt a_different_file.txtThis creates an exact duplicate of the file.
diffOpen 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.txtThe 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.
rmTo delete a file, use the rm
command:
rm a_different_file.txtAfter removing the file, run ls to verify that it’s gone.
findEventually you will have many directories. To find a file within your
directory tree, use the find
command like this:
find ~ -name my_file.txtThis requests a search, beginning from your home directory ~, to locate
all files named my_file.txt. The
result:
/home/na480/some_directory/my_file.txtYou 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.
mvLet’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
lsNow the listing looks something like this:
[na480@rando ~]$ ls
gitclones terminal_basicsA 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.
zip and unzipTo 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.zipThe 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.txttar and gzipIn 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:
c create an
archivez zip the
archive with gzipf filename
follows after the flagAfter 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.gzHere the three flags are:
x extract
the archivez unzip the
archive with gunzipf filename
follows after the flagBy 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.gzThe --skip-old-files
argument causes tar to assume
“no” without asking or printing any messages.
tar --skip-old-files -xzf terminal_basics.tar.gzThe -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