# OS Fundamentals and Linux Course ## Open Source Software Software with source code publically open so anyone can inspect, modify and contribute to. **MS-DOS (Microsoft Disk Operating System)** was the dominant operating system for PC's in the 1980s. ![MS-DOS GitHub](https://i.imgur.com/0iKJRkP.png) https://github.com/microsoft/MS-DOS So now I can view the code that runs this operating system, I can download it, make changes and modify just the way I want! ## Linux and Open Source *Linux is free and open source* then you might ask, who **owns** Linux? Linux is freely available to anyone however the trademark of the name "Linux" is with its creater, Linus Torvalds. Linux actually refers to the just the *Linux Kernel* although many people refer to it as an entire operating system but the actual OS should be called as **GNU/Linux**. ### GNU/Linux is a combination of two major projects: #### 1. [GNU Project](https://www.gnu.org/home.en.html): started by [Richard Stallman](https://stallman.org/): It was started to create *free Unix-like Operating System*. Don't worry about what Unix means, I'll get to that in few seconds. So the project went to develop serveral crucial components that are required for a fully functional OS. Some of the most important software from the GNU Project are: - ***GNU C Compiler(GCC)***: converts source code into executable programs - essential for compiling the Linux Kernel itself!! - ***GNU Bash(Bourne Again Shell)***: A command-line interface that users can type commands on and execute them. - ***GNU coreutils***: Implementation for command line tools like `ls`, `cp`, `rm` etc, we will be learning these commands! ***On a side note, Richard Stallman, CRAZY GUY! do check out his personal website.*** ![Richard Stallman](https://i.imgur.com/45LqjIo.png) #### 2. Linux or the Linux Kernel: developed by Linus Torvalds Around 1991, Linus was a computer science student just like you and me. He was using the Minix OS which was popular for academic use but not great for personal use. He wanted a Unix-like system but couldn't afford one so he just built his own operating system kernel! *Thus, Linux was born as a Unix-like kernel that worked well on PCs, and when combined with the GNU Project tools, it became the fully functional GNU/Linux operating system.* Here's the source code to the Linux Kernel: https://github.com/torvalds/linux For those interested, check out the actual mails sent by Linus when he was developing Linux: https://www.cs.cmu.edu/~awb/linux.history.html ## Kernel We have been saying Linux Kernel for so long, but what is a kernel? Okay think about this situation: I want to run a bunch of things: 1. Five Python Scripts 2. One browser with say 10 tabs 3. Spotify streaming music in the background 4. A file being downloaded All of these things are running simultanously, but your computer has only **one brain - the CPU** which can do only certain number of tasks at a time. Then how do we decide *which program gets to use th CPU first?* or *How long should each program run?* That means we need a *master controller* that makes such crucial descisions, This master controller is called the *Kernel*. The kernel is the core of an operating system and acts as a bridge between software and hardware. In the example above, the kernel connects your Python scripts, Spotify, and browser with the CPU and memory (RAM), ensuring they all run smoothly. ![Kernel Interaction](https://i.imgur.com/mtuTuw1.png) ### It has key responsibilities like: 1. ***Process Management***: The kernel decides which process gets CPU time and for how long. In our case, it schedules the Python scripts, the browser, and Spotify, ensuring none of them hog all the resources. 2. ***Memory Management***: Kernel would allocate some amount of memory to the browser tabs and the python files. Here, memory means RAM not your disk. 3. ***Device Management***: The kernel takes care of the devices attached to the system, for example when you plug in a USB device like keyboard, the kernel would ensure the data is flowing correctly between the hardware and software. 4. ***Security and Access Control***: Protecting your system from malware in simple words, the kernel make sures unauthorised programs can't access unauthorised areas of your computer. Now that you know what a kernel is, lets move on to how to talk to the kernel, Introducing you to **Shells** ## Shells You don't communicate directly with your kernel, instead you need some interface that converts our commands into a language that the kernel understands. This is exactly where shells comes in! So shells are basically like a translator between you and the kernel. It lets you give instructions to the operating system. Some examples of shells are: `bash`, `zsh`, `fish`. ![Shell](https://i.imgur.com/YeA208P.png) In the above image, notice how the shell is around the kernel. You type in commands like `ls`, `cd` and the shell just takes care of running the commands using the required libraries/implementation. The user doesn't have to bother about how the commands are implemented and lets the shell translate the commands into detailed kernel instructions. ##### Shell Scripting lets you write scripts that the shell can run and these scripts contain a bunch of commands. This is highly useful when you need do some repitative commands. For example: when servers are being maintained, the devlopers run shell scripts which run some commands to clear up space, read all the server logs and look for any errors. We will be writing shell scripts pretty soon! >[!Note] >#### ***Everything in Linux is a file*** >![everything is a file](https://i.imgur.com/kRaDGQD.jpeg) > - Lets say you are printing a document, Linux essentially is just writing the contents of the document to the printer, via the cable. *Umm so the printer is just a file to Linux!* > - Even directories or folders are files containing files! ## Command Line Operations >[!Note] >- If you are using **Windows**, you can run most of these commands on `git bash`. >- If you are using **macOS**, you don't need to worry at all, you can use your terminal itself! All these commands are supported. >- **Linux** ppl, just open up a terminal! > #### 1. `pwd` : Print Working Directory Shows which folder you are currently in. #### 2. `ls` : List Lists all the files and directories in the current directory. Do explore the flags `ls -a`, `ls -l`, `ls -A`. Check out more useful flags by using the `man ls` command which gives you the user manual for the `ls` command. ![image](https://hackmd.io/_uploads/Hy6WeXh1ke.png) #### 3. `cd`: Change Directory Lets you move from one directory to another! - `cd ..`: this lets you go back to the parent directory - `cd ~` or `cd`: takes you to the `home` directory - `cd /`: takes you to the `root` directory - `cd .`: doesn't do anything, just takes you to the current director which you already are in! #### 4. `man`: Manual Its a user manual page for a command, it shows you all the flags the command can support and what all can be done with the command. So you can do `man ls` which will give you the manual for the `ls` command. Check out `man` pages for any command! #### 5. `touch` Creates a empty file. Lets say you want to make a file called `hallo.py`. All you have to do it just `touch` the file! Literally! So you would just run the command `touch hallo.py` and the python file would be created! You can make any type of files with the `touch` command, like `touch fileName.json` or `touch fileName.yml`. You can also make multiple files at once like `touch file1.txt file2.json file3.py` #### 6. `mdkir`: Make Directory Like the name says command to make a directory Simply run `mkdir newFolderName`. You can run the `ls` command to see if the newFolder has been made. #### 7. `rmdir`: Remove Directory Again, like the name says, used to delete ***empty*** directory. Imma stress on the **empty directory** part. If the directory has files and other directories in it, this command will simply throw an error. ![rmdir error](https://i.imgur.com/11kQowo.png) See it threw a error saying, the directory isn't empty. #### 8. `rm -r`: Remove files and directories recursively If the directory you want to delete has files and directories in it, you can recursively delete all of them. Simply run the command `rm -r dirName` ![rm -r](https://i.imgur.com/9Uwfsd9.png) ## **NOTE THIS IS HIGHLY DANGEROUS, AND IS NOT REVERSABLE BECAUSE ONCE YOU DELETE IT'S GONE FOREVER. THERE IS NO RECYCLE BIN TO RESTORE FILES FROM.** #### 9. `rm -rf`: Remove recursively with force Sometimes you need to use the `force` or `-f` flag to forcefully delete some files. ## Again same warning as before, this is not reversable at all. #### 10. `cat`: Concatenate and Display Files Shows the content of a file, or combines multiple files and outputs their content. #### 11. `grep`: Global Regular Expression Print Lets say I need to search for the word `number` in the file called `math.py`. Here's the contents of `math.py`: ```python= x = int(input("enter any number")) y = int(input("enter another number")) output = x*y print(output) ``` Now, we can clearly see that the word `number` occurs twice. But what if I didn't know this, I would simply run the command `grep number math.py` ![grep](https://i.imgur.com/GPPkNdI.png) When I run `grep number math.py`, it returns the line which contains the word `number` in it. You can use Regular Expressions or pattern matching also. For example: I want to find all the words that start with `an` in `math.py` file. I can use Regex with the `grep` command as follows: ![regex with grep](https://hackmd.io/_uploads/BysnKDskyx.png) >[!Note] >### If you don't know what Regex or Regular Expressions, don't worry! Here's a guide to the same: [Beginners guide to regex](https://www.sitepoint.com/learn-regex/) ### You can combine multiple commands and send the output of one command as the input for another command! For example: I want to search for all the files that start with "ha" in a directory, I can combine or rather send the output of `ls` to `grep` which would grab the required files. ```bash= ls | grep "ha*" ``` ![ls and grep and pipe](https://i.imgur.com/JHOkHRm.png) #### Notice how I used the `|` this is called `pipe`. It basically pipes the output of the first command to the input of the next command. ---- ## How to install Linux: ![install linux](https://i.imgur.com/2xZaUmY.png) ## Package Management Commands ### Arch and Arch based Distros: 1. **Update package database and system:** `sudo pacman -Syu` 2. **Install a package:** `sudo pacman -S <package_name>` 3. **Remove a package (but keep its dependencies):** `sudo pacman -R <package_name>` 4. **Remove a package and its dependencies:** `sudo pacman -Rs <package_name>` 5. **Search for a package in the repositories:** `pacman -Ss <package_name>` 6. **Search for an installed package:** `pacman -Qs <package_name>` 7. **Display information about a package:** `pacman -Si <package_name>` 8. **List all installed packages:** `pacman -Q` 9. **Clean the package cache:** `sudo pacman -Sc` 10. **To remove all cached packages:** `sudo pacman -Scc` --- ### Yay Commands 1. **Update package database and system (includes AUR):** `yay -Syu` 2. **Install a package (from both official repos or AUR):** `yay -S <package_name>` 3. **Search for a package (from official repos or AUR):** `yay -S <package_name>` 4. **Search for installed packages (includes AUR packages):** `yay -Qs <package_name>` 5. **Remove a package (includes AUR packages):** `yay -R <package_name>` 6. **Remove a package and its dependencies (includes AUR packages):** `yay -Rs <package_name>` 7. **Clean up orphaned packages:** `yay -Yc` 8. **View and edit AUR PKGBUILDs before installing:** `yay --editmenu -S <package_name>` 9. **View all installed AUR packages:** `yay -Qm` --- ### Ubuntu (APT) Commands 1. **Update package database and system:** `sudo apt update && sudo apt upgrade` 2. **Install a package:** `sudo apt install <package_name>` 3. **Remove a package (but keep its configuration files):** `sudo apt remove <package_name>` 4. **Remove a package and its configuration files:** `sudo apt purge <package_name>` 5. **Search for a package in the repositories:** `apt search <package_name>` 6. **Display information about a package:** `apt show <package_name>` 7. **List all installed packages:** `dpkg --get-selections | grep -v deinstall` 8. **Clean the package cache:** `sudo apt clean` 9. **Remove unused dependencies:** `sudo apt autoremove` 10. **View the package details of an installed package:** `dpkg -s <package_name>` ## Shell Scripting ### 1. **Variables in Shell Scripting** - **Defining Variables**: Use `var_name=value` without spaces around `=`. For example: ```bash name="Alice" ``` - **Accessing Variables**: Prefix with `$`, e.g., `echo $name`. ```bash #!/bin/sh MY_VAR="Hello World" echo $MY_VAR ``` - **Types of Variables**: Environment variables (like `PATH`, `HOME`) are globally available, while user-defined variables are script-specific. ### 2. **Loops in Shell Scripting** - ### **For Loop**: ```bash #!/bin/sh for i in {1..5}; do echo "Iteration $i" done ``` ```bash #!/bin/bash for i in 1 2 3 4 5 6 do echo "Looping -> $i" done ``` - ### **While Loop**: ```#!/bin/bash INPUT=hello while [ "$INPUT" != "exit" ]; do echo "enter something, type \"exit\" to quit" read INPUTecho "you typed $INPUT" done ``` ```bash #!/bin/bash count=1 while [ $count -le 5 ]; do echo "Count is $count" ((count++)) done ``` Notice the usage of the flag `-le` which means `less than or equal to`. Some other flags are: - #### **`-le`: Less than or equal to** Checks if the left operand is less than or equal to the right operand. ```bash if [ "$a" -le "$b" ]; then echo "$a is less than or equal to $b" fi ``` - #### **`-ge`: Greater than or equal to** Checks if the left operand is greater than or equal to the right operand. ```bash if [ "$a" -ge "$b" ]; then echo "$a is greater than or equal to $b" fi ``` - #### **`-eq`: Equal to** Checks if two numbers are equal. ```bash if [ "$a" -eq "$b" ]; then echo "$a is equal to $b" fi ``` - #### **`-ne`: Not equal to** Checks if two numbers are not equal. ```bash if [ "$a" -ne "$b" ]; then echo "$a is not equal to $b" fi ``` - #### **`-lt`: Less than** Checks if the left operand is less than the right operand. ```bash if [ "$a" -lt "$b" ]; then echo "$a is less than $b" fi ``` - #### **`-gt`: Greater than** Checks if the left operand is greater than the right operand. ```bash if [ "$a" -gt "$b" ]; then echo "$a is greater than $b" fi ``` - **Until Loop**: Runs until a condition becomes true. ```bash #!/bin/bash count=1 until [ $count -gt 5 ]; do echo "Count is $count" ((count++)) done ``` ### 3. **Switch Case** - The `case` statement is useful for handling multiple conditions: ```bash #!/bin/bash read -p "Enter a number: " num case $num in 1) echo "You entered one" ;; 2) echo "You entered two" ;; 3) echo "You entered three" ;; *) echo "Invalid input" ;; esac ``` ```bash case $month in Jan|Feb|Mar) echo "Winter" ;; Apr|May|Jun) echo "Spring" ;; Jul|Aug|Sep) echo "Summer" ;; Oct|Nov|Dec) echo "Autumn" ;; *) echo "Invalid month" ;; esac ``` #### For more examples: [shell_scripts](https://github.com/bun137/os_fundamentals_linux_course_materials/tree/main/shell_scripting) --- ## Vim Cheat Sheet: [Link](https://www.barbarianmeetscoding.com/boost-your-coding-fu-with-vscode-and-vim/cheatsheet/) Made with ♡ by Shreya Gurram © Shreya Gurram 2024