Intro to Linux

unix_history_by_legosz_d46a501-fullview.png

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

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

linux-filesystem-hierarchy-small.gif

standard-unix-filesystem-hierarchy.png

  • / (root), /home, /tmp
  • Where am I? pwd
  • Moving around cd
  • Full vs relative path names
  • Making directories mkdir, and removing them rmdir

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

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 $PATH environment variable
echo $PATH

Further sources

Submission of Assignments

etc

Author: Breanndán Ó Nualláin <o@uva.nl>

Date: 2025-02-27 Thu 10:27