Psst, remember the cheatsheet!

Terminal#

Why would you want to use the terminal?#

A terminal is a text input and output environment. A shell is the primary interface that users see when they log in, whose primary purpose is to start other programs.

A typing-based interface is often called a command-line interface, or CLI, to distinguish it from a graphical user interface, or GUI. The heart of a CLI is a read-evaluate-print loop, or REPL: when the user types a command and then presses the Enter (or Return) key, the computer reads it, executes it, and prints its output.

Shell is the standard way to interact with a supercomputer. It is worth learning the basics and getting comfortable with the “black box”, to make efficient use of the resources.

The terms terminal, command line and shell are often used interchangably, even though they mean slightly different things.

Some terminal magic

Did you ever want to count all the files in a directory? Or lines in a file? Apart from opening the file in an editor, turning on line numbers or counting them manually you can also use a neat little command line tool called wc.

Check out the documentation for wc and see if you can find out how to

  1. Count the number of lines/characters of file /appl/data/geo/syke/natura/Readme_natura.txt.

  2. Count all directories in /appl/data/geo/syke.

If this was too easy, can you count only the directory and file names that include 2021 in the syke directory?

You can use the “login shell” via the Puhti webinterface for this exercise.

Basic Linux commands

Do you remember on how you edited some files in the web interface? Let’s do the same thing again; only from the command line:

  1. Login to the Puhti web interface, and start a login shell; first, check which directory you are in by typing pwd and hitting Enter:

pwd
  1. Then check your username, by typing echo $USER and Enter. $USER (note the $ in the beginning) is an environment variable which stores your CSC username. echo is a command that allows us to display text that is given as an argument (try also echo hello).

echo $USER
  1. We would like to create a new directory in our projects scratch students directory with our name, let’s move there:

cd /scratch/project_200xxxx/students
  1. Check if there are any files:

ls
  1. Make a directory with your name (you can either type it or use the variable $USER) and see if it appears:

mkdir $USER    
ls
  1. Go to that directory.

cd $USER      

Auto complete

If you just type cd and the first letter of the folder name, then hit tab key, the terminal completes the name. Handy!

Add an empty file into this directory.

touch $USER-first-file.txt    

Check that it is empty:

cat $USER-first-file.txt        

Fill it with some content:

Command line editor

These exercises are done with the nano editor, but you can use your favorite editor too. Here’s a nano cheat sheet.

  1. Open the file with nano:

nano $USER-first-file.txt     
  1. Edit the file. Type something there!

  2. Exit nano with Ctrl+X, type Y to confirm saving and press enter to accept the filename.

  3. Check that the modifications are actually there:

less $USER-first-file.txt     
  1. Exit the preview with q.

Exploring files#

  1. Download a file into this new folder. Use the command wget for downloading from a URL:

wget https://github.com/csc-training/csc-env-eff/raw/master/_hands-on/linux_prerequisites/my-first-file.txt
  1. Check what kind of file you got and what size it is using the ls command with some extra options:

ls -lth         # options are l for long format, t for sorting by time and h for convenient size units. 
  1. Use the less command to check out what the file looks like:

less my-first-file.txt
  1. To exit the less preview of the file, hit q.

Tip

Instead of less you can use cat which prints the content of the file(s) straight into the command line. For long texts less is recommended.

  1. Make a copy of this file:

cp my-first-file.txt $USER-second-file.txt    
ls -lth
less $USER-second-file.txt                    
  1. Remove the file we originally downloaded (leave your own copy).

rm my-first-file.txt
ls

Moving files

If you don’t want to have duplicate files you can use mv to ‘move/rename’ the file. Syntax is the same: mv /path/to/source/oldname /path/to/destination/newname.

Re-executing commands from history

If you remember a part of a command that you have used recently you can search for it with the command history | grep string. This will show all your used commands that have included the string string (replace this with the pattern you are searching for).