Introduction to Linux¶
Learn the Command Line, interactive tutorial on CodeAcademy.
ExplainShell, a tool for explaning a given command line.
Unix Pipeline (Brian Kernighan), Computerphile on Youtube.
Standard streams, Wikipedia.
Pipeline (Unix), Wikipedia.
stdin, stdout, stderr, additional material on redirection and pipelines.
Where GREP Came From, Computerphile on Youtube.
Is It Worth the Time?, XKCD.
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.,
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)¶
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 “
rsync” commands, and proxying network traffic through other computers.
How Secure Shell Works (SSH), Computerphile on Youtube.
The basic command is:
On SDU every user is in a domain, which for students is “
So if your username is “
foobar42” your full username is really “
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:
\” 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 “
xx” is a number between 10 and 40. For example:
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\\firstname.lastname@example.org 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 “
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 “
imada10” to connect straight to machine 10 in the Computer Lab.