An Introduction to Linux

An Introduction to Linux

This is the first blog of the ongoing Linux Masterclass Series.

What happens when you start a computer?

The first thing your computer does after starting is to run the BIOS (Basic Input-Output System). In other words, it executes the software that is in BIOS. BIOS, also known as firmware, is installed by default in the ROM (Read-only Memory) chip in the CPU motherboard. That software checks if the system is working correctly or not. It will check if the memory is loading, if the RAM and Hard Disk are there, the devices connected to the system, whether the keyboard is connected, and many other preconditions to launching the Operating System.
Once the checks are over, it loads the Bootloader that starts the Operating System. A Bootloader is a computer program responsible for starting up the operating system.

What is an Operating System?

An Operating System or OS is the software responsible for managing the computing device and acts as an interface between the hardware and the user. The computing device can be a desktop, smartphone, ATM etc.
For software to be considered an OS, it needs a few pre-requisites:

  1. Kernel: A Kernel is the core of any operating system with complete control over anything on the system. It facilitates the interaction between the software and the hardware.
  2. File System: method or data structure that the OS uses to store and retrieve data in the memory.
  3. User Interface (Command line interface or Graphical User interface)
  4. Ability to manipulate data based on user commands

Why do Software Engineers prefer Linux or macOS over Windows?

Linux and macOS are very similar as they are both UNIX-based. UNIX is a multitasking, multiuser operating system designed for flexibility and adaptability. On the other hand, Windows derives most of its functionalities from other operating systems.

  1. Linux is open source based. It makes feature updates and bug fixes a lot more frequent. If a developer finds a bug, s/he can raise the pull request. If it gets merged, it will be there in the next release. On the other hand, if a bug comes up in Windows, being a commercial product, the developers working on it make a list of all the bugs, fix them in one go and then come up with an update after a long time.
  2. It supports almost all programming languages and offers a vast range of tools and applications useful for software development.
  3. Linux Terminal is superior to Windows Command Prompt. The command line tools are developed natively for Linux, so it has better support.
  4. Easier to get development-related things done in Linux. Like, as the ability to do Bash Scripting and SSH into some remote server.

Where did Linux come from? History of Linux

In 1969, two people, Ken Thompson and Dennis Ritchie developed the first Operating System called UNIX. A decade later, another person named Richard Stallman developed the GNU Project. Around that time, some other OS were also developed, like BSD and MINIX. All of them were similar to UNIX but lacked a unified Kernel. Then came Linus Torvalds in 1991, who started working on his side project while attending university. That project was Linux Kernel. Today this Linux Kernel is the unified kernel used by all Linux distributions that are out there, like Debian, Ubuntu, mintOS, fedora, arch Linux etc.

Terminal and Shell

You can access the terminal in your Linux distribution by pressing Ctrl+Alt+T. A Terminal is the text input/output environment that launches the shell. Now, what is a Shell? It is the command line interpreter. It executes every single command line-by-line, just like python does. You can say it is a program that takes your command from the keyboard and takes it to the kernel for execution. Most of the shells use Bash as their programming/scripting language.

Screenshot from 2022-10-20 17-54-37.png As you can see in the screenshot, lenovo is the username of the computing device. You can choose whatever name you want on your computer. After @, lenovo-Lenovo-ideapad-500-15ISK is the hostname. The ~ symbol represents the home directory; it will change to the name of the directory you enter.

Some basic commands to interact with the OS

echo command

Whatever you write after echo, will be shown as the output. For example, echo Hello World will give the output ‘Hello World’

pwd command

It shows your present working directory or you can say, your location in the directory tree. The folders or directories are located in the system based on the tree structure. All directories have a parent directory. pwd command displays the path based on this tree's hierarchical structure. If you are in the Desktop folder, it will show /home/lenovo/Desktop as the output.

Screenshot from 2022-10-20 18-11-28.png

ls command

It shows all the directories and files that are present in the present working directory you’re currently in. The output of ls command depends on your location in the tree hierarchy. If you write ls -a as the command where -a is the flag and will show all the hidden files as well. If you add -l flag along with ls command, it will show many other details (like date of creation, permissions etc.) of every single item present in the directory. You can combine both flags by writing -la

cd command

It helps you change your location in the directory tree hierarchy.

By writing cd .. you can go back to a directory in the hierarchy. By using cd [directory name], you can go to that directory but it has to be present in your current directory. If it is not, then it will show not found output. Otherwise, you can also write a complete path to your desired directory after cd to tackle this problem, a path like pwd command shows. With cd - command you can go back to the previous directory. But this is not the same as cd .. command. cd .. takes you to the parent directory of the directory you were in. On the other hand, cd - takes you to the previous directory you were in, which is not necessarily the parent directory of the current directory as it is possible you switched to the current directory by putting cd [long path].

When you change the path, the exact hierarchy path gets added after the ~ sign which represents home.

mkdir and rmdir commands

mkdir command creates a new directory wherever you are situated in the hierarchy tree. Or wherever else you want by using the destination path and the -p flag like mkdir -p /new_folder/newDirectory, even if the new_folder does not exist, it will automatically create one for you.

rmdir command lets you remove the directory in the same way. But only if it does not have more directories inside it

If you want to remove a directory that has files and other subdirectories inside it, you have to use the command rm along with flag –r like rm -r new_folder or else the shell will give the error directory not empty. -r flag helps remove a directory recursively. To know exactly what rm –r folder command removed as the output, write rm –rv folder, where v means verbose.

touch command

This command creates a new file. You need to write the full name of the file with the extension like newfile.txt after touch like touch newfile.txt and this will create that desired file in whichever directory you are present.

cat command

It shows the content of the file, whatever is written in it.

Screenshot from 2022-10-20 20-47-47.png

history command

It lists all the commands you have entered in the shell since the beginning. If you want to use a command you used previously without typing everything once again, you can press the up key on your keyboard.

cp and mv commands

cp command will help you copy a file to any other folder with the help of its path. For example, if you write cp test.txt new_folder/subfolder1, this will copy test.txt to subfolder1.

mv command lets you do the same thing but rather than copying, it moves the file to the destination. This is like how you cut/paste in GUI.

If you want to copy or move a file without being present at the file’s location by using the cd command, you can write the whole path of the file instead like cp ~/Desktop/test.txt ~/Desktop/new_folder/subfolder1 (cp [file path] [destination path].

find command

It helps you find a file or directory in your system. Use it like ‘find [where to find] [argument] [file name]’, for example, find ~ -name file.txt. If you don’t know the extension of a file but know only the name then you can write file.* instead of file.txt. If you only remember the extension but not the name, you can write *.txt instead of file.txt. You can also use another flag -type if you want to specify the type of the element you’re searching for example –type d for directory (find ~ -type d -name new_folder2).

TIP: There are many flags for each command in Linux. You can’t possibly remember all of them. Whenever you need to find usable flags for a command, simply write in the shell ‘man [command name]’ where man means manual. Or you can also use google search like, 'mkdir command flags' and check out blogs or official documentation if you’re not comfortable with the ‘man’ command output.

To know what a command does or what is its purpose, you can write the command `whatis’ like whatis mkdir

To see a video lecture of this blog, go to this youtube video

Follow me on Twitter here

Do comment your thoughts or anything specific you learned below!

Did you find this article valuable?

Support Kshitij Sharma by becoming a sponsor. Any amount is appreciated!