Intro to Linux
Pen and paper in pairs
def make_counter(): n = 0 def counter(): nonlocal n n += 1 return n return counter c = make_counter() for i in range(4): print(c())
A common platform
- Operating Systems
- In the Beginning Was the Command Line
- How to Use The Command Line
- code editor: Emacs
Login to
linux.auc-computing.nlusingssh -i ~/.ssh/id_ed25519 myid@linux.auc-computing.nl
A configuration for ssh
Follow these instructions to make a configuration file for ssh.
Logging in to Linux
You should now be able to log in to our Linux compute server with the command
ssh advprog
The Command line
The command line versus a Graphical User Interface
The filesystem hierarchy
/(root),/home,/tmp- Where am I?
pwd - Moving around
cd - Full vs relative path names
- Making directories
mkdir, and removing themrmdir
Files
- What files do I have?
ls - Command options:
ls -l,ls -a - Manual:
man ls,man man - The pager:
less - Make a file:
touch - Delete a file:
rm
Redirection and piping
Send output to file (overwriting it if it already exists)
date > testfile.txt
Append output to file
date >> testfile.txt
Get input from one file and send output to another file
wc < testfile.txt > otherfile.txt
Get input from one file and send output to another command
cat < testfile.txt | wc
Some other commands
Who is logged in?
w
(you might have to stop this with
Control-C)Who were the last eight people to log in?
last | head -8
Some fun
cowsay "It's fun to learn Linux"
Python and Racket
- From the command line
- Python
- Ipython
- Racket
Running Python and Racket programs from the command line
- Download this python program and this racket program from the command line
wget https://advprog.auc-computing.nl/code/chars_per_line.py wget https://advprog.auc-computing.nl/code/sum-ints.rkt
#!/usr/bin/env python3 # -*- mode: python; coding: utf-8; -*- """ Read line by line and write the number of characters in each line. """ import sys def main(): for line in sys.stdin: print(len(line) - 1) # excluding newline character if __name__ == "__main__": main()
#! /usr/bin/env racket #lang racket/base ;; Read numbers from standard input, one per line. Print their sum. (define (main) (for/fold ((total 0)) ((line (in-lines))) (+ total (string->number line)))) (main)
- Change the attributes of the files to make them executable
chmod +x chars_per_line.py chmod +x sum-ints.rkt
- Run them from the command line. Finish with
Control-d
$ ./chars_per_line.py abc 3 defg 4 h 1 $ ./chars_per_line.py < chars_per_line.py 21 38 0 3 66 3 0 10 0 11 26 56 0 26 10 $
$ ./sum-ints.rkt 1 2 3 6 $
- Now combine them using redirection and a pipe
$ cat chars_per_line.py | ./chars_per_line.py | ./sum-ints.rkt 270 $
- When looking for executables, Linux checks the directories in the
$PATHenvironment variable
echo $PATH
Further sources
- LinuxCommand
- The Command Line Crash Course
- The bash shell and command language
- bash guide
-
Read a file of text, determine the n most frequently used words, and print out a sorted list of those words along with their frequencies.
McIlroy's shell script
#!/bin/bash tr -cs A-Za-z '\n'| tr A-Z a-z | sort | uniq -c | sort -rn | awk '{print $2}'| head -n $1