Intro to Linux and the Bash command line
Published Nov 05, 2018 23:53 UTC on Yaroslav's weblog
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.
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.
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
However, you can also move to your home directory by using the symbol ~. For example, in your terminal, if you input
$ 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
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"
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
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
user@host:/var$ cd /home/user/Documents user@host:~/Documents$
To move one folder up, we write "..". Example
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
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
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
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,
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
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
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
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.