An introduction to Linux (Ubuntu) basic commands.
Tags: hagital, beginners, tutorial
Published: 2023-06-30 00:37:20

**This article is made to serve as an elementary introduction to Linux Operating System. It is designed to help a beginner understand the structure and working of Linux as an Operating System using Command Line Interface (CLI).**

The first challenge that most individuals aspiring to become Enterprise Server Administrators, Cloud Developers, DevOps Engineers and other related fields encounter is the fact that most servers are running on **Linux Operating Systems** as against the more popular **Windows Operating System** mostly used in personal computers. 

Therefore, it becomes imperative for such an individual to have an understanding of how Linux works, its basic commands, packages and structure. This is what this writeup set out to achieve.   

**WHAT IS LINUX?**

Linux is an open-source operating system (OS). It was originally conceived of and created as a hobby by Linus Torvalds in 1991. Linus, while at university, sought to create an alternative, free, open-source version of the MINIX operating system, which was itself based on the principles and design of Unix. 

One of the key features of Linux is its open-source nature, which means that its source code is freely available for anyone to view, modify, and distribute.

Linux provides a powerful and stable platform for running software and applications. It is known for its robust security features, customizability, and scalability. The operating system supports a wide range of programming languages and frameworks, making it a favored choice among developers.

**Linux Distribution**
A Linux distribution, often shortened to Linux distro, is an operating system compiled from components developed by various open-source projects and programmers. Each distribution 
includes the **Linux kernel** (the foundation of the operating system), the **GNU shell utilities** (the terminal interface and commands), the **X-server** (for a graphical desktop), the **desktop environment**, a **package management system**, an **installer** and other services. 

**Some popular Linux distros:** 
**Red Hat Enterprise Linux & CentOS**, **Fedora**, **Debian**, **Ubuntu**, **OpenSUSE**, **Linux Mint** and others. Recall that Linux is open-sourced, so we have many distributions that have been developed overtime, though they all have the same underlying structure and concept.  

For this very article, we will be using the **Ubuntu** Linux distribution.

**BASIC LINUX COMMANDS**
Let us take a look at some basic Linux commands and concepts.

**Shell prompt:**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jrsud2n5ttfl0ptr08ql.PNG)
This is called the **SHELL prompt**. `username@machinename `followed by the current working directory and a $ sign. This is the first thing that displays when you open your terminal. The $ sign implies the shell is ready for input. 

**Moving around the command line:**

If we press the up-arrow key, we will see the previous command. 
Press the down-arrow key and the previous command disappears. 
We can position the cursor anywhere on the command line by using the left and right-arrow 
keys. This makes editing commands easy. 
If you highlight some text by holding down the left mouse button and dragging the mouse over it, it will COPY the text. Pressing the right mouse button will cause the text to be PASTED at the 
cursor location.

**Navigation:** 

Here we look at how to navigate the file system on our Linux system. Linux organizes its files in what is called a hierarchical directory structure. This means that they are organized in a tree-like pattern of directories (sometimes called folders in other systems), which may contain files and 
other directories. The first directory in the file system is called the root directory. The root directory contains files and subdirectories, which contain more files and subdirectories and so on. 


**General Commands**

The current working directory `pwd` 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6s4gsqduw0h2ak2c43ai.PNG)
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, notice that the path stems from the root directory. 

**/** means root directory 
To switch to root user, we use the command `sudo su` (super do switch user).

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oydvawz494nwkucsv9bz.PNG)
Type `exit` and hit the enter button to switch from root user.
 
**~** means current user’s home directory

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vdtdoyetrxy9x2rc0ld6.PNG)

**apt** package manager of **Ubuntu** used for installation of applications and updates. Example, **`apt install nginx`** to install nginx in ubuntu Linux.

**Listing the content of a directory**
These commands are used to view the content of a directory you are working on.

**ls** short list of the content only 
**ls -l** detail list of the content 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gc6arqgs1wp2nh9plg7i.PNG)

**ls -a** list content with hidden folders 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rkduer0ug2zg5mkefi8j.PNG)

**ls -la** list content with details, permissions and hidden folders.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w3szqj762rtwczar5be0.PNG)


**Changing Working Directory cd**

**cd** User Home directory (cd takes you from anywhere to the current user’s home directory)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0w2ka79xe6ud8dzcvjt4.PNG)

**cd /** Root directory 
Takes you to the Root directory from your current directory.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1ywfwsgiwq4l3ncvtu1v.PNG)

**cd .** Move back to current/present directory that you are working in. 


**cd ..** Move back to current directory's parent directory

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6ajxmdys9iw6jowaq0qi.PNG)

**Directories (Folder) & File**
**mkdir**: to make a directory (folder) `mkdir foldername`
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o4msjdpjqw00ta2o5wkt.PNG)
We created the directory and used the **ls** command to view it.
 

 **rmdir**: To delete an empty directory we use this command `rmdir foldername` 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g6vjkp6jo2gfwgaguna2.PNG)
 
**touch**: We use this to create new empty files **`touch 
“filename”`**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4vj6zq952nud06degku2.PNG)
We used the command to create a file named file2, and viewed it with the **ls** command.
Touch is also used to change timestamps on existing files and directories. 

**rm**: to remove a file we use this command this way `rm filename`. Let us use it to delete file2 that we just created.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mjl615g1mpfpmkconnn3.PNG)
We used the command to remove the file and used the **ls** command to show that the file is no longer available.

**echo**: This command is used to print or write into a file. Example, `echo “Welcome to Linux” > filename ` This will write "Welcome to Linux" to the file with the designated name.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uf5ijgdqwkp9jgnkgicz.PNG)
We just used the command to write "Welcome to Linux" in a the file **ben** and viewed the content of the file with the **cat** command.
If a file name does not exist, echo can create the file and write the content into it.
 
**cat**: short for concatenate used to display a file content.  `cat filename` as used above.


**Linux File Editors** 

We use these commands to edit files..
**vi** 
**vim**
**nano**

To use the **vi** command, type `vi filename` to edit an existing file or create a new file with the designated file name and write a content in it. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8okr6yhjdzpqy61ud9jz.PNG)

This will take us into the file to edit it.
Press **I** to insert the content.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k2rvayjiz3yj4ptqqu3l.PNG)

After editing Press ESC to exit insert mode. 
Then use “**:wq!**” to save and exit editor or “**:q!**” to exit editor without saving.

The **nano** command functions in similar way to the **vi** command.


**Let us use the `vim` command to create a text file and save it in a directory (folder) named "myfutureself"**

- Create the directory "myfutureself" with **mkdir** command.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q8k2pmapcpd46eeubych.PNG)

- Use command `cd myfutureself` to get into the file we created.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a1u1olsyyp34auerdebe.PNG)
We are now inside the new directory (myfutureself). Let us go ahead and create the file.

- Use the vim command to create a file named **congratulation**.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bqtfbrpsywop11mv3bqj.PNG)
This brings us inside the file **congratulations**
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9uy7fiduidhusm2o1b21.PNG)
Press **I** to insert the content (Write in the file).

**The file content**
This file is a congratulatory letter to my future self.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vvyguwjoizevmg671g5a.PNG)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cw9lssusv0r39z2s0l3r.PNG)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8xnr31jbkeem0yeeym16.PNG)

- After editing Press **ESC** key to exit insert mode. Then use “**:wq!**” to save and exit editor, by hitting the **enter** key.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/10zqyznta52gcmt2nod6.PNG)

Back to **shell prompt**.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p362uhx47b8802zitugr.PNG)

- View the file with the **ls** command, and the file content with the **cat** command.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hizmtezrzxcvijmrkgfx.PNG)

**File Content viewed with `cat` command.**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kfchqai7ai74yy8cnzdg.PNG)

Hope this guide helped you get started with understanding the **Linux Operating System**.

**Thanks for your time, please subscribe and like my page.**