Linux Basics To Advanced

Linux Basics To Advanced

(Day 1)

What is Linux?

  1. Linux is an open-source operating system (os)

  2. An OS is software that communicates with the hardware and allows other programs to run.

  3. The Linux os was developed by Linus Torvalds in 1991, created as an idea to improve the Unix OS.

    Why Linux?

    1. Free & Open Source

      1. Anyone can run, study, modify, and redistribute the source code

      2. Secure

      3. Perform well and stable

      4. Lightweight

      5. it's flexible, and you can receive excellent support from a large community of users.

Where is Linux used?

  1. Space centres like NASA

  2. Health Care

  3. Financial Institute e.g. NYSE

  4. smart devices

  5. Commercial uses for running application

  6. web servers

    Why you should learn about Linux?

    Linux Distribution:

    How to choose the right Linux distribution?

Linux Basics Command:

  1. 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.

  2. 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 command ls -a

  3. 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 in cd downloads

  4. mkdir & rmdir - Use the mkdir command when you need to create a folder or a directory.Use rmdir to delete a directory. But rmdir can only be used to delete an empty directory. To delete a directory containing files, use rm.

  5. 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.

  6. 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

  7. 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.

  8. 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.

  9. mv Use the mv command to move files through the command line. We can also use the mv command to rename a file.

  10. 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.

What is File System in Windows Operating Systems?

FS stores data in a hierarchy of directories and files.

Linux Directory Structure (Linux File System Hierarchy) ~ ServerKaKa

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?

LINUX FILE STRUCTURE - LinuxBaya

/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

  1. How to check Hostname on Linux?

    hostname

    its main purpose is to uniquely identify a network

  2. How to check the current login user using?

    whoami

  3. How to check IP on Linux?

    ip addr

  4. How to search for your folder?

    find <folder_name>/ -name <folder_name>

  5. Find a folder in a given location?

    ls folder*

  6. How to edit and write into a file?

    vi,vim,nano

  7. Diff between ls -lt and ls -ltr

    ls -lt - according to the modification date

    ls -ltr - reverse order (last modified file show in last)

  8. Permissions and ownership of files explained?

    Learning the shell - Lesson 9: Permissions

  9. How to read a file?

    cat, less

  10. 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>

  11. Compare two files using diff command.

    diff <file1> <file2>

  12. 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>

  13. How to rename a file?

    mv <current file_name> <change file_name>

  14. How to split and combine the files?

    combine the files

    cat file1 file2 > file3

    split the files

    split -l 1 file_name

    -l - line

  15. 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

  16. Search words in your file?

    :/<search_your_word>

  17. grep command?

    cat <file_name> | grep <word>

    without case sensitive

    cat <file_name> | grep -i <word>

  18. Head and tail command?

    head -1 <file_name>

    tail -2 <file_name>

  19. Sorting and Uniq command?

    sort <file_name>

    sort <file_name> | uniq

    (Day 4)

    Hard and Soft Links:

    1. 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.

  1. 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.

  2. 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.

  3. 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:

permissionon a fileon 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 fileenter 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

Learning the shell - Lesson 9: Permissions

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

Chmod 777 Ubuntu

Chmod 777 Ubuntu

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?

Introduction to Linux Operating System - GeeksforGeeks

what is shell scripting?

  1. Shell script consists of a set of commands to perform a task

  2. All the commands execute sequentially

  3. Some tasks like file manipulation, program execution, user interaction, automation of tasks etc can be done.

    Types of Shell

    1. Bourne Shell (sh)

    2. Bourne Again Shell (Bash)

    3. Korn shell

    4. C shell

    5. 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

Unix Operators | Various Types of Operators in Unix You Need To Know

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

Linux Advanced - Input Output Redirection and Piping

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

Online Password Generator - Secure Passwords Ideas

https://youtu.be/hEFJbL_Fbh8

Project: 2

How to create a disk stats using shell scripting

Disk_Stats

Project: 3

How to create a digital watch using shell scripting

Best Digital Clock GIFs | Gfycat