Day 2 - Introduction to bash

Video

Motivation

Once upon a time, people had to interface with computers via physical flipping of switches and soldering of components.

Then came the punch card, and there was much rejoicing. And very expensive typos, and long lines at the punch card feed stations.

But next came remote terminals - much like the desktop computers of today (and in some ways a lot like your office computers), you could type at the terminals and see the computer's responses on screen. Entire universities would log on to the same single computer.

That is still how much of modern supercomputing is done, and high intensity computing as well. In the department, when you run your compute jobs, that is how you should launch and control them.

This is not simply a relic of the past - I hope to show you some of the advantages this approach to computers offers locally as well as remotely.

While windows PowerShell - and its ancestor cmd.exe - have a long history of their own, they are not much used in scientific computing - or used much at all except for certain Windows-specific tasks. So we will focus on bash.

Basics

For this course, we are going to work using the mock data studentdata.zip. Please download and unzip it into your folder for this course.

If we succeeded yesterday, you should already have a version of bash on your system - either git bash on windows, or your native bash on other platforms.

Native BASH on Windows

Git bash is not a full bash, and suffers from a variety of issues that will likely not affect us during this course. There are a variety of more full-featured options available.

The most effective, modern solution is to simply install Linux - which in recent years has become easier than ever to do within Windows. Following the official directions for the Windows Subsystem for Linux - Ubuntu is recommended for first-time Linux users - will get you a full linux install, bash and all, in your windows command line.

If you choose to do this, very little about the course will be different for you.


To open bash in Atom, open a terminal. On windows, we will invoke it via its full path. Type in the PowerShell console:

'C:\Program Files\Git\bin\bash.exe'

Congratulations, your powershell window is now a bash window.

But what do we do with it?

First things first, we look around. We will have to get used to this perspective - it will be your main one on the department servers.

pwd

the print working directory command tells you where you are.

ls

The List command lists (most of) the things that are here. Some things - primarily, any file or directory starting with a . - are hidden by default.

To find out how to show them, commands come with help:

ls --help

That sure goes by fast! Fortunately, we can slow it down.

Console output is naturally printed, but we can redirect it. Yesterday we used > to redirect output into a file, but we can use | - the "Pipe operator" - to redirect it into another command:

ls --help | less

less takes a long buffer and places it into a navigable, interactive session. It is a great way to page through the output of a command, or even a file; you could page through, say, yesterday's atom_packages.txt by doing:

cat atom_packages.txt | less

Using the concatenate command to turn a file into a "stream" of output to feed to less.

Scroll through it and you will see that the flag we need is

ls -a

This shows us all the files and directories in the directory. But there are two surprises - . and ...

These are references - . means "here" and .. means "The directory containing this one."

The third essential navigational tool is cd. This changes the current directory. We want to work in the studentdata folder, so let's go there:

cd studentdata

if you check with

pwd

you can see we got where we wanted!

The last and most important part of this walkthrough is: how do we use the programs we have on files?

We can call any of them with a command-line interface using their command, to act on files.

In this case, the lecture portion is now over, and it is time for the worksheet. To practice, let's open it from the console:

atom worksheet.md

or follow the worksheet link for a web description!

Bash Quickreference

Command   Functionality Example Usage
pwd Prints the current location pwd
ls Lists files in directory ls -al
cd Changes current directory cd WWW
less Enters an interactive viewing of a buffer ls --help | less
cat Turns a file into a buffer, allowing it to be piped cat users.txt | less
head Returns the first x lines of a file head example.csv
tail Returns the last x lines of a file tail example.csv
cp Copies a file (or directory with -r flag) cp users.txt data/users.txt
rm Removes (deletes permanently) a file. Be careful. rm users.txt
mv Moves a file mv users.txt data/users.txt
grep Search for text in buffer, file or set of files grep Name users.txt
Department of Mathematics, Purdue University
150 N. University Street, West Lafayette, IN 47907-2067
Phone: (765) 494-1901 - FAX: (765) 494-0548
Contact the Webmaster for technical and content concerns about this webpage.
Copyright© 2018, Purdue University, all rights reserved.
West Lafayette, IN 47907 USA, 765-494-4600
An equal access/equal opportunity university
Accessibility issues? Contact the Web Editor (webeditor@math.purdue.edu).