Linux OS Basics – Part 1

To Begin

History

Hey. My name is Ismail, I’ll be guiding you throughout this series of Linux OS basics. With a little bit of a backstory, we will get started.

In 1969, Ken Thompson and Dennis Ritchie of Bell Laboratories developed the UNIX operating system. To make it more portable, It was initially written in C and then eventually it became a widely used operating system. Then in 1991, Linus Torvalds started developing a most important piece of the operating system called Kernel. Just so you know that the Kernel controls pretty much everything that happens on your system.

Linux Distribution

A Linux system is divided into three main parts:

  • Hardware – All the hardware that runs on your system, as well as memory, CPU, disks, etc.
  • Linux Kernel – This manages the hardware and tells it how to interact with the system.
  • User Space – This is where users like yourself will be directly interacting with the system.

There are many distributions available in the Linux world. Some of them are listed below and we’ll just go over the most popular options.

Debian

Debian is one of the oldest operating systems based on the Linux Kernel. Since its founding, Debian has been developed openly and distributed freely which ideally means it’s an open-source project.

Every Linux distribution installs and manages packages differently and they use different package management tools. We’ll get more into this in a later course.

Red Hat Enterprise Linux

RHEL-based operating systems will differ slightly from the Debian-based operating systems. RHEL has strict rules to restrict free re-distribution although it still provides source code for free.

Ubuntu

One of the most popular Linux distributions for personal machines is Ubuntu. Ubuntu is a Debian-based operating system.

Fedora

Fedora Project is community-driven containing open-source and free software. Red Hat Enterprise Linux branches off Fedora, so think of Fedora as an upstream RHEL operating system.

Fundamentals of Command Line

Shell

Throughout this entire course, we will be learning about what the shell can do and we will also see the beauty of the shell. Shell is actually a program that takes your command and instructs the operating system to perform it for you. If you’ve ever used a GUI, you’ve probably seen programs such as “Terminal” or “Console” these are just programs that launch a shell for you.

Let’s jump right in! Depending on the distribution your shell prompt might change, but for the most part it should adhere to the following format:

username@hostname:current_directory
pineturtle@DESKTOP-ESO0AIU:~/temp$

Let’s try a simple command, echo. This echo command will just print the text inputs to the display.

$ echo Hello World

pwd – Print Working Directory

In Linux, everything is a file and each of them is organized in a hierarchy. It is referred to as a directory tree. Here is an example of what a directory tree looks like.

Notice the command “ls -la -d */” . This is a command to list only the directories under “/” (root) directory and sort by ascending order. Will talk about ls command later in detail

 To see where you are, you can use the pwd command, this command means “print working directory” and it just shows you which directory you are in, note the path stems from the root directory.

$pwd

cd – Change Directory

From the previous command, you know how to locate your working directory. Now let’s try to change the directories and traverse between folders.

There are two different ways to specify a path, with absolute and relative paths.

  • Absolute path: This is the path from the root directory. The root directory is ” / “.
  • Relative path: This is the path from where you are currently in the filesystem. For instance, If I am in /home/pineturtle/temp and I want to navigate to a folder inside temp, I can simply type that folder name and change it, I do not have to specify the whole path from “/”.

There are also shortcuts to help you traverse through the directories.

  • . (current directory). This is the directory you are currently in.
  • .. (parent directory). Takes you to the directory above your current.
  • ~ (home directory). This directory defaults to your “home directory”. Such as /home/pineturtle.
  • – (previous directory). This will take you to the previous directory you were just at.

Go ahead and try it out yourself. You would get a clear picture of what we discussed here.

Try the below examples:

$mkdir -p /home/pineturtle/temp/temp1/temp2
$cd 

ls – List Directories

Now we know how to move around the directories, we’ll use some ls command. Remember we used an example when we used the “pwd” command. It is going to be the same.

The ls command will list directories and files in the current directory by default, however, you can specify which path you want to list the directories of.

$ls
$ls /home/pineturtle

We have a useful ls flag, -l for long, which shows a detailed list of files in a long format.

We get to see detailed information, starting from the left: file permissions, number of links, owner name, owner group, file size, timestamp of last modification, and file/directory name.

pineturtle@DESKTOP-ESO0AIU:/$ ls -l
total 684
lrwxrwxrwx   1 root root      7 Apr 23  2020 bin -> usr/bin
drwxr-xr-x   2 root root   4096 Apr 23  2020 boot
drwxr-xr-x   9 root root   2760 Apr 27 19:02 dev
drwxr-xr-x  92 root root   4096 Apr 27 19:02 etc
drwxr-xr-x   3 root root   4096 Apr 27 00:22 home
-rwxr-xr-x   2 root root 632096 Mar 24 16:47 init

Now it’s your turn, Go ahead and give it a try.

cat – concatenate

Navigating within directories will eventually become your daily activity. Now let’s learn how to read files.

cat is a command used to display the contents of the file and it can also combine multiple files and display their output of them. Go ahead and try the commands in the below screenshot.

pineturtle@DESKTOP-ESO0AIU:/tmp$ echo "this is a statement in file1" > file1.txt
pineturtle@DESKTOP-ESO0AIU:/tmp$ echo "this is a statement in file2" > file2.txt
pineturtle@DESKTOP-ESO0AIU:/tmp$ cat file1.txt
this is a statement in file1
pineturtle@DESKTOP-ESO0AIU:/tmp$ cat file2.txt
this is a statement in file2
pineturtle@DESKTOP-ESO0AIU:/tmp$ cat file1.txt file2.txt
this is a statement in file1
this is a statement in file2

It doesn’t really create a file when you use cat against file1.txt and file2.txt, It only displays the content in those files. Just above this command, we have used cat for each file. Instead of using cat for each file, you can use cat to combine and display the output. Do remember, Although we can view content in a file using cat, this command is definitely not great to read through large files.

less

You know, if you want to view large text files, then a simple less command is more. The text is displayed in a paged manner, so you can navigate through a text file page by page.

Now, go ahead and try the following.

pineturtle@DESKTOP-ESO0AIU:/tmp$ cd /tmp
pineturtle@DESKTOP-ESO0AIU:/tmp$ wget -O lftsample.txt shorturl.at/hyzX3
pineturtle@DESKTOP-ESO0AIU:/tmp$ less lftsample.txt

Now you can use the following command to navigate through less:

  • q – Used to quit out of less and go back to your shell.
  • Page up, Page down, Up and Down – Navigate using the arrow keys and page keys.
  • g – Moves to the beginning of the text file.
  • G – Moves to the end of the text file.
  • /search – You can search for specific text inside the text document. Prefacing the words you want to search with /
  • h – If you need a little help about how to use less while you’re in less, use help.

or you can use “less –help” command to see what options you got.

mkdir

Let’s go ahead and create some directories.

pineturtle@DESKTOP-ESO0AIU:/tmp$ mkdir folder1

You can also create subdirectories at the same time with the -p (parent flag).

pineturtle@DESKTOP-ESO0AIU:/tmp$ mkdir -p folder1/folder2/folder3

Now navigate through the created folders using “cd” command and you can see how the folders are created.

rm

This command is used to remove files and directories.

Delete files and folders using the below command. Replace “folder1/ ” with your filename or the folder name that you want to delete.

pineturtle@DESKTOP-ESO0AIU:/tmp$ rm -rf folder1/

you can also remove a directory with “rmdir” command. Go ahead and try it.

exit

If you are still here practising, I am sure you did love the content and best of all, you are doing a good job in getting through the command line basics.

For now, you can pat yourself on the back and take a break. To exit from the shell, you can use the exit command

pineturtle@DESKTOP-ESO0AIU:/tmp$ exit

Or the logout command:

pineturtle@DESKTOP-ESO0AIU:/tmp$ logout

Or if you are working out of a terminal GUI, you can just close the terminal.

See you at the next part 🙂

Leave a comment