aboutsummaryrefslogtreecommitdiff
path: root/content/weblog/2018-11-05_intro-to-linux-and-bash/index.md
blob: 28db692c9a1bf7fcd73453ee605c60dc37b5ddbb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
+++
title = "Intro to Linux and the Bash command line"
date = 2018-11-05T23:53:00Z
+++

Recently I decided to introduce a friend of mine to the wonderful world of
Linux, and like when someone moves to a completely new town, you have to help
that friend get around town and learn about how things work in this new town,
where are all the places of interest, etc. And so it is, in someway, when
someone decides to make the move to a new OS, they have to get used to the new
environment and make new habits, especially regarding Linux. Therefore, I
decided to write this tutorial for my friend, and anybody who decided to try
Linux, and want to learn to use more effectively, that is, with a higher level
of skill than an average user.

<!-- more -->

Most probably you already know, but Linux is not actually the OS itself, but
rather the kernel that is used in conjuction with a collection of programs and
packages called GNU (insert here jokes and memes about GNU/Linux (or
GNU+Linux)), and there are many different distributions (over 9000) of said
GNU/Linux.

If you're just about to install Linux, and haven't decided yet on a distro, here
I wrote a small list of distros that might start with:

* Manjaro - I personally use this distro. This one has different releases, with
  different Desktop Environments (or DEs), like KDE or GNOME. This is a "rolling
  release", what that means, is that instead of the classical update to version
  x.x of the OS system, updates for each package are being rolled out
  constantly, and sepparatly for each package. That is, you get basically the
  latest packages, with the latest updates, at the cost of maybe some stability.
  It is quite easy to get installed, and get started with (If you choose an
  official release like Manjaro KDE or Manjaro GNOME).
* Ubuntu - The most know Linux distribution. It is one of the most
  "user-friendly" distros out there. It is more stable than Manjaro (although
  not always, unless you're using the Long-Term Support versions). If you just
  want to install an OS where everything just works, and is already configured
  for you, and you don't want to choose from a list of DEs (or you don't know
  what a Desktop Environment is), this will most probably suit your needs.
* Debian - One the most stable distributions. It is not as easy to install as
  Ubuntu or Manjaro due to large ammount of choice given during installation,
  but it's not really hard to either. Actually, Ubuntu is derived from Debian.
  You must probably will have to configure it to your needs and likings
  (personally, I dislike the default settings of GNOME), but if you want real
  stability, and distro that has been around almost as long as the kernel
  itself, then this might be the distro for you. One downside of Debian, is that
  because of its focus on stability, many packages on the main (stable)
  branch/version are quite old/outdated. Another con of Debian, is that it
  doesn't include "non-free" packages in the main repository (that is, there are
  no proprietary packages by default), so if need a proprietary program or
  package, like nvidia's drivers, you will have to add the corresponding
  repositories by yourself.
* Fedora - It is similar to Ubuntu regarding stability and ease of use (i.e.
  quite simple to use). I can't say much about Fedora, because I haven't used it
  that much myself, but it is a very popular distribution. It has the same con
  as Debian, in that it doesn't include "non-free" software in its main
  repository.

If you found "Desktop Environment" to be a unfamiliar term, in a nutshell, it is
the collection of programs and packages that present with the Graphical User
Interface for interaction with the system. How your OS/distribution is going to
look like doesn't depend as much on the distro itself, as on the DE that you
choose to install or comes with the distro.

Do you have Linux installed? Excellent, now we can get started.

## File structure

If you are used to working with Windows systems, the the first thing that you
might notice is the difference in how files are handled/organized. In Windows,
because of its DOS legacy, drive letters are used to represent different drives,
partitions and file systems. In Linux, like in other Unix-like systems (e.g.
macOS, BSD) this differs somewhat.

In Linux, everything is a file, including the devices that are connected to your
computer. From your keyboard (which is a read-only file) to your drives.
Directories are also files.

Different disks or drives are mounted in a specific directory, and from this
directory, every file from that drive will be accessible. You can mount and
unmount drives yourself, but if you installed a distro with any of the most
popular desktop environments, then there's nothing to worry about, the system is
going to take care of mounting your pendrive for you, and creating a shortcut in
your file explorer and/or desktop each time you insert it.

Linux has what is called a "root" directory

```
/
```

In there are the files and subdirectories in your system are located, including
drives mounted in a specific subdirectory as mentioned before.

Each user in Linux has its own "home" directory. Inside your home directory your
personal documents/files and subdirectories are going to be stored. All the home
directories of each user are usually going to be stored inside the "/home/"
directory. For example, for user "user", their home directory is going to be

```
/home/user
```

However, you can also move to your home directory by using the symbol ~. For
example, in your terminal, if you input

```sh
$ cd ~
```

You are going to be taken to your home directory (e.g. "/home/user/").

## The command-line - Bash

Arguably the most useful program in Linux and any *nix system is the terminal.
Yes, maybe the average user won't have to use it, but it is the most flexible,
effective and useful instrument in your computer. A lot of work can be done
faster and more effectively in the terminal, rather than in a GUI. Obviously, it
is faster to learn to use a graphical interface, than a text-based or
command-line one, however, once you learn to properly use the terminal, you will
be able to use computer more efficiently than ever.

Your command-line, if you haven't changed any defaults, will most probably look
something like this

```sh
user@host:~$
```

The first part before the "@" symbol, is the user with whom you have logged in.
After the "@" symbol is the "hostname" of your machine, i.e. the name of your
computer in a network.

After the semicolon ":", the directory in which you are currently located is
going to be displayed. In our case, the "~" symbol is displayed, meaning that we
are inside our home directory. If you wish to see the full absolute path in
which you are located, you can input the command "pwd"

```sh
user@host:~$ pwd
/home/user
user@host:~$
```

The dollar "$" symbol, tells us that we are logged in as "normal" user. In Linux
and all *nix systems, there is a so called "super user", or just the "root" user
for short. Normal users don't have full access to all of the files in the
system, including other users' files. The root user has full access to all
system files. When you are logged in as the root user, the dollar "$" symbol is
going to be replaced by the hash "#" symbol.

However, we will be talking about the root user and permissions another time.

I can't tell you exactly how to open the terminal, as it is different in each
distro and desktop environment. You will have to look for the link that says
"Terminal" in your programs' menu, with the icon of a terminal on it.

To be able to start working in the terminal, we'll need to become acquainted
with some of the main commands used for navigating.

### Navigation in the terminal

There is one more thing that needs to be taken into consideration when working
with Linux and Unix-like systems, and that is, case-sensitivity. *nix systems
are case-sensitive, in contrast to Windows ones, which aren't. What that means,
is that, if on Windows, files "README.TXT" and "readme.txt" are the same file,
in Linux and Unix they are completely different files.

To move around directories, we use the "cd" (or change directory) command,
followed by the name, or path of the directory. The directory can be absolute or
relative.

A relative path, is, for example, the name of a subdirectory inside the
directory in which we are currently located. In other words, the path relative
to where we are located.

An absolute path, is the path relative to the root directory.

For example, if we want to move to the "Documents" directory inside
"/home/user/", and we are already at "/home/user/", the we can just type the
following

```sh
user@host:~$ cd Documents
user@host:~/Documents$
```

If we wanted to move to that same directory from another location in the system,
we would type

```sh
user@host:/var$ cd /home/user/Documents
user@host:~/Documents$
```

To move one folder up, we write "..". Example

```sh
user@host:~/Documents$ cd ..
user@host:~$
```

The two dots "..", means the root directory of the current subdirectory. One dot
"." means the current directory.

There is one more command, that will aid you in navigating - ls. This command
outputs a list of files and directories in the current location

```sh
user@host:~/Documents$ ls
Books       todo.txt    picture.png
```

This command also accepts arguments and flags. For example, to list hidden files
as well, add the "-a" flag

```sh
user@host:~/Documents$ ls -a
Books    .secret    todo.txt    picture.png
```

Hidden files in Linux start with a dot (e.g., hidden directory ".secret").

There is also the "-l" parameter, which shows us a list of the contents with
additional information, such as, permissions (more on that in the next part),
amount of files inside a directory, owner of the file (user and group), size on
disk, datetime of creation/modification, and the name of the file. Example

```sh
user@host:~/Documents$ ls -l
drwxr-xr-x  2 user user 4.0K Jul  18 04:20 Books
-rw-r--r--  1 user user 350  Jul  18 04:20 todo.txt
-rw-r--r--  1 user user 1.2M Jul  18 04:20 picture.png
```

You can mix flags as well,

```sh
user@host:~/Documents$ ls -al
drwxr-xr-x  2 user user 4.0K Jul  18 04:20 Books
drwxr-xr-x  5 user user 4.0K Jul  18 04:20 .secret
-rw-r--r--  1 user user 350  Jul  18 04:20 todo.txt
-rw-r--r--  1 user user 1.2M Jul  18 04:20 picture.png
```

You can also take a look at what is inside a directory without having to move to
it first, by passing along as the last argument the name/path of said directory,
for example

```sh
user@host:~/Documents$ ls -l Books
drwxr-xr-x 12 user user 4.0K Jul  18 04:20 Lessons
-rw-r--r--  1 user user 2.3M Jul  18 04:20 Crime and Punishment.pdf
```

### Shortcuts

Before we conclude the first part of this tutorial, I would like to mention some
useful "shortcuts" in bash.

The first one is command history. Each time you input a command into the
terminal, it saves it in a history file. You can move around your command
history by using the up and down arrows. Let's say you want to repeat the last
command you used, instead of typing it all over again, you could just hit the up
arrow one time, maybe modify it a bit, and then press enter to input it. If you
want to use an older command, you can press the up arrow multiple times, and if
you missed the command you needed, you can go forward in your history by
pressing the down arrow.

There is one more useful thing in Bash - Tab completion. When you press the
"Tab" key, bash will try to autocomplete the command for you.

Let's say, for example, that you are in the root directory and you want to move
to "/home/user/". you can start typing "cd h", then press "Tab", so you now have
"cd home/", now type "u", press "Tab" one more time, and now you have "cd
home/user/".

If there are multiple possible options to be autocompleted, then on the first
hit of the "Tab" key you are not going to get anything. If you don't get
anything it could also mean that there are nothing to autocomplete. In the first
case, if you press "Tab" twice, bash is going to output a list of possibilities,
for example

```sh
user@host:~$ cd Do
Documents/ Downloads/
user@host:~$ cd Do
```

In this case, we can type the next letter, for example "cd Doc", and press "Tab"
one more time, and we are going to get "cd Documents/". However, if you didn't
get anything on pressing "Tab" twice, it means that is nothing to autocomplete.

It can be a little be hard to understand at first how "Tab completion" works,
but the best way to understand it, by trying it yourself.

That is all for now, in the next part I am going to talk more about working on
the terminal.