Week 12-13: SSH and the Linux command line
2013-04-10
We'll spend the next few weeks working on a server running a Linux-based
operating system.
We're going to run through some examples using the Bash shell and some
utility programs.
Here are a few of the commands we'll use:
man
: display help information
cd
: change directory
pwd
: print working directory
ls
: list directory contents
wc
: count letters, words, or lines
cp
, rm
, mv
, touch
: copy, remove, move, create/update files
nano
: a simple text editor. vim
is more capable but more complicated.
SSH
SSH is protocol for secure network communication, remote login, and
remote command execution.
To log in to a server that you have an account on, use the command
ssh USERNAME@HOSTNAME
.
To run a command on the server, use the same command but also list the command:
ssh USERNAME@HOSTNAME cat .bashrc
will print the contents of your .bashrc
file.
File system
Most file systems we'll use are based on the same ideas. The only differences
we'll run into (compared to working with the Windows command line) are:
/
is the path separator rather than \
.
- There are no
C:
or D:
drives; rather all storage devices are mounted on
a directory, which can simplify man filesystem operations.
Globbing
Globbing is the way that Bash refers to multiple files at once (using
wildcards). It will often work the same as in the Windows command line, but in
the Windows command line globbing is performed by each program (so it can vary
from program to program), while the Bash shell performs globbing itself.
Exercises
-
Create a directory structure that looks like the hierarchy shown below (with
all files empty). The touch
command creates an empty file if the name
doesn't already exist.
folder1/
a.txt
b.txt
c.txt
folder2/
a.txt
b.txt
c.txt
folder3/
a.txt
b.txt
-
What command would you use to remove all files starting with a
, b
, c
,
or d
in your current directory? What happens if there is a subdirectory
starting with b
in your current directory?
-
The find
command is used to locate files (and list the found files). How
can you use it in combination with wc
to count the number of files with names
ending in .so
in the /usr/lib
directory?
-
The grep
command searches through files (or standard input), much like
findstr
that is usually available in the Windows command line. It uses
regular expressions to define a pattern for the search. Suppose you
want to print all the lines of a Java program that aren't comments. What
command could you use to do that? Assume that commented lines always
start with some number of spaces and then the single-line Java comment
token (//
). Hint: look at the man page for grep
to see how to
select all lines that don't match a pattern.
File permissions
After logging in, run the command ls -al
to give a detailed list
of your home directory. Here's a partial output for my directory:
drwx------ 15 jlepak jlepak 4096 Apr 24 01:58 .
drwxr-xr-x 15 root root 4096 Apr 24 00:40 ..
-rw-r--r-- 1 jlepak jlepak 270 Apr 24 01:31 add-csc185.sh
drwx------ 2 jlepak jlepak 4096 Nov 22 00:39 .aptitude
-rw-r----- 1 jlepak root 27681 Apr 2 19:18 auth.log
-rw------- 1 jlepak jlepak 1648 Apr 24 01:20 .bash_history
The first field gives details of the file permissions for each entry. There
are 10 columns: the first indicates if the entry is for a directory (or other
possibilities); the next 9 indicate if read, write, or execute (allowing the
file to be run as a command) permissions are enabled for the user (the primary
owner of the file, shown in field 3), group (a group of one or more users, shown
in field 4), and all others.
The chmod
program can change file permissions to enable or disable any of
those permissions.
More exercises
- Type the
whoami
and groups
commands. What is their output?
- Type the
who
command. Who else is logged in?
- Navigate to someone else's home directory: use the command
cd /home
to navigate to the folder that contains all the user directories, and
then pick one. What happens when you try to create a file in
someone else's home? (You can use the command touch my-file.txt
to
try).
- Navigate to the
/var/www/ilzd.org/csc185
folder. /var/www/ilzd.org
holds the files served up by the web server, and csc185
is a folder
that everyone in class has permissions to write to.
Create a text file (either a .txt
file or a .html
file if you are
familiar with HTML) and put a short message in it. Name it something
unique so I know it's from you. Verify that you can see it in a web
browswer by navigating to http://ilzd.org/csc185/yyy.txt
(where yyy.txt
is whatever you named your file).
Readings
Take a look at the Software Carpentry website, and especially
the chapter about the shell.