5.3.1. Linux Basics + Running Python#

Goal: Get comfortable working on the course Linux system using core command-line tools, and run a simple Python script from the terminal.

5.3.1.1. Learning Objectives#

By the end of today you should be able to:

  • Navigate the filesystem (pwd, ls, cd)

  • Create directories/files (mkdir, touch, cp, mv, rm)

  • View/edit text files (cat, less, vi)

  • Run a Python script (python3 script.py)

  • Capture command output into a file (redirection > and >>)

5.3.1.2. 0. Login and Setup#

Log into Pete using your assigned account.

ssh [your username]@pete.hpc.okstate.edu

5.3.1.3. 1. Warm-up: Where am I? What’s here?#

In this section we will look at basic linux commands that allow us to determine what directory we are currently in and what the contents of that directory are.

First, however, we will make sure we are logged into the cluster. Perform the following commands:

[mmccull@pete mmccull]$ echo $HOSTNAME
login01.cluster

The command pwd stands for print working directory. The shell will print the location/address of the current directory you are in.

[mmccull@pete ~]$ pwd
/home/mmccull

The command ls stands for list. The shell will print the contents of the current directory. In your case, there may be nothing there.

[mmccull@pete ~]$ ls
apps  force_files  PYK-ZAL.zip

If you do have something present, different files and/or directories may be colored differently. The default coloring scheme for ls on this system is: directories are blue, zipped files are red, executable files are green, and normal files are black (or white depending on your shell preferences).

Commands like ls can take options which format the output. Command options are given after a - symbol. For example, ls -l will list the contents of the directory in a table with additional information about each file/directory.

[mmccull@pete ~]$ ls -l
total 17272
drwxr-xr-x 6 mmccull clusterusers       10 Jun  6  2022 apps
drwxr-xr-x 3 mmccull clusterusers       12 Oct 19  2023 force_files
-rw-r--r-- 1 mmccull clusterusers 19030099 Nov 12  2020 PYK-ZAL.zip

The first column of this table provides information about the file permissions. drwxr-xr-x translates to: d indicates a directory, the next rwx indicates read write and excute permissions for the user, r-x read and execute permission for the group, and r-x read and execute for all. The second column you can ignore, the third column mmccull is my username, clusterusers is my group name, the column wiht 10, 12, and 19030099 indicates the file size in bytes. Thiis followed by the date and then the file/directory name.

Tip: If a command prints a lot, use:

ls -l | less

Press q to quit less.

5.3.1.4. 2. Create Your Course Directory#

We are going to create a directory in a shared group space. We will then move to this space and perform all of today’s activities in there.

First, we will change a setting in your shell boot script to setup proper priveleges for the group space files and directories.

[mmccull@pete ~]$ echo "umask 0007" >> ~/.bashrc
[mmccull@pete ~]$ source ~/.bashrc

Now we are going to create the folder. To do this I am going to make use of an environment variable $USER. In bash shell, things with a $ in front of them are variables. You can store different things in variables. Your username is stored in $USER. To see this, we can use the echo command:

[mmccull@pete ~]$ echo $USER
mmccull

Your output should be different. Now, create a directory for this course and for Week 1, then move into it (I am going to drop the [mmccull@pete ~]$ part of the shell prompt and just provide the commands):

mkdir -p /projects/tia001/$USER

At this point, we made a folder named your username in director /projects/tia001. You should still be in your home (/home/[your username]) directory. So now we want to move from one directory to another. The command to achieve this is cd which stands for change directory.

cd /projects/tia001/$USER
pwd

You should see a path /projects/tia001/[your usernmame]. You can move back to your home directory by simply running cd. For example, if I do all of that it looks like:

[mmccull@pete mmccull]$ cd /projects/tia001/$USER
[mmccull@pete mmccull]$ pwd
/projects/tia001/mmccull
[mmccull@pete mmccull]$ cd
[mmccull@pete ~]$ pwd
/home/mmccull

There are a couple of important things about the command cd. You can cd with either an absolute or a relative location. cd .. will take you back on directory (e.g. if you are in /projects/tia001/$USER and you type cd .. you will go back one directory to /projects/tia001). Try the following:

cd /projects/tia001/$USER
pwd
cd ..
pwd
cd ..
pwd
cd ..
pwd
cd projects
pwd
cd tia001
pwd
cd $USER
pwd

Now create a week1 folder in your /projects/tia001/$USER folder:

cd /projects/tia001/$USER
mkdir -p week1
cd week1
pwd

the pwd command should print /projects/tia001/[your usernmame]/week1.

5.3.1.5. 3. Files: Create, Copy, Move, Remove#

5.3.1.5.1. 3.1 Create a file#

touch myinfo.txt
ls -l

5.3.1.5.2. 3.2 Edit a file#

Use one editor below (either is fine):

  • nano myinfo.txt (beginner-friendly; https://linuxize.com/post/nano-save-and-exit/ )

  • vi myinfo.txt (powerful; you will see it often on HPC systems; we will use this heavily later; https://www.cs.colostate.edu/helpdocs/vi.html)

Add a single line to myinfo.txt (anything you want), save, and exit.

5.3.1.5.3. 3.3 View a file#

cat myinfo.txt

5.3.1.5.4. 3.4 Copy it#

cp myinfo.txt myinfo_backup.txt
ls -l

5.3.1.5.5. 3.5 Move/Rename it#

mv myinfo_backup.txt myinfo_copy.txt
ls -l

5.3.1.5.6. 3.6 Delete the copy#

rm myinfo_copy.txt
ls -l

Safety note: Be careful with rm. There is no undoing an rm operation on the command line.

5.3.1.6. 4. Running Python from the Command Line#

Python is a commonly used coding language that we will use in this class. You create a python file with the desired lines of code and then feed it to the python program to run the code. We will write a simple piece of code here

Create a file called calc.py in /projects/tia001/[your usernmame]/week1. Do this with the same text editor as before:

nano calc.py

OR

vi calc.py

Type (or copy and paste) the following contents into this file:

import math

x = 3
value = math.sqrt(x**2 + 1)
print(f"{value:.6f}")

Save and quit (ctrl+x; y in nano or :wq in vi).

Then run:

python3 calc.py

You should see a single number printed (with 6 digits after the decimal).

5.3.1.6.1. Quick check#

  • What is the value of \(\sqrt{10}\)?

  • Does your script output match (approximately)?

5.3.1.7. 5. Capturing Output: Create listing.txt#

Create a file called listing.txt in your week1 directory that contains:

  1. The output of pwd

  2. The output of ls -l

Use redirection:

pwd > listing.txt
ls -l >> listing.txt

Then confirm:

cat listing.txt

5.3.2. End-of-Class Assignment#

By the end of class, your directory must contain these files:

  • info.txt

  • calc.py

  • listing.txt

All should live in:

/projects/tia001/[your usernmame]/week1

5.3.2.1. A. Create info.txt#

Create a file info.txt containing exactly three lines (replace the angle-bracketed parts with your own):

NAME: <Your Name>
USERNAME: <your_username>
SHELL: <output of echo $SHELL>

To get the shell line, run:

echo $SHELL

5.3.2.2. B. Modify calc.py#

Modify calc.py to compute the square root of 17 (\(\sqrt{17}\)). Confirm you get the correct answer.

python3 calc.py

5.3.2.3. C. Confirm listing.txt is present#

Re-generate if needed:

pwd > listing.txt
ls -l >> listing.txt

5.3.2.4. D. Final check (do this last)#

cd /projects/tia001/$USER/week1
ls -l
cat info.txt
python3 calc.py
head -n 1 listing.txt

If all commands work and the files look correct, you are done.