Introduction to Linux

Resources

Ubuntu/Linux on Windows

The native command line interface in Windows is conceptually similar to that of Linux, but uses different syntax and very different programs. Luckily you can install what is called the Windows Subsystem for Linux (WSL), which essentially gives you a Linux distribution inside Windows. When installed you can then open a Linux terminal and other Linux programs in the same manner as in the IMADA Computer Lab.

See this guide for instructions for how to install WSL and Ubuntu 18.04.

Linux on macOS?

The command line interface in macOS is almost the same as in any Linux distrubution. The short story is that macOS is a variant of Unix, and Linux started as a free reimplementation of Unix. However, note that sometimes there are subtle differences.

Examples From the Introduction Session

How do I find out what a command does (e.g., ls)?:

man ls

How many lines of code in a file?:

wc -l code.java

Where did I put my Java code?:

find . -iname "*.java"

How many Java files do I have?:

find . -iname "*.java" | wc -l

How many lines of code in each file?:

find . -iname "*.java" | xargs wc -l

in sorted order?:

find . -iname "*.java" | xargs wc -l | sort -n

and without the total count:

find . -iname "*.java" | xargs wc -l | sort -n | head -n -1

or maybe just the total:

find . -iname "*.java" | xargs wc -l | sort -n | tail -n 1

Search for a string in all files:

grep -Rn "^class "

How many matches?:

grep -Rn "^class " | wc -l

Download a webpage, and just print the raw HTML to the terminal:

wget -O - http://dr.dk

Find all lines in the page with links:

wget -O - http://dr.dk | grep http

Extract just the link from the line:

wget -O - http://dr.dk | grep http | sed "s/^.*http/http/" | sed "s/\".*//" | sed "s/\?.*//"

Sort the links, and filter out duplicates:

wget -O - http://dr.dk | grep http | sed "s/^.*http/http/" | sed "s/\".*//" | sed "s/\?.*//" | sort | uniq

Filter out those linking to the same domain:

wget -O - http://dr.dk | grep http | sed "s/^.*http/http/" | sed "s/\".*//" | sed "s/\?.*//" | sort | uniq | grep -v "www.dr.dk"

Someone in my group changed something in a file, but what?:

diff -u original.java changed.java

Remote Access (to the IMADA Computer Lab)

The “ssh” (Secure Shell) program makes it possible to make encrypted connections to other (Linux) computers. The basic usage is to get command line access on remote machines (for example in the IMADA Computer Lab), but it can do much more, like facilitating file transfers through the “scp” and “rsync” commands, and proxying network traffic through other computers.

The basic command is:

ssh username@hostname

On SDU every user is in a domain, which for students is “sdu”. So if your username is “foobar42” your full username is really “sdu\foobar42. The IMADA Computer Lab machines are not accessible directly from the internet, so you first need to connect to a special machine “logon.sdu.dk”, sometimes referred to as the “jump server”. To connect you can then do:

ssh sdu\\foobar42@logon.sdu.dk

Note

The “\” character is special on the command line, so it must be escaped with another “\”, hence the “\\”.

You will then be asked for your password. Once logged in you can access your files, however you should generally not perform any heavy work on the jump server. Instead you can connect further to one of the physical machines. They each have a name on the form “imada-1063xx” where “xx” is a number between 10 and 40. For example:

ssh imada-106310

Note

When you do not specify a username, it uses username you are logged in as on the local machine.

It can get rather tedious to perform this double-login-dance, but luckily we can do it in just one command:

ssh -J sdu\\foobar42@logon.sdu.dk imada-106310

You will now be asked for your password twice, once for each machine.

It is still quite a lot to type, so we can make an SSH configuration file which simplifies it. If the folder “~/.ssh” does not exist, create it. Then create/open the file “~/.ssh/config”, and fill in something similar to the following:

Host imada
    User sdu\\foobar42
    HostName logon.sdu.dk
Host imada10
    User sdu\\foobar42
    HostName imada-106310
    ProxyJump imada

You can now use “ssh imada” to connect to the jump server, and “ssh imada10” to connect straight to machine 10 in the Computer Lab.