howtuwu/content/linux/terminal.md
2022-01-17 14:35:39 -06:00

9.4 KiB

title
The Terminal

Note: This guide uses Kubuntu. Other Linuxes may have different apps, but the general ideas are the same.

Introduction

Sooner or later, you're going to have to open up the command line. This is Linux after all! But don't worry, it's actually super easy! Let's get started. Open up the Konsole app and get ready to plunge into the exciting world of the command line.

Konsole

Orientation

Alright, let's get started. You should see a black window with a single line like:

ta180m@kubuntu:~$

Let's break down what's going on here. The first word is my username, ta180m. The second word after the @ is my computer's name kubuntu. Then, there's a colon, tilde, and a dollar sign. Whoa! What's the meaning behind this gibberish?

The colon is simply a separator. The tilde is more interesting: Anytime you use the command line, you are always in some folder. In this case, you are in your home folder, which is nicknamed ~. Make sure you remember this. We'll get to the dollar sign later.

Let's try typing a command! After all, this is the command line, right?

Moving around

We will start with pwd. This command stands for "print working directory (folder)". This will print which folder we are currently in. Try it out!

ta180m@kubuntu:~$ pwd
/home/ta180m

In my case, my home folder is located at /home/ta180m. Yours will be very similar. All regular users have their home folders located in the /home folder.

You can list the files and folder in your current folder with ls, short for list.

ta180m@kubuntu:~$ ls
Desktop  Documents  Downloads  Music  Pictures  Public  Templates  Videos

As you can see, I have a few automatically created folders in my home folder. We can also tell ls which folder we want to see the contents for.

ta180m@kubuntu:~$ ls /home
ta180m

I'm the only (regular) user on this computer, so there's only one folder in /home.

Note the initial slash in /home. The folder / is called the root. This is the folder that all the folders and files in your computer are in. We can also list the contents of /.

ta180m@kubuntu:~$ ls /
bin    dev   lib    libx32      mnt   root  snap      sys  var
boot   etc   lib32  lost+found  opt   run   srv       tmp
cdrom  home  lib64  media       proc  sbin  swapfile  usr

We'll ignore these cryptic folders for now. Anyways, what if we want to set out current folder to a different one? Thankfully, there's the nice and short command cd that will let us do this. For example, let's say I'm in my home folder and I want to enter one of the folders in my current folder, say, the Downloads folder. It's as simple as

ta180m@kubuntu:~$ cd Downloads
ta180m@kubuntu:~/Downloads$

Notice that the text between the colon and the dollar sign is now ~/Downloads instead of ~, signifying that I'm now in the Downloads folder.

You can also change your current folder to the root folder, /. You can probably already guess the command.

ta180m@kubuntu:~$ cd /
ta180m@kubuntu:/$

What about the going to the folder that my current folder is inside? Let's go to the folder above our current folder. You'll need to know that the folder you are currently in can also be referred to as . and the folder containing your current folder is ...

ta180m@kubuntu:~$ cd ..
ta180m@kubuntu:/home$

Making stuff

You can make a new folder with mkdir. For example, let's say I want to make a folder for all my code called code.

ta180m@kubuntu:~$ mkdir code
ta180m@kubuntu:~$ cd code
ta180m@kubuntu:~/code$ ls
ta180m@kubuntu:~/code$ pwd
/home/ta180m/code

Did you understand what just happened? You can also open up Dolphin, the file manager, to take a look around your folders.

You can create a new file with touch.

ta180m@kubuntu:~/code$ touch main.py

You can edit this file with a graphical text editor, like Kate, the preinstalled editor, or with an editor in your terminal. One such easy-to-use editor is nano. Open up the file and write something! You can exit when you're done by pressing Ctrl-X. (nano abbreviates Ctrl as ^)

ta180m@kubuntu:~/code$ nano main.py

Let's view the file! This uses a funny command, cat. It'll print out what you just wrote.

ta180m@kubuntu:~/code$ cat main.py
print('The command line is so fun!')

Superuser!

Try making a file in the root folder, /.

ta180m@kubuntu:~$ cd /
ta180m@kubuntu:/$ touch test
touch: cannot touch 'test': Permission denied

Whoa, what? I can't?

Nope. Only a very special user can edit them: root.

This is not the same root as the root folder, /. The root user is something completely different. Also known as the superuser, the root user has complete power over the computer. They can do anything, even delete all the computer's system files, and nothing will stop them.

Sounds cool! It's usually not a good idea to log in as the root user, since you might randomly mess up an important system file and screw up your computer.

However, sometimes, we do need root privilege for doing system modifications like installing software. To do that, we can use sudo.

sudo!

As the xkcd suggests, by inserting sudo before a command, no one will stop us now!

ta180m@kubuntu:/$ touch test
touch: cannot touch 'test': Permission denied
ta180m@kubuntu:/$ cat test
cat: test: No such file or directory
ta180m@kubuntu:/$ sudo touch test
ta180m@kubuntu:/$ cat test

It works now!

Managing software

The Kubuntu app store, Discover, is pretty awesome. Unfortunately, it only has graphical apps, so if you want to install something like the gcc compiler for writing some fun C++, you're out of luck.

However, the command line offers us an easy solution! In Kubuntu, regular Ubuntu, and Debian, the command apt manages software. (Other Linuxes may use different commands)

Before you get started, you'll need to update the catalog of software. This ensures that the catalog is up to date. Note the use of sudo since you'll be modifying your system's software.

ta180m@kubuntu:~$ sudo apt update
Hit:1 http://us.archive.ubuntu.com/ubuntu hirsute InRelease
Get:2 http://security.ubuntu.com/ubuntu hirsute-security InRelease [101 kB]
Get:3 http://us.archive.ubuntu.com/ubuntu hirsute-updates InRelease [109 kB]
Get:4 http://us.archive.ubuntu.com/ubuntu hirsute-backports InRelease [101 kB]
Fetched 310 kB in 1s (615 kB/s)    
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
All packages are up to date.

As you can see here, all my packages (software) are up to date. However, if they aren't, I can easily update all the software on my computer with one command. Now you see the power of the command line!

ta180m@kubuntu:~$ sudo apt upgrade
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
Calculating upgrade... Done
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.

Welp, looks like nothing was out of date so nothing got updated.

Anyways, let's actually install something. How about, neofetch, a cool command-line tool to report your system specs?

ta180m@kubuntu:~$ sudo apt install neofetch
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following NEW packages will be installed:
  neofetch
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 81.3 kB of archives.
After this operation, 359 kB of additional disk space will be used.
Get:1 http://us.archive.ubuntu.com/ubuntu hirsute/universe amd64 neofetch all 7.1.0-2 [81.3 kB]
Fetched 81.3 kB in 0s (373 kB/s)    
Selecting previously unselected package neofetch.
(Reading database ... 205472 files and directories currently installed.)
Preparing to unpack .../neofetch_7.1.0-2_all.deb ...
Unpacking neofetch (7.1.0-2) ...
Setting up neofetch (7.1.0-2) ...
Processing triggers for man-db (2.9.4-2) ...

Now let's enjoy our newly installed package! (It's much better with color)

ta180m@kubuntu:~$ neofetch
           `.:/ossyyyysso/:.               ta180m@kubuntu 
        .:oyyyyyyyyyyyyyyyyyyo:`           -------------- 
      -oyyyyyyyodMMyyyyyyyysyyyyo-         OS: Kubuntu 21.04 x86_64 
    -syyyyyyyyyydMMyoyyyydmMMyyyyys-       Host: KVM/QEMU (Standard PC (Q35 
   oyyysdMysyyyydMMMMMMMMMMMMMyyyyyyyo     Kernel: 5.11.0-22-generic 
 `oyyyydMMMMysyysoooooodMMMMyyyyyyyyyo`    Uptime: 1 hour, 47 mins 
 oyyyyyydMMMMyyyyyyyyyyyysdMMysssssyyyo    Packages: 1924 (dpkg) 
-yyyyyyyydMysyyyyyyyyyyyyyysdMMMMMysyyy-   Shell: bash 5.1.4 
oyyyysoodMyyyyyyyyyyyyyyyyyyydMMMMysyyyo   Resolution: 1024x768 
yyysdMMMMMyyyyyyyyyyyyyyyyyyysosyyyyyyyy   DE: Plasma 5.21.4 
yyysdMMMMMyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy   WM: KWin 
oyyyyysosdyyyyyyyyyyyyyyyyyyydMMMMysyyyo   Theme: Breeze Light [Plasma], Br 
-yyyyyyyydMysyyyyyyyyyyyyyysdMMMMMysyyy-   Icons: breeze [Plasma], breeze [ 
 oyyyyyydMMMysyyyyyyyyyyysdMMyoyyyoyyyo    Terminal: konsole 
 `oyyyydMMMysyyyoooooodMMMMyoyyyyyyyyo     CPU: AMD EPYC-Milan (4) @ 3.693G 
   oyyysyyoyyyysdMMMMMMMMMMMyyyyyyyyo      GPU: 00:01.0 Red Hat, Inc. QXL p 
    -syyyyyyyyydMMMysyyydMMMysyyyys-       Memory: 783MiB / 3926MiB 
      -oyyyyyyydMMyyyyyyysosyyyyo-
        ./oyyyyyyyyyyyyyyyyyyo/.                                   
           `.:/oosyyyysso/:.`