using unix

introduction

These pages are meant for students new to the SwatCS computers.

After working through the sections in the Table of Contents on the left, you should feel comfortable using some basic unix commands in the terminal window.

And don’t feel like you have to remember it all after one session. Feel free to return to these pages anytime to refresh or learn more. The more comfortable you are with unix commands in the terminal, the more efficient you will be getting your CS labs done!

UNIX is basically a simple operating system, but you have to be a genius to understand the simplicity.

passwords for new users

New users need to set their initial CS account password. If you haven’t already, go to our self-service password app and click the "Click to Request Reset" link near the bottom of the page. From that link you should be able to set your initial CS password (enter your username, click Request Password Reset, check your Swarthmore email, etc).

If you have any trouble setting your password, see our new user help page for detailed instructions and screenshots.

The CS computers are different from the ITS computers on campus. We run linux on these computers, and only students taking Computer Science (CS) have accounts on these computers (which is why your CS password should be different from your Swarthmore/ITS account password).

logging in

Once you have your CS account password, you can use ssh to access the CS computers. ssh stands for secure shell, and is a program that encrypts all communication between your home computer and the CS computers. It runs from a terminal window and is probably already installed on your computer (if not, see our remote access help page).

On Macs, search for the Terminal application and start it. On Windows, either search for the PowerShell application or install putty and start it. And try to keep the browser window and the terminal window open and side-by-side for the rest of this tutorial, so you can try out commands as we go!

To use ssh, enter the following command in the Terminal or PowerShell/putty window, replacing username with your username (e.g., dduck1):

ssh -Y username@cslab.cs.swarthmore.edu

The first time you connect with ssh it will ask "are you sure you want to continue connecting (yes/no)?", like this:

ssh -Y dduck1@cslab.cs.swarthmore.edu
The authenticity of host 'cslab.cs.swarthmore.edu (130.58.68.122)' can't be established.
ECDSA key fingerprint is SHA256:KDki/m7vRcdU4J99pfUd2a9dQ+xEbXR4ERZx4gW7gwU.
Are you sure you want to continue connecting (yes/no)?

Answer "yes" to this question (assuming the fingerprint matches!) and your computer will store that key fingerprint so it knows it is always connecting to the correct computer.

Finally, you should be prompted for your CS password, and, if correct, be logged in to one of our CS computers. Here’s an example of what you might see:

ssh -Y dduck1@cslab.cs.swarthmore.edu
Password:
Welcome to: macaw
Ubuntu 20.04 focal
macaw[~]$

In the above example, the user (dduck1) is logged in to a computer called macaw. You may get a different computer. Many of our computers have bird names (dodo, loon, hawk, etc). And the macaw[~]$ part is the unix terminal prompt. That’s where we will type all unix commands discussed below.

If you have any trouble getting logged in, or any questions about the CS computers, send us an email at sysadmins @ cs.swarthmore.edu and we will be happy to help you.

directories (aka, folders)

Your home directory (some people call them folders) is part of a tree-like structure:

directories example
  • the full path to dduck1’s home directory (the red oval) is /home/dduck1

  • the full path to dduck1’s 01 directory (the blue oval) is /home/dduck1/cs21/labs/01

  • you can use the pwd command to print your working directory, or where in the tree you currently are

Try this example! And the dollar sign ($) below is just the unix prompt (yours might look slightly different). You don’t type the dollar sign, just everything after the dollar sign.

And when we say "run this command", we mean type it in a terminal window.

$ pwd

The above should display /home/username, where username is your actual CS username (like dduck1, or aturing7). When you first log in, you are always in your home directory (/home/username).

Try this example, too!. Let’s make a sub-directory called testdir, just for testing:

$ mkdir testdir

The mkdir command makes a new directory (there’s no output or anything if the command worked).

In the next few sections we’ll see how to view our files and change into different directories, like the test directory we just made.

changing directories

We use the cd and pwd commands to change directories and display where we are.

Try this!

$ pwd
$ cd testdir
$ pwd

The first pwd should display your home directory. Then the change directory (cd) command moves you one level down in the tree, to your newly-created testdir. And the last pwd should display /home/username/testdir.

Users new to UNIX are often confused by paths and directories. A full path lists out all directories, from the root, like /home/dduck1/cs21/labs/00. You can always use a full path, but it’s sometimes quite long.

What does cd .. do? Try it and see if you can figure out where it puts you in the directory tree (hint: use pwd)!

If you use cd .. enough times, you will get to the root of the directory tree (slash or /):

$ cd ..
$ pwd
/home
$ cd ..
$ pwd
/
$ cd
$ pwd
/home/dduck1

You can always use cd by itself to get back to your home directory (see the last cd in the above example).

listing your files

The ls command is short for list. You can use the ls command to see your files and directories (folders). You can also list out files in another directory using the full path (with the slashes in it — see below).

Try these examples!

$ ls
$ ls testdir

The first ls above displays a few files and directories in your home directory. You don’t have much at this point (I think it just shows testdir, Desktop, and Documents).

The second ls above displays everything in your testdir directory, which is nothing at this point, since we just made that. In the next section we will add some files to that directory.

Try these, too.

$ ls ~jk
$ ls ~jk/inclass
$ ls -l ~jk/inclass

The first ls above lists out the files of user jk. This is something you might do when copying files from your professor. In the output of the first ls command, you should see an inclass/ entry. The slash at the end of inclass indicates it is a directory.

The second ls above lists the files in the /home/jk/inclass directory (~jk is shorthand for /home/jk).

The last example above uses the -l (lower-case L) option, which shows the long format for listing files. This shows extra info on the files and directories, such as the owner (jk) and size of the file, and who has access to read and write the file.

In the next section we will copy the hello.py file into our testdir, so we can view and edit the file.

copying files

The cp command is used to copy a file. The format of the command is cp file newfile, and this makes a copy of file called newfile.

For example, to make a backup copy of a file named prog.py, you could run cp prog.py backup-prog.py

Try this! Let’s change into our testdir directory and then copy the hello.py file from user jk:

$ cd testdir
$ cp /home/jk/inclass/hello.py hello.py
$ pwd
$ ls

The above pwd output should show you are in your testdir directory. The ls should now show you have a copy of hello.py in your testdir directory.

Remember: if you’re ever lost, a simple cd command will put you back in your home directory (/home/username).

One more to try! Copy over the unix_commands file from our /scratch/usingunix directory:

$ cp /scratch/usingunix/unix_commands .
$ ls

Don’t forget the dot (.) at the end of that cp command! Using a dot for the newfile part of the copy command is a shortcut for "name the copy the same as the original file". When you run ls, you should now have a copy of the unix_commands file.

In the next section we will see how to view the contents of a file.

viewing files

Sometimes we want to edit or change a file, and sometimes we just want to see the contents of the file.

To see the contents of the file there are two unix commands: less and cat.

The cat command (short for concatenate) can be used to just dump the contents of the file to the screen. If the file is large, the contents may scroll off the top of the terminal.

Try the cat command! If you are still in testdir, run the cat command on the two files we now have in there.

$ cat hello.py
$ cat unix_commands

To view a file one page at a time, use the less command:

$ less unix_commands

Hit q to quit out of less at any time.

editors

There are a variety of text editors on our computers. Some students use atom, some use vim, and some use other text editors such as emacs or sublime. Your class web page or instructor may suggest which editor is best for your class.

For CS21 this semester (F20) we are using emacs.

atom is probably the easiest to learn, but somewhat tricky to use if working remotely.

Students taking CS31 should learn vim. There is a vimtutor command which puts you into a file and teaches you the basics of vim. Your first lab (lab0) will have more information on using and learning vim.

rename or remove a file

The mv command is used to move or rename a file. The format of the move command is similar to the copy command: mv file newfile. For example, to rename a file from badname.py to goodname.py: mv badname.py goodname.py

You can also move a file into a different directory with mv file directoryname

The rm command is used to remove a file. For example, to remove a file called oldprog.py: rm oldprog.py

Examples to try!

$ cp /scratch/usingunix/myprog.py mypppprog.py
$ ls
$ mv mypppprog.py myprog.py
$ ls
$ cat myprog.py
$ rm myprog.py

The above commands should copy a file, then rename it, show the contents of the file, and finally remove it. When you remove the file, it will ask "remove myprog.py?", to which you can answer either 'y' or 'n'.

logging out

When you are done for the day you can log out of the terminal window with Ctrl-d (hold down the Ctrl key and then hit the d key).

summary

unix commands summary

  • cd — change directory

  • ls — list your files

  • pwd — print your working/current directory

  • cp — copy a file

  • mv — move/rename a file

  • rm — remove a file

There are many more unix commands, but those are the basics you will need for any CS class.

For example, you can change your password using our self-service password app, but there is also a passwd command that allows you to change your password from within the terminal (see below).

examples of the above commands

In this example, the student’s username is dduck1.

$ pwd
/home/dduck1
$ ls
Desktop/  Documents/  cs21/
$ cd cs21
$ pwd
/home/dduck1/cs21
$ ls
inclass/  labs/

$ cd inclass/
$ ls
w01-intro/
$ cd w01-intro/
$ ls
welcome.py

$ cp welcome.py  w2.py
$ ls
w2.py  welcome.py
$ mv w2.py welcome2.py
$ ls
welcome.py  welcome2.py
$ rm welcome2.py
rm: remove regular file welcome2.py? y
$ ls
welcome.py

$ passwd
Enter login(LDAP) password:
New CompSci password:
Retype new CompSci password:
LDAP password information changed for dduck1
passwd: password updated successfully

other possibly-useful commands

  • a2ps myfile.py — prints myfile.py file to the lab printer

  • sharples — prints out the menus and hours

  • cal or cal 2020 — prints out calendars

For more help documentation: https://www.cs.swarthmore.edu/newhelp

Any questions about the CS computers, please email sysadmins at cs.swarthmore.edu