Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
5 views64 pages

04 LinuxCommandsAndMore

The document covers Linux basics focusing on commands for redirection, filtering, managing services, and process handling. It explains how to start and stop services, find and kill processes, and manage software packages using RPM and YUM. Key topics include standard input/output, service management with systemd and init, and the importance of package management in Linux systems.

Uploaded by

iamsandesh1905
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views64 pages

04 LinuxCommandsAndMore

The document covers Linux basics focusing on commands for redirection, filtering, managing services, and process handling. It explains how to start and stop services, find and kill processes, and manage software packages using RPM and YUM. Key topics include standard input/output, service management with systemd and init, and the importance of package management in Linux systems.

Uploaded by

iamsandesh1905
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 64

DevOps

01 Linux Basics

Linux Commands &


More - Part 2
Agenda for this session
• Redirection & Filters, Simple Filter and
Advance Filter commands
• Start and Stop Services
• Find & Kill a process with id and name
• Package installation using RPM and YUM

2
Redirection
and Filter
Commands
(Simple and
Advanced)
Redirecting Standard Output to a File
(Using ‘>’, ‘>>’)
ls –la > mylist creates or overwrites file

cat mylist

ls –la >> mylist appends or creates file

cat mylist

4
Redirecting Standard Input from a File
(Using ‘<’)
cat < mylist passes the file ‘mylist’ to ‘cat’ command

This is the same as

cat mylist

5
Try this…
$cat > sample.txt August
January September
February October
March November
April December
May January
June February
July <Ctrl>C

6
Filter Commands
cat sample.txt lists all lines in sample.txt

head sample.txt lists the first 10 lines of sample.txt

head -5 sample.txt lists the first 5 lines of sample.txt

tail sample.txt lists the last 10 lines of sample.txt

tail -5 sample.txt lists the last 5 lines of sample.txt

7
Filter Commands
sort sample.txt sorts and lists all lines in sample.txt

uniq sample.txt removes adjacent duplicate lines and lists sample.txt

sort sample.txt | uniq lists unique lines of sample.txt in alphabetical order

wc sample.txt lists the number of lines, words and characters in sample.txt

tac sample.txt lists the lines of sample.txt in reverse order (opposite of cat)

8
Filter Commands
grep Feb sample.txt lists all lines in sample.txt having the string ‘Feb’

sed ‘s/uary/UARY/g’ sample.txt replaces ‘uary’ with ‘UARY’ in all places in sample.txt

nl sample.txt lists sample.txt with a serial number starting with 1 per line

9
Try this – Create student.dat with the
following lines (use vi or vim or any editor)
001 Amit Sahu
002 Cyndrella Murphy
003 Dhruv Shah
004 Manish Kulkarni
005 Balaji Joshi
001 Amit Sahu
004 Manish Kulkarni
002 Cyndrella Murphy
003 Dhruv Shah
005 Balaji Joshi
004 Manish Kulkarni
002 Cyndrella Murphy
10
Try the following commands
cat student.dat
sort student.dat
sort +0 student.dat
sort +1 student.dat
sort +2 student.dat
sort +3 student.dat (What is the output? Why?)
uniq student.dat
cat student.dat | uniq
cat student.dat | sort | uniq
sort student.dat | uniq | wc –l
echo student.dat file has $(cat student.dat|sort|uniq|wc –l) unique
lines

11
Redirecting Standard Output to a Command
(Using Pipe ‘|’)
ls –alr | more passes the output of ‘ls –alr’ to ‘more’ command

cat sample.txt | wc –l passes the output of ‘cat sample.txt’ to ‘wc -l’


command and displays the number of lines
in sample.txt.

echo sample.txt has $(cat sample.txt | wc -l) lines


assuming that there are 14 lines in sample.txt, it displays
sample.txt has 14 lines

12
Combination of Pipes and Redirection
cat sample.txt | wc –l > output.txt

cat output.txt

10

13
Standard Input (STDIN), Standard Output
(STDOUT), and Standard Error (STDERR)
By default,
STDIN is the keyboard (you type a command and
it appears on the terminal as you type)
STDOUT is the terminal window (CLI)
STDERR is the terminal window (CLI)

These three locations are file descriptors. File STDIN 0


descriptors are used to represent a file used by a STDOUT 1
process. STDERR 2

FILE DESCRIPTORS

14
Example
This command is to find from ‘/’ all files and directories with name
‘*mit*’ whether the file name is in smaller case or upper case.
find / -iname ‘*mit*'
Note: ‘-iname’ indicates case insensitive operation and ‘-name’
indicates case sensitive operation.

Typical output of this command could be,

/usr/share/doc/mit_intro.doc These two error messages


/usr/share/doc/something/examples/events_at_Mit.doc indicate that the user who runs
find: `/run/udisks2': Permission denied this command does not have
find: `/run/wpa_supp': Permission denied access to these two files or
/usr/share/clubs_MIT_progress.txt directories. These error messages
/usr/games/mit_game1 are written to STDERR.

To hide stderr, we can redirect these error messages by referencing standard


error's file descriptor number, 2, and a "redirect output" operator, >.
15
Example (Contd..)
This command is to find from ‘/’ all files and
directories with name ‘*mit*’ whether the file
find / -iname ‘*mit*’ 2> errors.txt
name is in smaller case or upper case. Error
messages will be recorded in a file ‘errors.txt’.

/usr/share/doc/mit_intro.doc
/usr/share/doc/something/examples/events_at_Mit.doc
/usr/share/clubs_MIT_progress.txt
/usr/games/mit_game1

This command displays the contents of


cat errors.txt errors.txt on STDOUT (terminal screen)

find: `/run/udisks2': Permission denied


find: `/run/wpa_supp': Permission denied

16
Commands (Text Processing)
awk Pattern scanning and processing language
cat Display file(s)
cut Extract selected fields of each line of a file
diff Compare two files
grep Search text for a pattern
head Display the first 10 lines of a file
less Display files on a page-by-page basis
sed Stream editor (esp. search and replace)
sort Sort text files
split Split files
tail Display the last 10 lines of a file
tr Translate/delete characters
uniq Filter out repeated lines in a file
wc Line, word and character count
17
printenv command
Try the following:
printenv > env_variables (creates a file called env_variables)
cat env_variables
wc –l env_variables
printenv | wc –l
printenv | head shows the first 10 lines from the output of printenv
printenv | tail shows the last 10 lines from the output of printenv
printenv | more shows page by page view of the output

18
Services –
Starting and
Stopping
What is a Service in Linux?
A service is a piece of operating system code used to handle some type
of request.

System administrators are responsible for working with services


(starting or stopping them).

A service is also known as daemon (pronounced ‘demon’).

20
Categories of Linux Services
The Linux services can be broken down into several different categories such as,
• Boot
• File system
• Hardware
• Language support
• Logging
• Network, web/Internet
• Power management
• Scheduling
• System maintenance

Some services support a range of requests while other services must combine to
perform their task.
21
Features or characteristics of a service
1. It runs in the background so that it does not take up processor time
unless called upon
2. Services can handle requests that come from many different sources:
users, applications software, hardware, other operating system services,
messages from the network
3. Services are configurable. Configuration is usually handled through
configuration files, which are often stored in the /etc directory (or a
subdirectory).
4. Services can be running or stopped, and the system administrator can
control which services are running or stopped and which services are
automatically started at system initialization time based on the runlevel.

22
Services can be managed in two ways
1. Using systemd
2. Using init

23
systemd
• systemd is a Linux initialization system and service manager
• It includes features like on-demand starting of services (also known as
daemons), mount and automount point maintenance, snapshot
support, and processes tracking using Linux control groups.
• systemd provides a logging daemon and other tools and utilities to
help with common system administration tasks.
• In systemd, we can manage services with systemctl command.

Read: https://www.linode.com/docs/guides/what-is-systemd/

24
Listing all services that are ‘running’
systemctl | grep running

Sample output of the command


Source: https://itsfoss.com/start-stop-restart-services-linux/ 25
systemd - Start, Stop, Restart, & Check Status
systemctl start <service_name>

systemctl stop <service_name>

systemctl restart <service_name>

systemctl status <service_name>

26
Another way is to use init
• It is the first process executed by the kernel. It is a daemon process
which runs till the system is shutdown. That is why, it is the parent of
all the processes.
• init reads the script stored in the file /etc/inittab to take care of
everything that a system do at the time of system initialization like
setting the clock, initializing the serial port and so on.

Read: https://www.javatpoint.com/linux-init

27
Managing services with init
service <service_name> start

service <service_name> stop

service <service_name> restart

service <service_name> status

28
Finding and
killing
processes
with id and
name
Processes
Process is a running program. Processes can start other processes. When
we interact with Linux, we create numbered instances of running
programs called “processes.”
ps lists processes
ps –ef lists all processes running in the system

To get manual pages on ps command, try


man ps

30
More on processes
Linux assigns a process id (PID) to every process that is started. Whenever
a process runs, Linux keeps track of it through PID.
After booting, the first process is an initialization process called init. It is
given a PID of 1. From that point on, each new process gets the next
available PID.
If your Linux system has been running for a while, you might find PIDs that
are large (4 or 5 digits or even more).

31
Parent and Child Processes
A process can only be created by another process.
We refer to the creating process as the parent and the created process
as the child.
The parent process spawns one or more child processes.
The spawning of a process can be accomplished in one of several ways.
Each requires a system call (function call) to the Linux kernel. These
function calls are fork(), vfork(), clone(), wait(), and exec().

Question: What is a ‘Zombie’ process? (Hint: Read selected pages from the book
‘Linux with Operating System Concepts’ by Richard Fox

32
Active processes
top displays active processes (‘q’ to quit)

Use up and down arrow keys to scroll up and down the list of
processes.

33
34
Creating a shell program ‘countdown.sh’
Use an editor (vi or vim or nano). Add the following lines.
#!/bin/bash
echo "Coundown Program Started on " $(date)
declare j num
j=11
for ((i=1; i<=10; i++))
do
sleep 2
echo Waiting $(($j-$i)) seconds
done
echo "Countdown Program Ended on " $(date)

Make countdown.sh executable by running the command ‘chmod +x countdown.sh’

35
Running the shell program ‘countdown.sh’
Type ./countdown.sh at the $ prompt.
$ ./countdown.sh

36
Redirecting the output of ‘countdown.sh’
$ ./countdown.sh > out.txt
Wait for the program to end (wait for the $ prompt). Use ‘cat’to see the content of the file.

37
Killing a process with <ctrl>C
When you start a process (run a long program for example), you can
stop it using <ctrl>C

38
Running ‘countdown.sh’ in the background
Redirect the output to out.txt and run it as a background process. For this, use the
following command. (& is used to run a command in background)

$ ./countdown.sh > out.txt & (This will return the process id and
run it in the background)

39
Continued…
Try
$ ./countdown.sh > out.txt & Right after this, try the following
$ ps

1044 is the
process id of
countdown.sh.

40
Kill command (using process id)
& is used to initiate a background process

1180 is the
process id
of this
running
program

41
Killing a process immediately
-9 option can be used in kill command to send a signal that can terminate
any process immediately when attached with a PID or a process name. It is a
forceful way to kill or terminate a process or set of processes.

kill -9 <pid> / <processname>

kill -9 507

This command sends SIGKILL (9) — kill signal. This signal cannot be handled
(caught), ignored or blocked.

42
Killing a process by its name
Try killall countdown.sh OR pkill countdown.sh

More Info: https://linuxconfig.org/how-to-kill-a-process-by-name


43
44
Read
• https://itsfoss.com/how-to-find-the-process-id-of-a-program-and-kill-
it-quick-tip/
• https://itsfoss.com/kill-zombie-process-linux/
• https://www.javatpoint.com/kill-process-linux
• https://www.tecmint.com/find-and-kill-running-processes-pid-in-
linux/
• https://stackoverflow.com/questions/3510673/find-and-kill-a-
process-in-one-line-using-bash-and-regex
• Chapter 4 of “Linux with Operating System Concepts” by Richard Fox

45
Services and Processes in Linux
One of the most powerful features of Linux is its ability to manage
processes and services.
Processes are individual instances of a program that run on the system,
while services are background processes that provide various functions
to the system.

46
Package
installation
using RPM
and YUM
What is a package
It is a compressed software archive file containing all the files included
with a software application (pre-compiled binary software files,
installation scripts, configuration files, dependency requirements, and
other details about the software).

It can be a command-line utility, GUI application, or software library.

Most software applications of Linux are distributed as packages.

Read: https://www.scaler.com/topics/cyber-security/package-management-in-linux/

48
Types of packages
1. RPM packages (. rpm)
2. Debian packages (. deb)
3. TAR archives (. tar)
4. TGZ archives (. tgz)
5. GZip Archives (. gz)
Read: https://www.cbtnuggets.com/blog/certifications/open-source/the-5-linux-packaging-types-you-need-to-know

Linux administrators must be familiar with the various types of packaging formats that are used with Linux.

49
What is package management
Package management is a method of installing, updating, removing, and
keeping track of software updates from specific repositories (repos) in the
Linux system.

Linux distros often use different package management tools.

Ubuntu, Fedora, RedHat are examples of Linux distros


(The short form of ‘distributions’ is ‘distros’).

50
Repositories
We use software repositories (also called repos) to obtain packages.
Repositories are simply the location where the packages are stored,
commonly accessible through the internet.

A repository can contain a single package or thousands of packages.

Most Linux distributions have their own unique repositories.

51
Dependencies
In Linux, each package contains metadata about the additional
packages that are required. These additional packages are
called dependencies.

A single package can sometimes have hundreds of dependencies.


When installing, upgrading, or removing packages, these dependencies
may also need to installed, upgraded, and optionally removed.

52
Package Manager
A package manager is used to get (access and download), install, upgrade,
and remove packages and their dependencies.

It reduces the complexity and improves ‘ease of use’ because it automates


the process of obtaining, installing, upgrading, and removing packages and
their dependencies.

This dramatically improves the user experience and the ability to properly
and efficiently manage software on your Linux system.

53
Package Manager - Advantages
1. Ease of getting what you need - Easily obtain the correct, trusted, and
stable package for your Linux distribution.
2. Automatic dependency management - Automatically manage all
dependencies when installing/updating/uninstalling a package.
3. Standardized approach - Linux distributions have conventions or standards
regarding how applications are configured and stored in the /etc/ and
/etc/init.d/ directories. By using packages, distributions are able to enforce
a single standard.
4. Ease of applying patches and security upgrades – A single command to
automatically update all packages to the latest versions stored on the
configured repositories.
Read: https://www.linode.com/docs/guides/linux-package-management-overview/

54
RPM
RPM (Red Hat Package Manager) is a command-line utility for installing software
packages from an RPM file. Also, it helps in updating or removing software packages.
RPM packages contain software programs and libraries, as well as information about the
dependencies required to run those programs.
Key Features:
1. Dependency Resolution: Resolves dependencies during installation or update.
2. Rollback Support: Allows users to rollback to a previous version of a package if
needed.
3. Verification: Verifies package integrity to ensure that there is no tampering.
4. Scripting Support: Allows users to run scripts during package installation or removal.
5. Source Code Management: Can be used to build and manage source code packages.
55
RPM – Modes for package management
Every mode has its own set of options. Use ‘man rpm’ command to know more.

MODE DESCRIPTION GENERAL OPTIONS PURPOSE

-i Installs a package --version Prints version number

-U Upgrades a package -v Prints verbose output

-e Erases a package --help Prints help


COMMAND EXAMPLES:
-V Verifies a package
rpm -i package-file
-q Queries a package rpm -U package-file
rpm -iv package-file

56
YUM
YUM (Yellowdog Updater Modified) is a package management system used in Linux distributions
like Fedora, CentOS, and Red Hat Enterprise Linux. YUM is a command-line tool. It is a front-end
to the RPM package manager and provides a user-friendly interface for managing software
packages. It uses a repository-based system, where software packages are downloaded from
remote repositories and installed on the local system.
Key Features:
1. Dependency Resolution: Resolves dependencies during installation or update.
2. Rollback Support: Allows users to rollback to a previous version of a package if needed.
3. Automatic Updates: Can automatically update packages to their latest available version.
4. Plugin Support: Allows users to extend its functionality using plugins.
5. Repository Management: Helps manage remote repositories and configure their settings.

Read: https://sysadminxpert.com/difference-between-yum-and-rpm/ 57
YUM
COMMAND PURPOSE OPTIONS PURPOSE
yum install Installs the specified packages
-C Runs from system cache
remove Removes the specified packages
Includes packages that provide
--security
Searches package metadata for a fix for a security issue
search
keywords
-y Answers yes to all questions
info Lists description
Skips packages causing
Updates each package to the latest --skip-broken
update problems
version
repolist Lists repositories -v Verbose

history Displays history COMMAND EXAMPLES


yum install firebox
COMMAND FORMAT
yum remove firebox
yum -option command
yum update firebox
yum list installed

58
References
• https://www.javatpoint.com/linux-package-manager
• https://www.javatpoint.com/rpm-command-in-linux
• https://www.redhat.com/sysadmin/how-manage-packages
• https://www.tecmint.com/20-practical-examples-of-rpm-commands-in-linux/
• https://www.tecmint.com/20-linux-yum-yellowdog-updater-modified-
commands-for-package-mangement/
• How to Install VIM Editor on Linux (RHEL / CentOS 7/8) Using 6 Easy Steps |
CyberITHub
• https://en.wikipedia.org/wiki/Yum_(software)
• https://en.wikipedia.org/wiki/RPM_Package_Manager
• https://phoenixnap.com/kb/rpm-vs-yum
59
Summary
• Redirection & Filters, Simple Filter and Advance Filter commands
• Start and Stop Services
• Find & Kill a process with id and name
• Package installation using RPM and YUM

60
Any Questions?

61
Git installation

This is to set sudo password

This is to
install git

62
Signing in as superuser using su

63
Thank You

64

You might also like