what is Linux?
Linux is an open-source operating system (OS) that manages a computer’s hardware and resources. It was first created in 1991 by Linus Torvalds.
Linux Architecture
Shell: A shell is a command-line interface (CLI) that provides a user with a way to interact with an operating system. It takes commands/input from the user, interprets them, and then sends the instructions to the kernel. It also takes the output from the kernel.
The shell is also known as an Interpreter which is used to translate programs in assembly language to machine language and then the made request is transferred to the kernel.
shell communicates between the user and the computer’s kernel.
Kernel: The Linux kernel is the main component of the Linux operating system (OS). kernel is a computer program that manages the computer’s hardware resources, such as the CPU memory, and input and output devices. It is the core interface between the computer’s hardware and its processes.
Key characteristics of Linux include:
Open Source: The Linux kernel and most of its software are released under free and open-source licenses. This means that users have the freedom to view, modify, and distribute the source code.
Multitasking and Multiuser: Linux is a multitasking and multiuser system, meaning it can handle multiple processes running simultaneously and support multiple users logged into the system at the same time.
Security: Linux has a strong security model. Its permission and user account system help prevent unauthorized access to sensitive data and system files.
Stability and Reliability
Command Line Interface (CLI) and Graphical User Interface (GUI):
Linux file hierarchy
/ (Root Directory):
The top-level directory in the Linux file system hierarchy.
All other directories and files are organized beneath the root directory.
It is represented by a forward slash (/).
/bin (Binary):
Contains essential binary executables (command binaries) needed for system recovery and repair.
/boot:
Contains files necessary for the boot process, including the Linux kernel and bootloader configuration.
/dev (Device):
Contains device files representing hardware devices on the system.
/etc (Etcetera):
Contains system-wide configuration files and shell scripts used to initialize or configure system settings at boot time.
/home:
Home directories for user accounts are typically located here.
/lib (Library):
Contains shared libraries needed by the system and executables in /bin and /sbin.
/media:
Mount point for removable media such as USB drives or CD-ROMs.
/mnt (Mount):
Historically used as a mount point for temporarily mounted filesystems.
/opt (Optional):
Reserved for the installation of additional software packages from vendors.
/proc (Process):
A virtual filesystem providing information about running processes and kernel parameters.
/root:
Home directory for the root user (superuser/administrator).
/run:
Contains system runtime data, such as process IDs and sockets.
/sbin (System Binary):
Contains essential system binaries, often used for system administration.
/srv (Service):
Holds data directories for services provided by the system.
/sys (System):
A virtual filesystem that exposes kernel parameters and configuration information.
/tmp (Temporary):
Used for temporary files that may be deleted between reboots.
/usr (Unix System Resources):
Contains user binaries, libraries, documentation, and source code for the system.
/var (Variable):
Contains variable data files, such as logs, spool files, and temporary files.
/etc:
Contains system-wide configuration files and shell scripts used to initialize or configure system settings at boot time.
Listing commands
ls option_flag arguments
--> List the subdirectories and files available in the present directory
Examples:
ls -l--> list the files and directories in long list format with extra information
ls -a --> list all including hidden files and directory
ls *.sh --> list all the files having .sh extension.
ls -i --> list the files and directories with index numbers inodes
ls -d */ --> list only directories.(we can also specify a pattern)
Directory commands
pwd --> present working directory.
cd path_to_directory --> change directory to the provided path
cd ~ or just cd --> change directory to the home directory
cd - --> Go to the last working directory.
cd .. --> change directory to one step back.
cd ../.. --> Change directory to 2 levels back.
mkdir directoryName --> to make a directory in a specific location
Examples:
mkdir newFolder # make a new folder 'newFolder'
mkdir .NewFolder # make a hidden directory (also . before a file to make it hidden)
mkdir A B C D #make multiple directories at the same time
mkdir /home/user/Mydirectory # make a new folder in a specific location
mkdir -p A/B/C/D # make a nested directory
What is Kernel?
The Linux kernel is the main component of the Linux operating system (OS). It is the core interface between the computer’s hardware and its processes. The kernel is a computer program that manages the computer’s hardware resources, such as the CPU memory, and input and output devices.
What is Shell?
A shell is a command-line interface (CLI) that provides a user with a way to interact with an operating system. It takes commands/input from the user, interprets them, and then sends the instructions to the kernel. It also takes the output from the kernel. shell communicates between the user and the computer’s kernel.
What is Linux Shell Scripting?
A shell script is a computer program designed to be run by a Linux shell, a command-line interpreter.
if-else
#!/bin/bash
val=hemant
if [ $val == "hemant" ]
then
echo "name: $val is found"
else
echo "name does not match"
fi
Example of If else in Shell Scripting that numbers are +ve/-ve
#!/bin/bash
echo "Enter a number: "
read num
if [ $num -gt 0 ]; then
echo "$num : is +ve number"
elif [ $num -lt 0 ]; then
echo "$num : is -ve numner"
else
echo "Number is Zero "
fi
Create and delete multiple directories (day 1 .. day 20)using a for loop.
for i in {1..20}; do
# Create a new folder named "folder$i"
mkdir day$i
done
login script
#!/bin/bash #shebang
// bash interpreter use those are commonly use. other interpreter are like ksh,c shell, Bourn.
// Ask the user for login details
read -p 'Username: ' uservar
read -sp 'Password: ' passvar #-s indicates secure
echo "Thankyou $uservar, Now we have your login details"
File Permissions
Changing Linux Permissions using Alphabets.
We use the “chmod” command to modify permissions for all three categories.
chmod u+w file.txt #grants write permission to the file's owner.
chmod g-x file.txt #removes execute permission from the group.
chmod o+r file.txt #allows others to read the file.
Changing Linux permissions using numeric code
You may need to know how to change permissions in numeric code in Linux, so to do this you use numbers instead of “r”, “w”, or “x”.
0 = No Permission | 1 = Execute | 2 = Write | 4 = Read
touch file.txt #create a file
chmod 777 file.txt #set the permission to give read-write-execute access to owner, group and others