(Day 1)
What is Linux?
Linux is an open-source operating system (os)
An OS is software that communicates with the hardware and allows other programs to run.
The Linux os was developed by Linus Torvalds in 1991, created as an idea to improve the Unix OS.
Why Linux?
Free & Open Source
Anyone can run, study, modify, and redistribute the source code
Secure
Perform well and stable
Lightweight
it's flexible, and you can receive excellent support from a large community of users.
Where is Linux used?
Space centres like NASA
Health Care
Financial Institute e.g. NYSE
smart devices
Commercial uses for running application
web servers
Why you should learn about Linux?
Linux Distribution:
Linux Basics Command:
pwd - When you first open the terminal, you are in the home directory of your user. To know which directory you are in, you can use the
pwd
command.ls - Use the
ls
command to know what files are in the directory you are in. You can see all the hidden files by using the commandls -a
cd - Use the
cd
command to go to a directory. For example, if you are in the home folder, and you want to go to the downloads folder, then you can type incd downloads
mkdir & rmdir - Use the
mkdir
command when you need to create a folder or a directory.Use rmdir to delete a directory. Butrmdir
can only be used to delete an empty directory. To delete a directory containing files, userm.
rm - Use the rm command to delete files and directories. Use "rm -r" to delete just the directory. It deletes both the folder and the files it contains when using only the rm command.
touch - The
touch
command is used to create a file. It can be anything, from an empty txt file to an empty zip file. For example,touch new.txt
man & --help - To know more about a command and how to use it, use the man command. It shows the manual pages of the command. For example, man cd shows the manual pages of the cd command.
cp - Use the cp command to copy files through the command line. It takes two arguments: The first is the location of the file to be copied, and the second is where to copy.
mv Use the mv command to move files through the command line. We can also use the mv command to rename a file.
echo - The "echo" command helps us move some data, usually text into a file. For example, if you want to create a new text file or add to an already made text file, you just need to type in, echo hello, my name is Safia>> new.txt
(Day 2)
Linux Filesystem Hierarchy Standard (FHS):
What is File System?
OS store and manage data on disk or partitions using a structure called FileSystem.
FileSystem includes files, directories and their related permissions.
FS stores data in a hierarchy of directories and files.
Types of File Systems in Linux?
ext3(extended 3)
ext4
XFS
FAT
BTRFS etc..
How to check your Linux FileSystem?
lsblk -f
What is ext4?
Nowadays ext4 is the default file system on most Linux distributions. It can support large files and file systems of up to 16 terabytes. It also supports an unlimited number of sub-directories
File System Structure?
/boot
Having files used by the boot loader (ex: grub)
/dev
System device files (ex: speakers, keyboards etc)
/etc
Have configuration files
/usr/sbin
System binaries of the root directory
/opt
Installation of optional add-on applications
/proc
Running process
/usr/lib
C program library files needed by commands and apps
/tmp
Having temporary files
/home
Directories of users
/root
The home directory of the root user
/var
System logs
/run : System daemons that start very early (ex:systemd and udev)
temporary runtimes files like PID
/mnt
To mount external filesystem(ex: NFS)
/media: For CDROM Mounts
(Day 3)
Today is all about important commands in Linux
How to check Hostname on Linux?
hostname
its main purpose is to uniquely identify a network
How to check the current login user using?
whoami
How to check IP on Linux?
ip addr
How to search for your folder?
find <folder_name>/ -name <folder_name>
Find a folder in a given location?
ls folder*
How to edit and write into a file?
vi,vim,nano
Diff between ls -lt and ls -ltr
ls -lt - according to the modification date
ls -ltr - reverse order (last modified file show in last)
Permissions and ownership of files explained?
How to read a file?
cat, less
What is the word count command?
wc <file_name>
ex : 2 6 31 file1
2 - lines
6 - words
31 - bytes
if you only see the line?
wc -l <fiel_name>
Compare two files using diff command.
diff <file1> <file2>
Compressing and Decompressing of files (zip unzip)?
tar cvf files.tar <file1> <file2>
gzip <file_name.tar>
unzip
gunzip <files_name>
tar xvf <file_name.tar>
How to rename a file?
mv <current file_name> <change file_name>
How to split and combine the files?
combine the files
cat file1 file2 > file3
split the files
split -l 1 file_name
-l - line
How to use less command?
less <file_name>
you see the end content of your file
shift + G
you see the first line of your content
shift + P
Search words in your file?
:/<search_your_word>
grep command?
cat <file_name> | grep <word>
without case sensitive
cat <file_name> | grep -i <word>
Head and tail command?
head -1 <file_name>
tail -2 <file_name>
Sorting and Uniq command?
sort <file_name>
sort <file_name> | uniq
(Day 4)
Hard and Soft Links:
What is a link in Linux?
A connection between a file name and the actual data on the disk.
we can call it a shortcut
example of the window :
Difference between Soft and Hard links:
Soft link: The link will be removed if the original file is removed or deleted.
ln -s <Data_location> <soft_link_name>
Hard link: Renaming, Delating or removing the file will not affect the link
ln
Day 5
Top Command:
The top (table of processes) command shows a real-time view of running processes in Linux and displays kernel-managed tasks.
The command also provides a system information summary that shows resource utilization, including CPU and memory usage.
top
top then c = shows commands absolute path
top then k = kill a process by PID(Process Id) ctrl+z
top then n = to change the no.of tasks displayed
top then d,s =to change interval of refresh
top then M = To sort the Linux running process by memory usage
top then r = you can change the nice value of a PID
negative priority = high
positive priority = low
The range of the nice value is +19 (low priority) to -20 (high priority).
top then u = to filter task by user
top then f = Field Management
top then h = help
top then z or b = Toggle:'z'color/mono;'b' bold/reverse(only if 'x'or )
top then x y = 'x' sort field;'y'running tasks
top then i = process which some memory usage
Day 6
User Management:
User management includes everything from creating a user to deleting a user on your system.
From which user are you?
whoami
To create a user account
sudo useradd dev_safia -m
For check user account properties
sudo cat /etc/paaswd
For creating user account password
passswd dev_safia
To check user password properties
sudo cat /etc/shadow
For switch user account
su dev_safia
For logout from user account
exit
For delete user account
sudo userdel dev_safia
For change user login name
usermod -l login-name old-name
(DAY 7)
File Permissions:
What is meant by file permissions?
File permissions control what user is permitted to perform which actions on a file.
All three owners (user owner, group, others) in the Linux system have three types of permissions defined. Nine characters denote the three types of permissions.
Read (r) : The read permission allows you to open and read the content of a file. But you can't do any editing or modification in the file.
Write (w) : The write permission allows you to edit, remove or rename a file. For instance, if a file is present in a directory, and write permission is set on the file but not on the directory, then you can edit the content of the file but can't remove, or rename it.
Execute (x): In Unix type system, you can't run or execute a program unless execute permission is set. But in Windows, there is no such permission available.
Permissions are listed below:
permission | on a file | on a directory |
r (read) | read file content (cat) | read directory content (ls) |
w (write) | change file content (vi) | create file in directory (touch) |
x (execute) | execute the file | enter the directory (cd) |
Permissions (rwx) levels
u - yourself (the current user)
g - group in which user belongs to
o - others
a -all
How to see the permissions?
ls -ltr
How to change permissions?
chmod u+r <file_name> (For adding permission)
chmod u-w <file_name>
chmod ugo+r <file_name>
chmod ugo+r <file_name>
chmod a+rwx <file_name> (Adding permission for all)
DAY 8
chmod Numeric Mode
File Permission Numbers:
The codes of the file permission have three digits, which are as follows:
The first digit is assigned to the owner.
The second digit represents the group of the file.
The last one is for everyone.
The digits lie between 0-7 where:
4\= read
2\= write
1\= execute
0\= no permission
The permission digit of all classes is decided by summing up the permission values.
In other words, all digits for all classes can be the value of 4, 2, 1, and 0. Here is the full permission list:
0 (0 + 0 + 0) = The user class does not have permission.
1 (0 + 0 + 1) = Only execute permission.
2 (0 + 2 + 0) = Only write permission.
3 (0 + 2 + 1) = Execute and write permissions.
4 (4 + 0 + 0) = Only read permission.
5 (4 + 0 + 1) = Execute and read permissions.
6 (4 + 0 + 2) = Write and read permissions.
7 (4 + 2 + 1) = All permissions.
Permission 777
A 777 permission offers to execute, write, and read permissions to every three classes of users. In other words, everyone who has access to our system can execute, read, and change files.
DAY 9
ACL
when we have chmod and chown then what is the use of ACL?
It allows you to give a more specific set of permissions to a file or directory without changing the base ownership and permissions.
commands : setfacl
& getfacl
getfacl testfile.txt
For adding the permission for the user
setfacl -m u:user_name:rwx <target_file>
For adding the permission for group
setfacl -m g:group_name:rwx <target_file>
To remove a specific entry
setfacl -x u:user_name:rwx <target_file>
To remove all entries
setfacl -b <target_file>
For adding permission for user in all the files inside a folder -
setfacl -Rm u:user_name:rwx <target_file/folder>
Create a new user account with admin (sudo) access on Ubuntu or Debian Linux
Commands to add or create a new sudo user (admin) on an Ubuntu or Debian Linux server:
Open the terminal application
For remote Ubuntu/Debian server use the ssh command and log in as the root user using either su or sudo.
Create a new user named marlena, run: adduser safia
Make marlena user ‘sudo user’ (admin) run: usermod -aG sudo safia
Verify it by running the id safia command
Log in as marlena: su - safia. Than run sudo command for verification. For example: sudo ls -l /etc/shadow
DAY 1
Linux Shell Scripting
What is Linux Shell?
what is shell scripting?
Shell script consists of a set of commands to perform a task
All the commands execute sequentially
Some tasks like file manipulation, program execution, user interaction, automation of tasks etc can be done.
Types of Shell
Bourne Shell (sh)
Bourne Again Shell (Bash)
Korn shell
C shell
TCsh Shell
Scripting Format
#!/bin/bash
The sign #! is called she-bang and is written at top of the script. It passes the instruction to program /bin/sh.
How to check what is your shell?
echo $0
DAY 2
why do we use variables?
we can use variables to store a value or to refer to/point to a value
To access the value stored in a variable, prefix its name with the dollar sign ($) −
Variables in script
var_name=$(hostname)
echo $var_name
example:
#!/bin/bash
name=safia
age=22
echo My name is $name and my age is $age
host=$(hostname)
echo my hostname is $host
path=$(pwd)
echo what is my current location?
echo my current location is $path
How to take input from users?
read <var_name_to_store_value>
example:
#!/bin/bash
echo what is your name?
read name
echo Hi $name, welcome to my channel
if-else
The if then else condition loop states that if the condition meets, output goes to if part otherwise it goes to the else part.
The word fi represents if loop termination.
if[$age -eq 18];
then
echo "you are eligible to vote"
else
echo "Sorry you are not eligible to vote"
fi
example:
#!/bin/bash
echo Hi Welcome to My Channel
sleep 2
echo Do you like my video?
sleep 2
read ans
if [ $ans -eq 1 ]
then
echo " Thank you "
else
echo " please say why you not like my video "
fi
Types of Operators
equal -eq / ==
Greaterthanorequalto -ge
Switch case
#!/bin/bash
echo Hey Choose an option
echo
echo a = To see the current date
echo b = list all the files in current dir
echo c = your current location
read choice
case$choice in
a)date;;
b)ls -ltr;;
c)pwd;;
*)echo "Non a valid input"
esac
For Loop
Suppose there is a situation when you need to execute the same commands again and again. we use for loop to iterate over some condition and to repeat a set of commands in repetition.
for i in 1 2 3 4 5
do
echo Number is $i
done
Other ways to write For loop
for task in Read Write Blog Video Sleep
do
echo My task is $task
done
where,
i - variable
value - string and numeric value according to you
DAY 3
you have to create a lot of files
for file in Day{1..90}
do
touch $file
done
While loop
Linux scripting while loop is similar to C language while loop. There is a condition in while. And commands are executed till the condition is valid. Once the condition becomes false, the loop terminates.
count=0
num=10
while [ $count -le $num ]
do
echo $count
let count++
done
why do we use the let
command?
we use the "let" command for the expression/operation in the middle of a script.
++ = increment
Iterate values from the file
hosts="<path_of_file_named_items>"
for item in $(cat $items)
do
echo $item
done
How to use the expression
using let command
Example
let a++
let a=31*14
Alias
alias <short_name>="<your_command>"
Example:
alias
alias d="ls-ltr | awk '{print$9}"
alias pchron="ps-ef | grep"
DAY 4
Functions
Block of code which performs some task and runs when it is called
Can be reused many times in our program which lessens our lines of code.
We can pass arguments to the method.
There are two ways
function devops {
echo "Hi User"
}
__________________________________________________________________________________
DevOps() {
echo "Hello"
}
________________________________________________________________________________________
Note: To call the function, just use the name of the function like DevOps.
Day5
How to use the arguments/parameters in the function?
#!/bin/bash
echo "My name is Gita"
echo "My age is 30"
echo
echo "My name is sham"
echo "My age is 40"
echo
echo "My name is Mohan"
echo "My age is 50"
echo
______________________________________________________________________
Calculator Script Example
#!/bin/bash
addition() {
local num1=$1
local num2=$2
let sum=$num1+$num2
echo "Sum of $num1 and $num2 is $sum"
}
___________________________________________________________________
addition 60 80
addition 100 800
DAY 6
Shell Variables
RANDOM - A random integer between 0 and 32767 is generated
#!/bin/bash
#Generate a number between 1 and 6
NUM=$(($RANDOM % 6 + 1))
echo "Apki Result hai $NUM"
UID - User ID of the user logged in
#!/bin/bash
#Display if the user is root or not
if [[ $UID -eq 0 ]] then echo 'You are Root.' else echo 'You are not Root.'
fi
Example of UID:
To check if the script executed with the root user or not.
DAY7
I/O Redirects Use in Scripts
Cases, where you don't, wanna show the output of your command on the terminal or
write the output of commands in
>,>>,<
When we run a command in a terminal, below three files created
standard input - (stdin) - File Descriptor - 0
Standard Output - (stdout) - File Descriptor - 1
Standard Error - (stderr) - File Descriptor - 2
Output (stdout - 1 )
The output of a command is shown in the terminal
To route output in file using >
hostname > file_name
To append output in existing file using >>
pwd >> file_name
Error (stderr - 2 )
if any command gives you an error then it is considered as stderr - 2
We can redirect the error to a file:
Ex: cd /root/ 2>error_file
To redirect both standard output and error to a file:
Ex: cd /root/ > error_file 2>&1
Input (stdin - 0 )
input is used when feeding file contents to a file
Ex: cat < file_name
cat << EOF
DAY 8
What is /dev/null
In case if you don't wanna print the output of a command on the terminal and also write it in a file,
we can redirect the output to /dev/null
Example :
# cd /root/ &> /dev/null
Project: 1
Creating A Password Generator
Project: 2
How to create a disk stats using shell scripting
Disk_Stats
Project: 3
How to create a digital watch using shell scripting