Please click ⭐if you like the project.
-
Linux: Linux is a family of open-source Unix-like operating systems based on the Linux kernel. We use the term “Linux” to refer to the Linux kernel.
-
Kernel: A kernel is the core component of an operating system. The kernel facilitates interactions between hardware and software components.
-
Shell: The shell is the interface that allows the users to communicate with the kernel.
-
Popular Linux Distributions: A Linux distribution is created from a Linux kernel. To install Linux, we need to choose a distribution. There are around 600 Linux distributions, with more than 500 in active development.
Debian
Ubuntu
Mint
Maniaro
openSUSE
RedHat
Fedora
-
pwd: The pwd(Present Working Directory) command outputs the name of the current working directory.
$ pwd
-
cd: The
cd
command in linux known as change directory command. It is used to change current working directory.$ cd [directory]
To change directory to the root directory:
$ cd /
To move inside a directory from a directory:
$ cd dir_1/dir_2/dir_3
To change directory to the home directory:
$ cd ~
To move to the parent directory of current directory:
$ cd ..
-
ls: The
ls
command lists files and directories within the file system, and shows detailed information about them.ls [flags] [directory] Example: $ ls //Listing files and directories with details $ ls -l //Home directory $ ls ~
The list of possible options for
ls
command:-a Show all (including hidden) -R Recursive list -r Reverse order -t Sort by last modified -S Sort by file size -l Long listing format -1 One file per line -m Comma-separated output -Q Quoted output
-
mkdir: With
mkdir
(make directory) command you can create directories or folders.$ mkdir newfolder $ ls newfolder
To create two or more directories:
$ mkdir folder1 folder2 folder3 $ ls folder1 folder2 folder3
-
rmdir: The
rmdir
(remove directories) is used to remove (empty) directories.Syntax
$ rmdir [option] directory
Remove empty directory:
$ rmdir foldername
Remove multiple directories:
$ rmdir foldername1 foldername2 foldername3
-
touch: The
touch
command is used to create, change and modify timestamps of a file.Create a new (empty) file:
$ touch file_name
Create multiple files:
$ touch file1_name file2_name file3_name
-
rm: The
rm
(remove) command is used to remove objects such as files, directories, symbolic links etc from the file system.Remove file:
rm file_name
-
cat: The
cat
command is very frequently used in Linux. It reads data from the file and gives their content as output. It helps us to create, view, concatenate files. So let us see some frequently used cat commands.$ cat [OPTION] [FILE]
To view a file:
cat filename
To view multiple files:
cat filename1 filename2 filename3
Create a new file:
cat> mynewfile
Copy the contents of one file to another file:
cat [filename-whose-contents-is-to-be-copied] > [destination-filename]
To append the contents of one file to the end of another file:
cat file1 >> file2
To display content in reverse order using tac command:
tac filename
-
cp: The
cp
command is ued to copy a file to another location. -
mv: The
mv
command is used to move a file another location -
echo: The
echo
command prints message to screen
-
sudo: The
sudo
(superuser do) command gives some admin privileges to non-admin usersList available commands:
sudo -l
Run command as root:
sudo
Run command as root:
sudo -u root
Run command as user:
sudo -u user
Switch to the superuser account:
sudo su
-
Basic User Commands:
Your username is indicated by the
whoami
command.whoami
The
who
command will provide you with details about who is logged on the system.who
Command
who am i
the who command will only show the line that points to your current session.who am i
The
w
command will inform you who is logged on and what they are doing.w
The
id
command will provide your user id, your primary group id, and a list of the groups you belong to.id
The
su
command enables a shell to be run as another user.su
The
su
command retains the same shell environment by default. To become another user and also get the environment of the target user, issue thesu -
command followed by the target username.If
su
orsu -
is not provided with a username, the command assumes that root is the target.The root user does not have a password set on some Linux systems like Ubuntu and Xubuntu. On these Linux systems, You can become root user via
sudo su
command. -
User Management:
On Linux, the local user database is
/etc/passwd
./etc/passwd
root
user also called the superuser is capable of doing almost everything.root
The
useradd
command is used for creating a new user.useradd
You can delete a user with
userdel
command.userdel
With the
usermod
command, you can modify a user's properties.usermod
User passwords can be set with the
passwd
command.passwd
-
Group Management:
groupadd
command is used to create a new group.groupadd
You can change group membership with the
useradd
orusermod
command.useradd
or
usermod
groups
command is used to display a list of groups to which the user belongs.groups
groupmod
command can be used to change the group name.groupmod
groupdel
command is used to delete a group.groupdel
With the
gpasswd
command, we can add a user to a group and to remove a user from a group. In the example below:gpasswd -a
gpasswd -d
1.Ownership:
chown
– change ownership.
2.Permissions:
chmod
– change permissions
The breakdown of permissions:
u – user
g – group
o – other
The permissions you can give to a file or folder:
r – read
w - write
x - execute
4 if read permission is desired.
2 if write permission is desired.
1 if execute permission is desired.
Thus 7 means read+write+execute, 6 means read+write, and 5 means read+execute.
Using the -R switch is important. If you want the permissions to apply from the parent object to child objects (the sub-folders and files), you must use the -R (recursive).
- Pattern search:
The
grep
command is used to search patterns in files.
grep pattern files
grep -i Returns the results for case insensitive strings
grep -n Returns the matching strings along with their line number
grep -v Returns the result of lines not matching the search string
grep -c Returns the number of lines in which the results matched the search string
- Find files and directories:
The find
command is used to find or search files and directories by file name, folder name, creation date, modification date, owner and permissions etc and perform subsequent operations on them.
Search file with name:
find ./directory_name -name file_name
Search for empty files or directories:
find ./directory_name -empty
- Whereis to locate binary or source files for a command:
Vi is a text editor originally created for the Unix operating system. Vim (Vi IMproved) as its name suggests, is a clone of Vi and offers more features than Vi.
The reasons why we should use Vi/Vim editor. ● Vim is available on most linux distro’s. ● Vim Uses Less Amount of System Resources. ● Vim Supports All Programming Languages and File Formats ● Vim is Very Popular in the Linux World
Start with Vi Editor
Command Mode: It allows the entry of commands to manipulate text. Insert mode: It allows typed characters on the keyboard into the current file.
You can create a new file or open an existing file using vi filename
command.
vi newfile (Create a new file and open)
or
vi existingfile (Open an existing file)
Example to use:
vi my_file.txt (create a new file or open the existing file)
Press `i` to enter the insert mode
Enter the text in the file
Press `ESC` to quit from insert mode
Press `:wq` to quit from vi
Exiting
These commands are used to exit from the file.
```cmd
:w # Write (save) the file, but don't exit
:wq # Write (save) and quit
:wq! # Force write (save) and quit
:q # Quit, but it fails if anything has changed
:q! # Quit and throw away for any changes
```
1.Linux Environment Variables
-
Variables can be classified into two main categories,
environment variables
, andshell variables
. -
Shell variables
are valid in the current shell instance. -
Environment variables
are variables that are valid system-wide.
Shell variables
The name of a variable can contain only letters (a to z or A to Z), numbers ( 0 to 9) or the underscore character (_) and beginning with a letter or underscore character.
Create a shell variable:
$ shellvar="shell variable"
To call a shell variable:
$ echo $shellvar
To remove a shell variable:
$ unset shellvar
Environment Variables
Environment variables allow you to customize how the system works and the behavior of the applications on the system.
Create an environment variable:
$ export ENVVAR="environment variable"
To call a shell variable:
$ echo $ENVVAR
or
$ printenv ENVVAR
To remove an environment variable:
$ unset ENVVAR
Other Commands
To get a list of all shell variables, environmental variables and shell functions:
$ set
To see path:
$ printenv PATH
To add a directory to path:
$ export PATH=$PATH:/home/ec2-user/test
1. The man pages
2. The info pages
3. The whatis command
4. The help option
1.Using Filters:
cat
- Concatenate files and print on the standard output
- Create a folder and name it examples.
```bash
cat example.txt
```
tee
-
Read from standard input and write to standard output and files
-
Write the content of the example.txt file in reverse order to another file named temp.txt and display the content of temp.txt in reverse order.
tac example.txt | tee temp.txt | tac
-
Check whether the temp.txt file created and display the content.
ls cat temp.txt
grep
-
Print lines that match patterns. The most common use of grep is to filter lines of text containing (or not containing) a certain string.
-
Create a text file named tennis.txt.
cat > tennis.txt
Amelie Mauresmo, Fra
Justine Henin, BEL
Serena Williams, USA
Venus Williams, USA
```
tr
-
The command 'tr' stands for 'translate’. It is used to translate, like from lowercase to uppercase and vice versa or new lines into spaces.
-
Create a text file named example.txt.
cat << EOF > example.txt Example:This is an example. EOF
-
In the content of example.txt, replace or translate aer letters with 'QAZ'.
cat example.txt | tr aer QAZ
-
Write the content of example.txt on the same line.
cat example.txt | tr '\n' ' '
-
Delete all the vowels in the content of example.txt.
cat example.txt | tr -d aeiou
-
Write the whole content of example.txt in capital letters.
cat example.txt | tr [a-z] [A-Z]
wc
-
Print line, word, and characters for each file.
wc example.txt
-
Find how many users are there in the computer.
wc -l /etc/passwd
sort
-
The sort filter will default to an alphabetical sort. The sort filter will default to an alphabetical sort.
sort example.txt
-
Sort the content of example.txt in reverse order.
sort -r marks.txt
uniq
-
Report or omit repeated lines. With the help of uniq command you can form a sorted list in which every word will occur only once.
-
Display only the unique names in the content of example.txt.before using uniq command, the file must be sorted
sort example.txt | uniq
comm
-
Compare two sorted files line by line. By default, comm will always display three columns. First column indicates non-matching items of first file, second column indicates non-matching items of second file, and third column indicates matching items of both the files.
-
Both the files has to be in sorted order for 'comm' command to be executed.
comm file1.txt file2.txt
2.Using Control Operators:
;
-
More than one command can be used in a single line with
;
. -
Write two seperate cat command on the same line using ;.
cat days.txt ; cat count.txt
&
- When a line ends with an ampersand &, the shell will not wait for the command to finish. You will get your shell prompt back, and the command is executed in background. You will get a message when this command has finished executing in background.
- Run sleep 20 command and let this command work behind while you're running other commands.
sleep 20 &
ls -l
cat count.txt
cat days.txt
$?
-
This control operator is used to check the status of last executed command. If status shows '0' then command was successfully executed and if shows '1' then command was a failure.
-
Run ls command and show that it is executed successfully.
ls
echo $?
- Run lss command and show that it failed.
lss
echo $?
&&
- The command shell interprets the && as the logical AND. When using this command, the second command will be executed only when the first one has been successfully executed.
||
- The command shell interprets the (||) as the logical
OR
. This is opposite of logicalAND
. Means second command will execute only when first command will be a failure.
&& and ||
- We can use this logical AND and logical OR to write an if-then-else structure on the command line. This example uses echo to display whether the rm command was successful.
#
- Everything written after a pound sign (#) is ignored by the shell. This is useful to write a shell comment but has no influence on the command execution or shell expansion.
\
-
Lines ending in a backslash are continued on the next line. The shell does not interpret the newline character and will wait on shell expansion and execution of the command line until a newline without backslash is encountered.
-
Escaping characters are used to enable the use of control characters in the shell expansion but without interpreting it by the shell.
1.Amazon Linux Instance:
- Update Amazon Linux Instance.
sudo yum update
- Check if 'git' is installed on Amazon Linux instance.
git --version
- Install git on Amazon Linux instance.
sudo yum install git
- Uninstall git on Amazon Linux instance.
sudo yum remove git
- Install git on Amazon Linux instance without any interruption.
sudo yum install git -y
- Uninstall git with dependencies on Amazon Linux instance without any interruption.
sudo yum autoremove git -y
- List all available packages for Amazon Linux instance.
sudo yum list
- List all available git packages for Amazon Linux instance.
sudo yum list git
- List all installed packages on Amazon Linux instance.
sudo yum list installed
- Check the available version of git with info command.
sudo yum list git
2.Ubuntu:
Update Ubuntu instance
sudo apt update
- Upgrade the packages. This command installs the listed available packages.
sudo apt upgrade
- Check if 'git' is installed on Ubuntu instance.
git --version
- Check the info for the git package installed on Ubuntu instance.
sudo apt info git
- List all available packages for Ubuntu instance.
sudo apt list
- List all installed packages on Ubuntu instance.
sudo apt list --installed
Shell scripting basics
- Create a folder and name it shell-scripting.
mkdir shell-scripting && cd shell-scripting
- Create a
script
file namedbasic.sh
. Note all the scripts would have the .sh extension.
#!/bin/bash
echo "Hello World"
-
Before we add anything else to our script, we need to alert the system that a shell script is being started. This is done specifying
#!/bin/bash
on the first line. -
After to save the above content, we need to make the script executable.
chmod +x basic.sh
- Then we can execute the
basic.sh
. To execute basic.sh, it is required to add./
beginning of thebasic.sh
../
means we're calling something in the current working directory. We have to specify the path for executables if they're outside our $PATH variable.
./basic.sh
- We can add the other shell commands to our script.
#!/bin/bash
echo "hello"
date
pwd
ls
- And execute again.
./basic.sh
If Statement
#!/bin/bash
read -p "Input a number: " number
if [[ $number -gt 50 ]]
then
echo "The number is big."
fi
- Relational Operators
Operator | Description |
---|---|
-eq | equal |
-ne | not equal |
-gt | greater than |
-lt | less than |
-ge | greater than or equal |
-le | less than or equal |
- String Operators
Operator | Description |
---|---|
= | equal |
!= | not equal |
-z | Empty string |
-n | Not empty string |
- File Test Operators
Operator | Description |
---|---|
-d file | directory |
-e file | exists |
-f file | ordinary file |
-r file | readable |
-s file | size is > 0 bytes |
-w file | writable |
-x FILE | executable |
If Else Statements
#!/bin/bash
read -p "Input a number: " number
if [[ $number -ge 10 ]]
then
echo "The number is bigger than or equal to 10."
else
echo "The number is smaller than 10"
fi
If Elif Else Statements
#!/bin/bash
read -p "Input a number: " number
if [[ $number -eq 10 ]]
then
echo "The number is equal to 10."
elif [[ $number -gt 10 ]]
then
echo "The number is bigger than 10"
else
echo "The number is smaller than 10"
fi
Boolean Operations
The Boolean operators below are supported by the Bourne Shell.
Operator | Description |
---|---|
! | negation |
&& | and |
While loops
#!/bin/bash
number=1
while [[ $number -le 10 ]]
do
echo $number
((number++))
done
echo "Now, number is $number"
Until loops
- The until loop is identical to the while loop, except that it will execute the commands within it until the test becomes true.
#!/bin/bash
number=1
until [[ $number -ge 10 ]]
do
echo $number
((number++))
done
echo "Now, number is $number"
For loops
#!/bin/bash
echo "Numbers:"
for number in 0 1 2 3 4 5 6 7 8 9
do
echo $number
done
echo "Names:"
for name in Joe David Matt John Timothy
do
echo $name
done
echo "Files in current folder:"
for file in `pwd`/*
do
echo $file
done
Using arrays with the for loop
#!/bin/bash
devops_tools=("docker" "kubernetes" "ansible" "terraform" "jenkins")
for tool in ${devops_tools[@]}
do
echo $tool
done
Continue and Break Statements
#!/bin/bash
number=1
until [[ $number -lt 1 ]]
do
((number++))
tens=$(($number % 10))
if [[ $tens -eq 0 ]]
then
continue
fi
echo $number
if [[ $number -gt 100 ]]
then
break
fi
done
Select loops
#!/bin/bash
read -p "Input first number: " first_number
read -p "Input second number: " second_number
PS3="Select the operation: "
select operation in addition subtraction multiplication division exit
do
case $operation in
addition)
echo "result= $(( $first_number + $second_number))"
;;
subtraction)
echo "result= $(( $first_number - $second_number))"
;;
multiplication)
echo "result= $(( $first_number * $second_number))"
;;
division)
echo "result= $(( $first_number / $second_number))"
;;
exit)
break
;;
*)
echo "Wrong choice..."
;;
esac
done
Shell Scripting/Functions
mkdir functions && cd functions
#!/bin/bash
Welcome () {
echo "Welcome to Linux Lessons"
}
Welcome
Passing Arguments to Functions
#!/bin/bash
Welcome () {
echo "Welcome to Linux Lessons $1 $2 $3"
}
Welcome Joe Matt Timothy
Returning Values from Functions
pwd
echo $? #0
pwt # It is wrong command
echo $? #127
-
When a bash function completes, its return value is the status of the last statement executed in the function. We can speciy return status by using the
return
keyword. We can think thereturn
keyword as exit status of function. -
Add
return 3
line toWelcome function
.
#!/bin/bash
Welcome () {
echo "Welcome to Linux Lessons $1 $2 $3"
return 3
}
Welcome Joe Matt Timothy
echo $?
Nested Functions
#!/bin/bash
function_one () {
echo "This is from the first function"
function_two
}
function_two () {
echo "This is from the second function"
}
function_one
Variables Scope
#!/bin/bash
var1='global 1'
var2='global 2'
var_scope () {
local var1='function 1'
var2='function 2'
echo -e "Inside function:\nvar1: $var1\nvar2: $var2"
}
echo -e "Before calling function:\nvar1: $var1\nvar2: $var2"
var_scope
echo -e "After calling function:\nvar1: $var1\nvar2: $var2"