Linux#
This notebook serves as starting point for learning linux and the bash terminal. I highly recommend, that you run it interactively via the mybinder.org link in the top. This will allow you to execute the commands yourself.
Usually developers, system administrators, devop engineers, etc. interact with the the linux operating system via a textual interface, called the shell. The most common type of shell is called bash
. For now, the differences between these is not so important, but during troubleshooting it might help to know which shell you are using.
Linux comes with lots of preinstalled programs which make working with the shell very easy. We will look at some of them and how to use them.
This notebook is for learning purpose only. You will never interact with the shell using such a notebook. The %%bash
in the lines is only for the notebook. You can safely ignore it.
File System#
Linux is a directory/file-based operating system.
In Linux, everything is treated as a file, including hardware devices, processes, and system configurations. The file system is organized in a hierarchical directory structure, with the root directory /
at the top. All files and directories are accessed relative to this root directory.
The home directory is a personal directory assigned to each user on the system. It serves as the default working directory when a user logs in and is where the user’s personal files, configurations, and directories are stored. The ~
is a shorthand for the home directory. It simplifies navigation.
Typically, when you open up a terminal, your session will start in the ~
folder. This is not always the case, as for the notebook here for example. The notebook and the shell open up in the folder from where they are launched. Let us look at that first.
%%bash
pwd # print the current working directory
ls # list the contents of the current working directory
/home/runner/work/learning-notes/learning-notes/learning-notes
_config.yml
_toc.yml
abbreviations.md
assessment.md
bash.md
cnn.ipynb
convolution.ipynb
dqn.ipynb
fo
rmular.ipynb
images
intro.md
iris.ipynb
k-means.ipynb
kalman-filters.ipynb
kubernetes.md
links.md
li
nux.ipynb
mlp.ipynb
obj-detection.ipynb
optimizers.ipynb
p-np.ipynb
pca.ipynb
perceptron.ipynb
rl.ip
ynb
rnn.ipynb
softmax.ipynb
torch.ipynb
virtualization.md
Next we want to see how we can navigate the filesystem. Let us start with the root and see what lies beneath it.
%%script bash
cd / # change to the root directory
pwd # print the current working directory
ls # list the contents of the root directory
/
bin
bin.usr-is-merged
boot
data
dev
etc
home
imagegeneration
lib
lib.usr-is-merged
lib32
lib64
lost+
found
media
mnt
opt
proc
root
run
sbin
sbin.usr-is-merged
snap
srv
sys
tmp
usr
var
While the root directory is the starting point, you will seldomly operate inside it directly. You will however use it for navigation. The Github build pipeline runs this notebook inside an Ubuntu linux, such the output here will reflect a typical ubuntu installation. Ubuntu follows the Filesystem Hierarchy Standard. It is not necessary to remember them all, but it is good to have an overview of the most important directories.
/dev
: Contains device files that represent hardware devices, such as sda (hard drives), tty (terminals), and null. etc/tmp
: A temporary directory for storing files that are deleted after a reboot or after a certain period./var
: Contains variable data, such as logs (log), mail (mail), and temporary files for running services./home
: Contains the home directories of users. For example, /home/alice is the home directory for the user alice./usr/bin
: Non-essential user binaries./usr/lib
: Libraries for user applications./mnt
: A temporary mount point for attaching external storage devices like USB drives or network shares.
%%bash
whoami # shows my username
cd ~ # change to my personal directory
pwd
runner
/home/runner
As you can see, our home directory lies beneath /home/
. This is expected.
Manipulating files#
As said before, everything is file based in linux. A common task is to configuration files or read logs etc.
%%bash
cd ~
touch mytext.txt # create a new file called mytext.txt in the root directory
echo -n "Hello" > mytext.txt # write "Hello" into the file without a trailing newline
echo " World!" >> mytext.txt # append "World!" to the file
cat mytext.txt # display the contents of the file
Hello World!
We have seen a lot. touch
simply creates a a file. We give it the txt suffix to highlight that it is a plain text file. Although the suffix does not mean a lot. It is just an indication. Next, we used the echo command to output text. A plain echo "Hello"
would just print Hello to the standard outoutput stdout
, which is typically the terminal. The >
operator redirects the output to a file instead of displaying it to the terminal. The >>
appends to a file instead of overwriting. If a program prints a lot of debug information for example to stdout, you can use >
/>>
operator to aggregate the logs and write them into a file.