Download as pdf or txt
Download as pdf or txt
You are on page 1of 41

Linux Basics

Bash Scripting

www.newpharma-development.ro
What is Linux ?
Linux Architecture
Linux Installation
Linux Basic Navigation
Linux Users & Permissions
Linux Network
Linux Load & Logs
Bash Scripting

www.newpharma-development.ro
Be a Linux Ninja or Jeday

www.newpharma-development.ro
What is Linux ?

• It’s an operating system, just like MAC OS or Windows 10


• It’s derived from Unix
• Was created in 1991 by Linus Torvalds
• Written in C, Assembly Language
• Linux comes from the name of the kernel

www.newpharma-development.ro
Linux
• In 2021, the lines of code to the Linux Git repository reached 27.8
million.
• In 2022, 100% of the world’s top 500 supercomputers run on Linux.
• All of the top 25 websites in the world are using Linux.
• 96.3% of the world’s top one million servers run on Linux.
• 90% of all cloud infrastructure operates on Linux, and practically all
the best cloud hosts use it.

www.newpharma-development.ro
Linux Distros

www.newpharma-development.ro
How to install linux ?

• Install directly on the machine


• Install inside a virtual machine
• HyperV – installed by default on Windows 10
• VirtualBox
• VMware Workstation Player
• Cloud Virtual Machine
• Windows Subsystem – WSL (Ubuntu/Debian)
• Containers

www.newpharma-development.ro
Linux Architecture

Kernel
• Core section which directly interacts with the hardware
Shell
• the interface which takes input from users and sends
instructions to the Kernel, also takes the output from Kernel
and send the result back to output shell
Applications
• utility programs which runs on Shell, ex: your web browser,
media player, text editor etc.

www.newpharma-development.ro
Linux Filesystem
Data in Linux is organized into files.
All files are organized into directories.
Directories are organized into a tree-like structure called the
filesystem.

www.newpharma-development.ro
Linux Filesystem

www.newpharma-development.ro
Linux Command line

Different shell types: C shell(csh), Bourne shell(bash), Korn


shell (ksh)
Most common one: bash – (Bourne Again Shell)
If you would like to know which shell, you are using you may
use command echo $SHELL
username@hostname:current_directory

www.newpharma-development.ro
Linux Basic Navigation

pwd (Print Working Directory) – shows which directory you are in


ls (List Directories) – list directories and files
cd (Change Directory) – change directory
• . (current directory). This is the directory you are currently in.
• .. (parent directory). Takes you to the directory above your current.
• ~ (home directory). This directory defaults to your “home directory”. Such as
/home/catalin.
• - (previous directory). This will take you to the previous directory you were just at.

www.newpharma-development.ro
Linux Paths

Absolute path – specify a location in relation to the root


directory(/); they always begin with a forward slash (/); Example:
/home/www/web
Relative path – specify a location in relation to where we currently
are in the system;
~(tilde) – shortcut for your home directory
.(dot) – reference to your current directory
..(dotdot) – reference to your parent directory; you can use it in
your path to go up in hierarchy
Tab - internal autocomplete mechanism; type and press Tab on
your keyboard

www.newpharma-development.ro
Linux File/Directory Manipulation
Be aware that command line has no undo mechanism.

Commands:
mkdir [options] <Directory> – create a directory; use –p option to also create parent
directories if needed
rmdir [options] <Direcory> – to delete a directory; directory needs to be empty
touch [options] <filename> – creates a blank file
cp [options] <source> <destination> - duplicates file or directory; use option -r for
recursive
mv [options] <source> <destination> - move a file or directory
rm [options] <filename> - to delete a file ; user –r option to remove folders with all
the files and folders inside
cat <filename> - to output file content; nice for short files
less <filename> - page displayer; very nice for large files

www.newpharma-development.ro
Linux Filters
Programs that access textual data and then transforms it.
• head [-number of lines to print] [path] – list the first n lines
• tail [-number of lines to print] [path] – is the opposite of head
• sort [-options] [path] – to sort input
• wc [-options] [path] – word count
• cut [-options] [path] – nice to use when content is separated into columns, and you
want to list only certain columns
• sed <expression> [path] – allows you to search and replace in your data
• uniq [options] [path] – remove duplicated lines
• grep – small Linux program for finding matching patterns

www.newpharma-development.ro
Linux File/Directory Manipulation

www.newpharma-development.ro
Linux Text Editors

• Command line text editors


• Vi/Vim
• Nano

• GUI text editors


• Gedit(Gnome)
• Sublime
• Visual Studio Code

www.newpharma-development.ro
Linux User Accounts
Linux has 3 types of accounts:
• Root, called also superuser because it has access to run every command and
change everything
• System accounts, needed for the operation of some specific components
• User accounts , provides interactive access to the system

There are four main user administration files:


• /etc/passwd − Keeps the user account and password information. This file holds
the majority of information about accounts on the Unix system.
• /etc/shadow − Holds the encrypted password of the corresponding account. Not
all the systems support this file.
• /etc/group − This file contains the group information for each account.
• /etc/gshadow − This file contains secure group account information.

www.newpharma-development.ro
Linux User Accounts
Commands
• useradd – to create a user ; adduser – another command to create accounts
• usermod – to modify user attributes
• userdel – to delete a user account
• groupadd – to create a group
• groupmod – to change group attributes
• groupdel – to delete a group
• passwd – to change password for a user
• su username – to become another user

www.newpharma-development.ro
Linux Permissions
Linux permissions dictates 3 things you can do:
• r read – you can view the content
• w write – you can change the content
• x execute – you can execute if is a script or a program

3 sets of people for whom we may specify permissions:


• Owner – single person who owns the file
• Group – every file belongs to a single group
• Others – everyone else who is not in the group or is not the owner

www.newpharma-development.ro
Linux Permissions

To view permissions we use command: ls –l

First line identifies the file;


• (-) is for a regular file
• (d) is for a directory
The next 3 letters represents the owner permissions; a letter represents the
presence of a permission and – is the absence of a permission
The following 3 represents the group permissions
The last 3 represents permissions for other users

www.newpharma-development.ro
Linux Permissions
To change permissions use chmod
Chmod has permission arguments that are made up of 3
components
• Who are we changing the permission for? [ugoa] - user (or
owner), group, others, all
• Are we granting or revoking the permission - indicated with
either a plus ( + ) or minus ( - )
• Which permission are we setting? - read ( r ), write ( w ) or
execute ( x )

www.newpharma-development.ro
Linux Packages Installation

In order to install a new package we use a package manager:


• Apt for systems like Debian,Ubuntu Ex: apt install apache2
• Yum for systems like Redhat,Centos Ex: yum install httpd
Another way to install a software is by obtaining the .deb or.rpm file:
• .deb is the extension for Debian software package
• .rpm comes from RPM package manager
Download the .deb file, make it executable and execute dpkg –i file.deb ; If
you have dependency issues you can run apt install –f to fix them.
Download the .rpm file, make it executable and execute rpm –i file.rpm
Other option is to build the package and install it.

www.newpharma-development.ro
Linux Packages Installation

In order to install a new package we use a packer manager:


• Apt for systems like Debian,Ubuntu Ex: apt install apache2
• Yum for systems like Redhat,Centos Ex: yum install httpd
Another way to install a software is by obtaining the .deb or.rpm file:
• .deb is the extension for Debian software package
• .rpm comes from RPM package manager
Download the .deb file, make it executable and execute dpkg –i file.deb ; If
you have dependency issues you can run apt install –f to fix them.
Download the .rpm file, make it executable and execute rpm –i file.rpm
Other option is to build the package and install it.

www.newpharma-development.ro
Linux Processes
Every time we run a command in Linux it’s creates a process.
Operating system tracks processes through a five-digit ID number known as
the PID or the process ID. Each process in the system has a unique pid.
When you start a process (run a command), there are two ways you can run it −
• Foreground Processes
• Background Processes
By default, every process that you start runs in the foreground.
It gets its input from the keyboard and sends its output to the screen.
A background process runs without being connected to your keyboard. The simplest
way to start a background process is to add an ampersand (&) at the end of the
command.
To list processes we use command: ps [options] ;
ps –auxe shows all the information
To kill a foreground process we use CTRL + C, to kill a background process we use
command kill [options] process id

www.newpharma-development.ro
Linux Bash – golden rule

When you don’t know what a


command does:
• man – stands for manual; manual
pages are a set of pages that explain
every command available on your
system including what they do, the
specifics of how you run them and
what command line arguments they
accept
Example: man ls

www.newpharma-development.ro
Linux Environment

Defined by environment variables which are set by system, user, shell,


other programs
env – The command allows you to run another program in a custom
environment without modifying the current one. When used without
an argument it will print a list of the current environment variables.
printenv – The command prints all or the specified environment
variables.
set – The command sets or unsets shell variables. When used without
an argument it will print a list of all variables including environment
and shell variables, and shell functions.
unset – The command deletes shell and environment variables.
export – The command sets environment variables.

www.newpharma-development.ro
Linux Remote Access
This can be done through several ways:
• SSH – network protocol that allows user to access
a computer over an unsecured network; uses port
22
• FTP – network protocol to transfer filles between
client and server ; Uses port 21
• SFTP – secure file transfer protocol – same as ftp
but over a secure connection; Uses port 22
To use SSH , you need a SSH server to be installed on the
remote server (openssh-sever).
As a client to connect via SSH , you have multiple
possibilities:
• download a terminal emulator as PUTTY
• connect from cmd (they usually have ssh
integrated) by typing: ssh username@ip
For SFTP or FTP , you can use clients as FileZilla or
Winscp. Winscp gives you the possibility to use SCP
protocol to transfer files , when you have SSH access.

www.newpharma-development.ro
Linux Network

Configuration files are in /etc/network.


Name convention is: eth0, eth1 but it was changed with systemd
197 , you can see something like ens0,enp0.
Commands
• ifconfig – list network interface configuration, similar to windows
ipconfig
• ping – to test reachability of a host
• netstat – to check your network activity
• traceroute – determines the pathway to your target
• iftop – traffic monitoring

www.newpharma-development.ro
Linux Server Load
df – h displays the amount of disk space available on the system
du – displays the disk space used by files and folder
free –m displays the amount of memory used
top / htop – provides a dynamic real time view of a running
system

www.newpharma-development.ro
Linux Logs
The default log folder in Linux is /var/log:
• /var/log/messages : General message and system related stuff
• /var/log/auth.log : Authentication logs
• /var/log/kern.log : Kernel logs
• /var/log/cron.log : Crond logs (cron job)
• /var/log/maillog : Mail server logs
• /var/log/syslog/ : Entries from syslog
• /var/log/apache2/ : Apache access and error logs directory
• /var/log/boot.log : System boot log
• /var/log/mysql/: MySQL database server log file
• /var/log/utmp or /var/log/wtmp : Login records file
• /var/log/apt: apt command log file.
You can run tail –f <log file> to see in real time new lines from the file.

www.newpharma-development.ro
Bash Scripting

Bash scripts are essentially just a sequence of the same


Linux commands that you would ordinarily use every day.
It is used to automate repetitive tasks on Linux filesystem.
It might include a set of commands, or a single command, or
it might contain the hallmarks of imperative programming
like loops, functions, conditional constructs, etc.

www.newpharma-development.ro
Bash Scripting – Hello World Example

Create a file helloworld.sh

#! /bin/bash
# This is the basic bash script
echo " Hello World! “

Line 1 represents #! (shebang) and specifies the bash shell location.


Line 2 represents the commented line.
Line 3 represents echo command to print the output.

Assign the execute permission(x) and excute the Script prefixing with ./
./helloworld.sh

www.newpharma-development.ro
Bash Scripting – Variables

variable_name=value
1. Prefix the variable name with dollar ($) sign while reading or printing
a variable.
2. Leave off the dollar sign ($) while setting a variable with any value.
3. A variable name may be alphanumeric, or it may be written with an
underscore (_).
4. A variable name is case-sensitive: x and X are considered as two
different variables.
5. variable name can be written either in UPPER_CASE or LOWER_CASE
letters or mixture of both as you want.
Bash variables are untyped, which means just type the variable name
by assigning its value, and it will automatically consider that data
type.

www.newpharma-development.ro
Bash Scripting – Variables

• System-Defined Variables – created and maintained by linux


• User-Defined Variables – created and maintained by user

www.newpharma-development.ro
Bash Scripting – For Loop / If / Functions

www.newpharma-development.ro
Linux Resources

• Linux Bible book


• https://linuxjourney.com/
• https://devhints.io/bash
• https://www.javatpoint.com/bash

www.newpharma-development.ro
DEMO

www.newpharma-development.ro
Homework

• Install Ubuntu Desktop Virtual Machine Using Oracle Virtual


Box
• Create simple bash script to display current user, current
working directory, all the files inside the current directory
and current date.
• Bonus Windows(optional): Install WSL on Windows 10
• Bonus Linux(optional): Create a ubuntu virtual machine
using Vagrant

www.newpharma-development.ro
hello@newpharma-development.ro

www.newpharma-development.ro
Thank you!

www.newpharma-development.ro

You might also like