Introduction to Linux¶
Here we are going to teach you how to interact with a Linux machine. There are two ways that a computer can be controlled; the first is through the graphical user interface (GUI), which is most likely how you navigated to this page, and the second is via the command line. Which instead of clicking and dragging, involves issuing written commands to an interactive terminal that are then executed and the output is displayed to you in text.
On macOS and Linux, open the Terminal App, on Windows open CMD. Opening a terminal should give you a prompt like this
Where,- Bennu = hostname
- ~/Documents = current location
- Osiris = username
Files and directories¶
Everything in Linux is either a file or directory (folder). A file is basically a chunk of data stored on a hard drive with a name to place it in the file system. Directories only contain data on the files and directories that are in them. To give you an idea of this, a basic outline of the Linux file system is given below:
/
├── bin
├── boot
├── dev
├── etc
├── home
├── Osiris
├── file.py
├── Documents
├── cool-stuff.py
├── Downloads
├── Music
Navigating¶
First we need to figure out where we are, to do so, use the pwd
(present working directory) command.
~
and it can be used as a substitute for the above.
To see what is in this directory use the ls
command.
-l
(long) flag to see more flags. Use man ls
to read the manual.
% ls -l
total 0
drwx------@ 27 Osiris staff 864 11 May 12:53 Desktop
drwx------@ 37 Osiris staff 1184 12 May 16:55 Documents
drwx------+ 12 Osiris staff 384 6 Oct 2021 Music
ls
.
You can also call files relative to home using the ~
key, e.g. ls ~/Downloads
.
Three more special keys are the .
, ..
, and -
. The first two allow you to move relative to the current directory so instead of writing ~/current/working/directory/file
, you can simply write ./file
. Similarly, if the file was in the directory above, i.e. ~/current/working/file2
, we can write ../file2
. These are useful when you want to execute scripts. Finally, the -
key allows us to navigate back to our previous directory.
% pwd
/home/Osiris/Documents
% cd ../Music
% pwd
/home/Osiris/Music
% cd -
% pwd
/home/Osiris/Documents
Play around with these commands to navigate through your computer. Once you feel comfortable getting around this way, proceed to the next section.
Creating a new directory¶
This action is the same as the "New Folder" action when using a file browser.
Learning more about commands¶
Linux has two ways that we can learn more about a command within the terminal. The first is using Manual pages, man
, these contain all of the information that you will need to effectively use any command.
% man vim
VIM(1) General Commands Manual VIM(1)
NAME
vim - Vi IMproved, a programmer's text editor
SYNOPSIS
vim [options] [file ..]
vim [options] -
vim [options] -t tag
vim [options] -q [errorfile]
ex
view
gvim gview evim eview
rvim rview rgvim rgview
DESCRIPTION
Vim is a text editor that is upwards compatible to Vi. It can be used to edit all
kinds of plain text. It is especially useful for editing programs.
There are a lot of enhancements above Vi: multi level undo, multi windows and
buffers, syntax highlighting, command line editing, filename completion, on-line
help, visual selection, etc.. See ":help vi_diff.txt" for a summary of the
differences between Vim and Vi.
While running Vim a lot of help can be obtained from the on-line help system, with
the ":help" command. See the ON-LINE HELP section below.
...
--help
flag after the command.
% vim --help
VIM - Vi IMproved 8.2 (2019 Dec 12, compiled Feb 12 2022 04:26:50)
Usage: vim [arguments] [file ..] edit specified file(s)
or: vim [arguments] - read text from stdin
or: vim [arguments] -t tag edit file where tag is defined
or: vim [arguments] -q [errorfile] edit file with first error
Arguments:
-- Only file names after this
-v Vi mode (like "vi")
-e Ex mode (like "ex")
-E Improved Ex mode
-s Silent (batch) mode (only for "ex")
-d Diff mode (like "vimdiff")
-y Easy mode (like "evim", modeless)
-R Read only mode (like "view")
...
Viewing files¶
If you want to view the contents of a file there are two commands that you can use to do so. The first is better suited to small files.
The second method calls up an interactive pager, that displays over the entirety of your terminal window. It will also allow you to scroll through and search for text. Use thej
and k
keys to scroll, and when you are ready to quit press q
.
Again, please take the time to explore your computer and view the contents of files on your system before proceeding.
Deleting, copying, and renaming and moving¶
The command to copy files is cp
, to copy a file run the copy command followed by the name of the file that you want to copy and the name that you want the copy to have. This command is handy when you'd like to try something new with your code, but you're worried about breaking it.
In Linux, moving something and renaming it are the same thing. To do this use the mv
command, this command takes two arguments: the path of the file to move/rename and the new path/new name.
To delete a file we use the rm
command.
Danger
The rm
command is not like deleting files using a GUI! This command does not move files to the Bin, instead it permanently deletes them, there is (almost) no way to recover them.
Building code¶
The code that we write in programming languages is designed to be easy to read for humans. However, computers are unable to understand this, they only understand what is written in binary. So we need to convert our code to binary, to do this we use a compiler. An example of a C compiler is gcc
, to compile code using this we write:
% cat fun.c
#include <stdio.h>
int main() {
printf("Wow, coding is fun!\n");
return 0;
}
% gcc fun.c
% ls
a.out fun.c
Copy the code above and try to compile the code yourself.
Permissions and running code¶
To run code, we simply call it in the command line.
Now, this may not have ran for you. The reason for this is permissions. Remember early when we ranls -l
, that there was at the beginning of a line something like this: -rw-------
? This is telling us several things. This first character will be either a -
for a file, or a d
for directories - so the above string relates to a file. Following the first character are the remaining characters which have three characters for three groups of people: 1) the owner of the file, 2) users in the group of the file, and 3) everyone else. The three groups of characters for each group of people tells us if they can read (r
) the file, write (w
) to the file, and execute (x
) the file. So in the case above, only the owner of the file can read and write to its contents.
In order to run code, we need to be allowed to execute it. To change our permissions we can use the chmod
command. To let anybody execute the file a.out
we can run:
Extra information
If you'd like to learn more about the options for the chmod
command, I encourage you to read its manual using man
.
As a sidenote, the command to change the ownership of a file is chown
and again if you'd like to know more about it, use its man
page.
Redirection of output¶
Now that you know how to run a few basic commands and programs, I think it is a good time to teach you how we can redirect their output. The default behaviour is to display their output to stdout
so that we can see it. However, we are also able to redirect the output to a file. To do this we use the >
operator followed by the file we want to store the output in. Note, the echo
command just outputs what you put into it.
% echo "Wow! This guide has been super helpful for me :)" > tootrue.txt
% cat tootrue.txt
Wow! This guide has been super helpful for me :)
>>
, this operator will append the output to a file if it already exists, otherwise it is the same as >
.
% echo "I agree!" >> tootrue.txt
% cat tootrue.txt
Wow! This guide has been super helpful for me :)
I agree!
The last kind of redirect, redirects the output between programs. The operator for this is a pipe, |
.
% ls -l /
total 108
drwxr-xr-x 2 root root 4096 Sep 11 06:37 bin
drwxr-xr-x 4 root root 4096 Sep 19 06:59 boot
drwxr-xr-x 19 root root 4000 Sep 4 15:07 dev
drwxr-xr-x 186 root root 12288 Sep 19 06:56 etc
drwxr-xr-x 43 root root 4096 Sep 4 15:33 home
lrwxrwxrwx 1 root root 33 Sep 19 06:58 initrd.img -> boot/initrd.img-4.15.0-64-generic
lrwxrwxrwx 1 root root 33 Sep 19 06:58 initrd.img.old -> boot/initrd.img-4.15.0-62-generic
drwxr-xr-x 25 root root 4096 Sep 4 16:09 lib
drwxr-xr-x 2 root root 4096 Sep 4 16:09 lib32
drwxr-xr-x 2 root root 4096 Jul 25 2018 lib64
drwx------ 2 root root 16384 Sep 4 13:21 lost+found
drwxr-xr-x 2 root root 4096 Jul 25 2018 media
drwxr-xr-x 2 root root 4096 Jul 25 2018 mnt
drwxr-xr-x 6 root root 4096 Sep 18 16:47 opt
dr-xr-xr-x 259 root root 0 Sep 4 14:13 proc
drwx------ 5 root root 4096 Sep 10 15:55 root
drwxr-xr-x 41 root root 2420 Sep 19 15:14 run
drwxr-xr-x 2 root root 12288 Sep 19 06:56 sbin
drwxr-xr-x 4 root root 4096 Sep 4 13:28 snap
drwxr-xr-x 2 root root 4096 Jul 25 2018 srv
dr-xr-xr-x 13 root root 0 Sep 4 20:10 sys
drwxrwxrwt 80 root root 12288 Sep 19 15:09 tmp
drwxr-xr-x 12 root root 4096 Sep 4 16:09 usr
drwxr-xr-x 19 root root 4096 Sep 4 15:05 var
lrwxrwxrwx 1 root root 30 Sep 19 06:58 vmlinuz -> boot/vmlinuz-4.15.0-64-generic
lrwxrwxrwx 1 root root 30 Sep 19 06:58 vmlinuz.old -> boot/vmlinuz-4.15.0-62-generic
% ls -l / | grep lib
drwxr-xr-x 25 root root 4096 Sep 4 16:09 lib
drwxr-xr-x 2 root root 4096 Sep 4 16:09 lib32
drwxr-xr-x 2 root root 4096 Jul 25 2018 lib64
ls
command into the grep
command. Where grep
restricts the output only to lines that contain a pattern, in this case "lib".
Remote access with SSH¶
The command ssh
is used to access other machines connected across a network, including the internet. To call ssh
we write:
Depending on the frequency that you'll be accessing the machine, it could quickly become tiresome to keep entering the password. Fortunately, there is a way to avoid this, and that is SSH Keys. This allows you to authenticate by using an ssh key pair, which are a pair of text files with special data. On your own computer, you can generate them using the following.
% ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/Osiris/.ssh/id_rsa):
Created directory '/home/Osiris/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/Osiris/.ssh/id_rsa.
Your public key has been saved in /home/Osiris/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:zJ2SQwYgAS4x9kKXthtPetW+2De1GttGHfUACfYVTIU Osiris@Bennu
The key's randomart image is:
+---[RSA 2048]----+
|++ooo.. o..=++.|
|+oo+ . . ...E .|
|.o... + . .o|
|. .o . * + . ..|
| * . S o . .|
| o o + o . |
| . o ..o . |
| . o o+o |
| .o+. |
+----[SHA256]-----+
~/.ssh/id_rsa
, which is your private key and should never be moved, and ~/.ssh/id_rsa.pub
, which is your public key and can be copied to any machine you like. You can put your public key on another computer using this command.
This will place your public key onto the host computer, so that next time when you try to connect ssh will see your public key on the computer and check for the private key on your computer. Which means that you'll no longer need to enter your password when connecting.
Other useful commands¶
Command | Use |
---|---|
rsync | Copy files and directories between machines using ssh |
ps | List processes running |
kill | Stop process running |
head | Display top x lines of file, 10 by default |
tail | Display bottom x lines of file |
where | List the path to command's location |
whichis | If multiple versions, list all their paths |
apropos | Searches man pages by keyword, useful to find new commands |
Next step¶
Once you feel comfortable with the contents of this page, you can continue on
to the next page: GitHub - Part 1. If there is anything that you don't feel
confident using please return to that section and practice it some more.
Remember, if you ever need more information on how a command works, you can read
its manual using man {CMD}
.