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
Count the number of lines/characters of file
/appl/data/geo/syke/natura/Readme_natura.txt
.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.
Solution
man wc
for finding the documentation. Use q
to exit the documentation.
wc -m /appl/data/geo/syke/natura/Readme_natura.txt
to count the characters in the file. Orcat /appl/data/geo/syke/natura/Readme_natura.txt | wc -m
-> 177ls /appl/data/geo/syke | wc -l
, we pipe the output ofls
which is a list of files intowc
which by defining-l
prints out the amount of newlines (= number of lines) -> 200
Count all directory and file names that include 2021
: ls *2021* | wc -l
-> 156
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:
Navigating folders
Login to the Puhti web interface, and start a login shell; first, check which directory you are in by typing
pwd
and hittingEnter
:
pwd
Then check your username, by typing
echo $USER
andEnter
.$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 alsoecho hello
).
echo $USER
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
Check if there are any files:
ls
Make a directory with your name (you can either type it or use the variable $USER) and see if it appears:
mkdir $USER
ls
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.
Open the file with
nano
:
nano $USER-first-file.txt
Edit the file. Type something there!
Exit
nano
withCtrl+X
, typeY
to confirm saving and press enter to accept the filename.Check that the modifications are actually there:
less $USER-first-file.txt
Exit the preview with
q
.
Exploring files#
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
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.
Use the
less
command to check out what the file looks like:
less my-first-file.txt
To exit the
less
preview of the file, hitq
.
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.
Make a copy of this file:
cp my-first-file.txt $USER-second-file.txt
ls -lth
less $USER-second-file.txt
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).