Open Source and Linux3
Open Source and Linux3
Despite the importance of open source, OSS is not without its share of challenges. Let us take an objective look at
the pros and cons of open source software to decide if it is right for you:
Pros of open source software
The benefits of open source software are:
1. Lower cost
Open source projects are typically developed and maintained by a community of volunteers who share their skills
and knowledge for free. This means anyone can access and use the software without paying hefty licensing fees or
upfront costs.
Moreover, since the source code is available to everyone, businesses can modify and customize the software
according to their specific needs, eliminating the need for expensive proprietary software as a service (SaaS)
solutions.
2. Ease of integration
Unlike closed source software, which often requires proprietary application programming interfaces (APIs) and
protocols, open source software promotes interoperability and compatibility with other systems. This means
developers can seamlessly integrate different components or modules from various sources without facing major
hurdles.
3. No vendor lock-in
With OSS, there is no vendor lock-in. This means users are not tied to a single provider or limited by proprietary
formats or protocols. Developers are free to modify and customize the software according to their specific needs
without any restrictions imposed by a single vendor.
Additionally, it ensures that users can easily switch between different vendors if they find one better suited to their
requirements or if they encounter any issues with the current provider. This flexibility ultimately leads to cost
savings and improved efficiency in the long run.
4. Greater flexibility
Developers have access to the underlying source code, giving them the freedom to make any necessary changes or
enhancements. This flexibility empowers organizations and individuals to tailor the software to meet their unique
requirements, whether adding new features or integrating with other systems. The ability to customize open
source software makes it a versatile solution for various applications across various industries.
This inherent flexibility in open source software ensures that businesses can build agile IT infrastructures that
evolve alongside their changing needs—a valuable advantage that closed source alternatives may not offer.
5. The power of community
Unlike closed source software, where development and improvement are limited to a specific team or company,
open source projects benefit from a global community of developers, users, and enthusiasts. The power of
community lies in its ability to harness the collective intelligence of passionate individuals who share a common
goal: creating high-quality software accessible to all.
Cons of open source software
Open source software, despite its many advantages, also has some drawbacks that users should be aware of:
1. No prompt support
Open source software often relies on community forums and user-driven discussions for troubleshooting and
problem-solving. While these forums can be helpful in many cases, they may not always provide immediate or
timely responses to users’ queries or issues. This can be frustrating for individuals or businesses requiring urgent
technical assistance.
It’s important to consider this aspect before choosing open source software, especially if your organization requires
a high level of technical support and quick resolution for any potential problems that may arise during usage.
2. Complexity and difficult learning curve
Unlike closed source software, which often comes with user-friendly interfaces and comprehensive documentation,
open source software might require some technical expertise to navigate effectively.
The sheer flexibility and customization options offered by open source software can lead to higher complexity.
Users may need to invest time in understanding the intricacies of the software’s framework and architecture,
configuration settings, and dependencies. This can be daunting for those unfamiliar with coding or with limited
technical knowledge.
3. Limited features
Open source software (OSS) offers numerous benefits, but it’s important to acknowledge that there can also be
limitations. One of these is the potential for limited features in certain OSS projects. This is because OSS
development relies on community contributions and volunteer efforts, which may result in prioritizing core
functionalities over additional features.
4. High maintenance needs
Since these projects rely on community contributions and updates, they require regular upkeep to ensure
compatibility and security. This includes regularly monitoring for updates, bug fixes, and security patches. Open
source applications can become vulnerable to security breaches or compatibility issues with other software
components without proper maintenance.
Additionally, because multiple contributors are involved in the development of open source projects, coordinating
updates can be more complex compared to closed source alternatives, where a single vendor oversees all aspects
of maintenance.
5. Less stable builds
The open nature of OSS means that anyone can modify and update the code, which can lead to unintended
consequences. As a result, users may experience occasional crashes or glitches while using open source
applications. However, passionate developers within the community work diligently to address these issues and
release patches and updates to improve stability over time.
Open Source Software Examples
There are numerous examples of open source software that have made a significant impact in the tech industry.
These include:
VLC Media Player: VLC Media Player, developed by VideoLAN, is a highly popular open source media player that
supports a wide range of audio and video formats. With its humble beginnings in 1996 as an academic project at
École Centrale Paris, VLC has become one of today’s most widely used media players.
GIMP: GIMP, short for GNU Image Manipulation Program, is a powerful open source image editing software. The
history of GIMP dates back to 1995 when Spencer Kimball and Peter Mattis developed the software as a class
project at the University of California, Berkeley. Over the years, it has evolved into a highly versatile tool used by
graphic designers, photographers, and artists worldwide.
Mozilla Firefox: Mozilla Firefox is a popular web browser that has gained immense popularity among users
worldwide. It was created by the Mozilla Foundation and was initially released in 2004 as an open source project.
Linux: Linux is a powerful open source operating system that has revolutionized the computing world. Developed
by Linus Torvalds in 1991, Linux was initially created as an alternative to proprietary operating systems like
Windows and macOS. It is based on the Unix operating system and is designed to run on a wide range of devices,
from servers to Raspberry Pi.
Apache HTTP Server: The Apache HTTP Server, commonly known as Apache, was created by a group of developers
in 1995 to provide a robust and reliable platform for hosting websites. Since its inception, it has become one of the
most popular web servers globally, commanding over half of the market share.
Takeaway
An Introduction to Linux
Introduction
The Linux operating system is an extremely versatile Unix-like operating system, and has taken a clear lead in the
High Performance Computing (HPC) and scientific computing community. Linux is a multi-user, preemptive
multitasking operating system that provides a number of facilities including management of hardware resources,
directories, and file systems, as well as the loading and execution of programs. A vast number of utilities and
libraries have been developed (mostly free and open source as well) to accompany or extend Linux.
There are two major components of Linux, the kernel and the shell:
1. The kernel is the core of the Linux operating system that schedules processes and interfaces directly with the
hardware. It manages system and user I/O, processes, devices, files, and memory.
2. The shell is a text-only interface to the kernel. Users input commands through the shell, and the kernel receives the
tasks from the shell and performs them. The shell tends to do four jobs repeatedly: display a prompt, read a
command, process the given command, then execute the command. After which it starts the process all over again.
It is important to note that users of a Linux system typically do not interact with the kernel directly. Rather, most
user interaction is done through the shell or a desktop environment.
Unix
The Unix operating system got its start in 1969 at Bell Laboratories and was written in assembly language. In 1973,
Ken Thompson and Dennis Ritchie succeeded in rewriting Unix in their new language C. This was quite an audacious
move; at the time, system programming was done in assembly in order to extract maximum performance from the
hardware. The concept of a portable operating system was barely a gleam in anyone's eye.
The creation of a portable operating system was very significant in the computing industry, but then came the
problem of licensing each type of Unix. Richard Stallman, an American software freedom activist and programmer
recognized a need for open source solutions and launched the GNU project in 1983, later founding the Free
Software Foundation. His goal was to create a completely free and open source operating system that was Unix-
compatible or Unix-like.
Linux
In 1987, the source code to a minimalistic Unix-like operating system called MINIX was released by Andrew
Tanenbaum, a professor at Vrije Universiteit, for academic purposes. Linus Torvalds began developing a new
operating system based on MINIX while a student at the University of Helsinki in 1991. In September of 1991,
Torvalds released the first version (0.1) of the Linux kernel.
Torvalds greatly enhanced the open source community by releasing the Linux kernel under the GNU General Public
License so that everyone has access to the source code and can freely make modifications to it. Many components
from the GNU project, such as the GNU Core Utilities, were then integrated with the Linux kernel, thus completing
the first free and open source operating system.
Linux has been adapted to a variety of computer systems of many sizes and purposes. Furthermore, different
variants of Linux (called Linux distributions) have been developed over time to meet various needs. There are now
hundreds of different Linux distributions available, with a wide variety of features. The most popular operating
system in the world is actually Android, which is built on the Linux kernel.
Why Linux
Linux has been so heavily utilized in the HPC and scientific computing community that it has become the standard
in many areas of academic and scientific research, particularly those requiring HPC. There have been over 40 years
of development in Unix and Linux, with many academic, scientific, and system tools. In fact, as of November 2017,
all of the TOP500 supercomputers in the world run Linux!
Linux has four essential properties which make it an excellent operating system for the science community:
Performance – Performance of the operating system can be optimized for specific tasks such as running small
portable devices or large supercomputers.
Functionality – A number of community-driven scientific applications and libraries have been developed under
Linux such as molecular dynamics, linear algebra, and fast-Fourier transforms.
Flexibility – The system is flexible enough to allow users to build applications with a wide array of support tools
such as compilers, scientific libraries, debuggers, and network monitors.
Portability – The operating system, utilities, and libraries have been ported to a wide variety of devices including
desktops, clusters, supercomputers, mainframes, embedded systems, and smart phones.
Files and Processes
Everything in Linux is considered to be either a file or a process:
A process is an executing program identified by a unique process identifier, called a PID. Processes may be short in
duration, such as a process that prints a file to the screen, or they may run indefinitely, such as a monitor program.
A file is a collection of data, with a location in the file system called a path. Paths will typically be a series of words
(directory names) separated by forward slashes, /. Files are generally created by users via text editors, compilers, or
other means.
A directory is a special type of file. Linux uses a directory to hold information about other files. You can think of a
directory as a container that holds other files or directories; it is equivalent to a folder in Windows or macOS.
A file is typically stored on physical storage media such as a disk (hard drive, flash disk, etc.). Every file must have a
name because the operating system identifies files by their name. File names may contain any characters, although
some special characters (such as spaces, quotes, and parenthesis) can make it difficult to access the file, so you
should avoid them in filenames. On most common Linux variants, file names can be as long as 255 characters, so it
is convenient to use descriptive names.
Files can hold any sequence of bytes; it is up to the user to choose the appropriate application to correctly interpret
the file contents. Files can be human readable text organized line by line, a structured sequence only readable by a
specific application, or a machine-readable byte sequence. Many programs interpret the contents of a file as having
some special structure, such as a pdf or postscript file. In scientific computing, binary files are often used for
efficiency storage and data access. Some other examples include scientific data formats like NetCDF or HDF which
have specific formats and provide application programming interfaces (APIs) for reading and writing.
The Linux kernel is responsible for organizing processes and interacting with files; it allocates time and memory to
each process and handles the file system and communications in response to system calls. The Linux system uses
files to represent everything in the system: devices, internals to the kernel, configurations, etc.
Shells
A variety of different shells are available for Linux and Unix, each with pros and cons. While bash (updated version
of sh) and tcsh (descended from C-shell/csh) are the most common shells, the choice of shell is entirely up to user
preference and availability on the system. In most Linux distributions, bash is the default shell.
The purpose of a shell is to interpret commands for the Operating System (OS) to execute. Since bash and other
shells are scripting languages, a shell can also be used for programming via scripts. The shell is an interactive and
customizable environment for the user.
All examples in this tutorial use the bash shell.
Text Editors
A text editor is a tool to assist the user with creating and editing files. There is no "best" text editor; it depends on
personal preferences. Regardless of your typical workflow, you will likely need to be proficient in using at least one
common text editor if you are using Linux for scientific computing or similar work. Two of the most widely used
command-line editors are Vim and Emacs, which are available via the vim and emacs commands, respectively, in
systems where they are installed.
Linux File System
A file system is a structured method of storing and managing data—including files, directories, and metadata—on
your machine. Think of it like a library. If thousands of books were scattered around, finding one would be hard. But
in an organized structure, like labeled shelves, locating a book becomes easy.
This article aims to simplify the complexities of Linux file systems, guiding beginners through their layers,
characteristics, and implementations. By shedding light on these nuances, we empower users to make informed
choices in navigating the dynamic landscape of Linux operating systems.
What is the Linux File System
A Linux file system is a set of processes that controls how, where, and when data is stored or retrieved from
storage devices. It manages data systematically on disk drives or partitions, and each partition in Linux has its own
file system because Linux treats everything as a file, including devices and applications.. Like Windows uses C: and
D: drives, Linux uses mount points, but everything appears under the root / directory. In Linux, everything is treated
as a file, including devices and applications.
Linux File System Structure
The architecture of a file system comprises three layers mentioned below.
1. Logical File System:
The Logical File System acts as the interface between the user applications and the file system itself. It facilitates
essential operations such as opening, reading, and closing files. Essentially, it serves as the user-friendly front-end,
ensuring that applications can interact with the file system in a way that aligns with user expectations.
2. Virtual File System:
The Virtual File System (VFS) is a crucial layer that enables the concurrent operation of multiple instances of
physical file systems. It provides a standardized interface, allowing different file systems to coexist and operate
simultaneously. This layer abstracts the underlying complexities, ensuring compatibility and cohesion between
various file system implementations.
3. Physical File System:
The Physical File System is responsible for the tangible management and storage of physical memory blocks on the
disk. It handles the low-level details of storing and retrieving data, interacting directly with the hardware
components. This layer ensures the efficient allocation and utilization of physical storage resources, contributing to
the overall performance and reliability of the file system.
Together, these layers form a cohesive architecture, orchestrating the organized and efficient handling of data in
the Linux operating system.
Architecture Of a File
System
Characteristics of a File System
A file system defines the rules and structures for how data is organized, stored, accessed, and managed on a
storage device.
Space Management: How the data is stored on a storage device. Pertaining to the memory blocks and
fragmentation practices applied in it.
Filename: A file system may have certain restrictions to file names such as the name length, the use of special
characters, and case sensitive-ness.
Directory: The directories/folders may store files in a linear or hierarchical manner while maintaining an index table
of all the files contained in that directory or subdirectory.
Metadata: For each file stored, the file system stores various information about that file's existence such as its data
length, its access permissions, device type, modified date-time, and other attributes. This is called metadata.
Utilities: File systems provide features for initializing, deleting, renaming, moving, copying, backup, recovery, and
control access of files and folders.
Design: Due to their implementations, file systems have limitations on the amount of data they can store.
Linux File Systems:
Here are some linux file systems:
Types of File System in Linux
1) ext (Extended File System):
Implemented in 1992, it is the first file system specifically designed for Linux. It is the first member of the ext family
of file systems.
2) ext2:
The second ext was developed in 1993. It is a non-journaling file system that became known for its efficient
handling of flash drives and SSDs. It solved the problems of separate timestamp for access, inode modification and
data modification. Due to not being journaled, it is slow to load at boot time.
3) Xiafs:
Also developed in 1993, Xiafs was developed as an alternative but lacked the power and functionality of ext2. Due
to limited features and scalability, it is no longer in use.
4) ext3:
Introduced in 1999, ext3 brought in journaling capabilities, offering improved reliability. Unlike ext2, it avoided
long boot-time checks after an improper shutdown. It also supported online file system growth and HTree
indexing, making it efficient for large directories.
5) JFS (Journaled File System):
First created by IBM in 1990, the original JFS was taken to open source to be implemented for Linux in 199 it is
Known for its ability to perform well under varied loads JFS performs well under different kinds of load but is not
commonly used anymore due to the release of ext4 in 2006 which gives better performance.
6) ReiserFS:
It is a journal file system developed in 2001. Despite its earlier issues, it has tail packing as a scheme to reduce
internal fragmentation. It uses a B+ Tree that gives less than linear time in directory lookups and updates. It was the
default file system in SUSE Linux till version 6.4, until switching to ext3 in 2006 for version 10.2.
7) XFS:
XFS is a 64-bit journaling file system and was ported to Linux in 2001. It now acts as the default file system for many
Linux distributions. It provides features like snapshots, online defragmentation, sparse files, variable block sizes,
and excellent capacity. It also excels at parallel I/O operations.
8) SquashFS:
Developed in 2002, this file system is read-only and is used only with embedded systems where low overhead is
needed.
9) Reiser4:
It is an incremental model to ReiserFS. It was developed in 2004. However, it is not widely adapted or supported on
many Linux distributions.
10) ext4:
The fourth ext developed in 2006, is a journaling file system. It has backward compatibility with ext3 and ext2 and it
provides several other features, some of which are persistent pre-allocation, unlimited number of subdirectories,
metadata checksumming and large file size. ext4 is the default file system for many Linux distributions and also has
compatibility with Windows and Macintosh.
11) btrfs (Better/Butter/B-tree FS):
It was developed in 2007. It provides many features such as snapshotting, drive pooling, data scrubbing, self-
healing and online defragmentation. It is the default file system for Fedora Workstation.
12) bcachefs:
This is a copy-on-write file system that was first announced in 2015 with the goal of performing better than btrfs
and ext4. Its features include full filesystem encryption, native compression, snapshots, and 64-bit check summing.
13) Others:
Linux also has support for file systems of operating systems such as NTFS and exFAT, but these do not support
standard Unix permission settings. They are mostly used for interoperability with other operating systems.
File Systems Comparison:
Please note that there are more criteria than the ones listed in the table. This table is supposed to give you an idea
of how file systems have evolved.
File Systems
Parameters
ext ext2 Xiafs ext3 JFS ReiserFS XFS Reiser4 ext4 btrfs
Max. 4032
filename 255
255 255 248 255 255 255 3976 255 255
length character
(bytes) s
Allowable
Any
characters
except except except except Unicode except except except except except
in directory
NUL NUL, / NUL NUL or / except NUL or / NUL NUL, / NUL, / NUL, /
entries
NUL
(Any byte)
Max.
Undefine Undefine Undefine Undefine Undefine Undefine Undefine Undefine Undefine
pathname Undefined
d d d d d d d d d
length
Max. file 16GB - 16GB - 8TB (on
2 GB 64MB 4PB 8TB 8EB 16GB - 16TB 16EB
size 2TB 2TB x86)
Max. 2TB - 2TB -
2 GB 2GB 32PB 16TB 8EB - 1EB 16EB
volume size 32TB 32TB
Max. no. of
- - - - - - - - 2^32 2^64
files
Metadata
only No No No Yes Yes Yes Yes No Yes No
journaling
Compressio
No No No No No No No Yes No Yes
n
Block sub-
No No No No Yes Yes No Yes No Yes
allocation
Online No No - Yes No Yes Yes Yes Yes Yes
File Systems
Parameters
ext ext2 Xiafs ext3 JFS ReiserFS XFS Reiser4 ext4 btrfs
grow
Yes
Encryption No No No No No No No Yes (experiment No
al)
Checksum No No No No No No Partial No Partial Yes
Observations:
We see that XFS, ext4 and btrfs perform the best of all the other file systems. In fact, btrfs looks as if it's almost the
best. Despite that, the ext family of file systems has been the default for most Linux distributions for a long time.
So, what is it that made the developers choose ext4 as the default rather than btrfs or XFS? Since ext4 is so
important for this discussion, let's describe it a bit more.
ext4 in Linux File System
Ext4 was designed to be backward compatible with ext3 and ext2, its previous generations. It's better than the
previous generations in the following ways:
It provides a large file system as described in the table above.
Utilizes extents that improve large file performance and reduces fragmentation.
Provides persistent pre-allocation which guarantees space allocation and contiguous memory.
Delayed allocation improves performance and reduces fragmentation by effectively allocating larger amounts of
data at a time.
It uses HTree indices to allow unlimited number of subdirectories.
Performs journal checksumming which allows the file system to realize that some of its entries are invalid or out of
order after a crash.
Support for time-of-creation timestamps and improved timestamps to induce granularity.
Transparent encryption.
Allows cleaning of inode tables in background which in turn speeds initialization. The process is called lazy
initialization.
Enables writing barriers by default. Which ensures that file system metadata is correctly written and ordered on
disk, even when write caches lose power.
There are still some features in the process of developing like metadata checksumming, first-class quota supports,
and large allocation blocks.
However, ext4 has some limitations. Ext4 does not guarantee the integrity of your data, if the data is corrupted
while already on disk then it has no way of detecting or repairing such corruption. The ext4 file system cannot
secure deletion of files, which is supposed to cause overwriting of files upon deletion. It results in sensitive data
ending up in the file-system journal.
XFS performs highly well for large filesystems and high degrees of concurrency. So XFS is stable, yet there's not a
solid borderline that would make you choose it over ext4 since both work about the same. Unless you want a file
system that directly solves a problem of ext4 like having capacity > 50TiB.
Btrfs on the other hand, despite offering features like multiple device management, per-block checksumming,
asynchronous replication and inline compression, does not perform the best in many common use cases as
compared to ext4 and XFS. Several of its features can be buggy and result in reduced performance and data loss.
Some Hands On Example on Linux File System
For example, if our use_case is to set up a server that will first store and serve large multimedia files (videos and
audios). In that case we have to prioritize efficient speed and use of storage space.
According to this requirement the XFS file system would be a better choice. Because we know that XFS is optimized
for large files and can work on high volumes of data transfer which in general makes it ideal for media servers.
Following steps to use it:
Step 1: Installing XFS utilities package on Linux system.
sudo apt-get install xfsprogs
Installing xfsprogs
Step 2: Create a partition to format as XFS.
For example: `/dev/sda1`
This can be done using tool like `fdisk`.
Step 3: Format the partition as XFS.
sudo mkfs.xfs /dev/sda1 -f
Format the partition
We have formatted partition using XFS filesystem. (Used -f for forcefully to avoid error or warning) .
Step 4: Mount the XFS partition to a directory we want.
sudo mount /dev/sda1 /mnt/jayesh_xfs_partition
mounting of XFS partition
We have mounted XFS partition to a directory `/mnt/jayesh_xfs_partition`, (you can create your own directory.)
Step 5: To verify the mount.
df -h
Successful mount
Linux File Hierarchy Structure
The Linux File Hierarchy Structure or the Filesystem Hierarchy Standard (FHS) defines the directory structure and
directory contents in Unix-like operating systems. It is maintained by the Linux Foundation.
In the FHS, all files and directories appear under the root directory /, even if they are stored on different physical or
virtual devices.
Some of these directories only exist on a particular system if certain subsystems, such as the X Window System, are
installed.
Most of these directories exist in all UNIX operating systems and are generally used in much the same way;
however, the descriptions here are those used specifically for the FHS and are not considered authoritative for
platforms other than Linux.
linux-directory
1. / (Root):
At the top of every Linux file system is the root directory represented by a forward slash /. It’s the base point, and
no directory exists above it. If you look at the file system graphically, you’ll see all other directories branching from
this root directory.
Every single file and directory start from the root directory.
The only root user has the right to write under this directory.
/root is the root user’s home directory, which is not the same as /
Only the root user has permission to modify contents inside this directory. Regular users cannot make changes
here. For example, if you attempt to create a file in / as a non-root user, you’ll encounter a "Permission Denied"
error.
root-linux-directory
2. /bin :
The /bin directory contains essential commands and binaries needed by all users, including cp, ls, ssh, and kill.
These commands are universally available across user types.
Contains binary executables.
Common linux commands you need to use in single-user modes are located under this directory.
Commands used by all the users of the system are located here e.g. ps, ls, ping, grep, cp
bin-linux-directory
3. /boot :
This directory stores all files required for booting the system. It includes the GRUB bootloader configuration and
essential kernel files that are loaded during startup.
Kernel initrd, vmlinux, grub files are located under /boot
Example: initrd.img-2.6.32-24-generic, vmlinuz-2.6.32-24-generic
boot-linux-directory
4. /dev :
Device files in Linux are stored in the /dev directory. These are special files that act as interfaces between hardware
and software. Device files are of two types: block devices (e.g., hard drives) and character devices (e.g.,
microphones and speakers). Examples include /dev/sda1 for disk partitions.
These include terminal devices, usb, or any device attached to the system.
Example: /dev/tty1, /dev/usbmon0
dev-linux-directory
5. /etc :
Short for "Editable Text Configuration," /etc contains configuration files for system applications, users, services, and
tools or it contains the Host-specific system-wide configuration files. For example, user details like UID and local
addresses are defined here.
This also contains startup and shutdown shell scripts used to start/stop individual programs.
Example: /etc/resolv.conf, /etc/logrotate.conf.
etc-linux-directory
6. /home :
Every non-root user has a personal directory inside /home. For example, if your username is anshu, your personal
directory would be /home/anshu. Each user can create, delete, or modify files only in their own home directory and
cannot access others’ home directories.
Home directories for all users to store their personal files, containing saved files, personal settings, etc..
example: /home/kishlay, /home/kv
home-linux-directoryhome-linux-directory
7. /lib:
Applications require shared libraries to run, which are stored in /lib. These include dynamic libraries needed during
runtime. For example, Apache server libraries are available here.
Library filenames are either ld* or lib*.so.*
Example: ld-2.11.1.so, libncurses.so.5.7
lib-linux-directory
8. /media:
Devices like USBs, CDs, and pen drives are mounted under /media. For example, when a CD-ROM is inserted
(appeared in FHS-2.3), its details will appear here.
Temporary mount directory for removable devices.
Examples, /media/cdrom for CD-ROM; /media/floppy for floppy drives; /media/cdrecorder for CD writer
media-linux-directory
9. /mnt :
When external drives are connected, they are temporarily mounted in /mnt. This is where their contents become
accessible to the system.
Temporary mount directory where sysadmins can mount filesystems.
mnt-linux-directory
10. /opt :
Third-party software and packages not part of the default system installation are stored in /opt. It includes their
configuration and data files.
Contains add-on applications from individual vendors.
Add-on applications should be installed under either /opt/ or /opt/ sub-directory.
opt-linux-directory.
11. /sbin :
Essential system binaries, e.g.,
This directory holds administrative binaries like iptables, firewall management tools, fsck, init, route etc. These
binaries are primarily for system administrators and typically require root privileges to execute.
Just like /bin, /sbin also contains binary executables.
The linux commands located under this directory are used typically by system administrators, for system
maintenance purposes.
Example: iptables, reboot, fdisk, ifconfig, swapon
sbin-linux-directory
12. /srv :
Site- specific data served by this system, such as data and scripts for web servers, data offered by FTP servers, and
repositories for version control systems.
srv stands for service.
Contains server specific services related data.
Example, /srv/cvs contains CVS related data.
srv-linux-directory
13. /tmp :
Programs create temporary files during execution, and these are stored in /tmp. These files are deleted
automatically after the program finishes or when the system is restarted.
Directory that contains temporary files created by system and users.
Files under this directory are deleted when the system is rebooted.
tmp-linux-directory
14. /usr :
Secondary hierarchy for read-only user data; contains the majority of (multi-)user utilities and applications.
Contains binaries, libraries, documentation, and source-code for second level programs.
/ usr/bin contains binary files for user programs. If you can’t find a user binary under /bin, look under /usr/bin. For
example: at, awk, cc, less, scp
/usr/sbin contains binary files for system administrators. If you can’t find a system binary under /sbin, look under
/usr/sbin. For example: atd, cron, sshd, useradd, userdel
/usr/lib contains libraries for /usr/bin and /usr/sbin
/ usr/local contains user's programs that you install from source. For example, when you install apache from
source, it goes under /usr/local/apache2
/usr/src holds the Linux kernel sources, header-files and documentation.
usr_bin-linux-directoryusr_sbin-linux-directoryusr_lib-linux-dirctoryusr_local-linux-directory
usr_src-linux-directory
15. /proc:
The /proc directory provides detailed information about system processes. Each process is assigned a unique ID and
represented as a directory inside /proc. For example, /proc/meminfo gives real-time data about memory usage
including total, free, buffer, and cache statistics.
Contains information about system process.
This is a pseudo filesystem that contains information about running processes. For example: /proc/{pid} directory
contains information about the process with that particular pid.
This is a virtual filesystem with text information about system resources. For example: /proc/uptime
proc-linux-directory
proc-linux-directory
Linux Introduction
Overview of Linux and Why it is so popular?
Linux is an open-source operating system (OS) that runs on a computer and controls all of the other software and
hardware on the computer. Initially, Linux was built on the Unix operating system, which was created many
decades ago as a platform for scientific and academic computing. It is used on a wide range of devices, including
servers, desktop computers, laptops, cloud computing, and Mobile devices.
As Linux is open-source software so anyone can access its source code and make changes to it. Other than this
Linux is known for its Security features. It has a large community of users and developers who are constantly
working to identify and fix security vulnerabilities. Also, Linux is well known for its Customizability allowing users to
modify and configure it in order to meet their specific needs.
Linux Architecture Components
The main architectural components that work together to form a complete Linux OS include Kernel, System
Libraries, User Space, Shell, System Utilities, Daemons, and Configuration files.
Kernel: The kernel is the central part of the Linux OS. It manages the computer's hardware resources and
communicates with the software to provide a stable and consistent interface for us.
System Libraries: System libraries are collections of pre-written code that provides common functions for other
programs to use.
User Space: User space mainly refers to the programs and applications that run on top of the operating system.
This includes a wide range of applications such as text editors, web browsers, games, etc.
Shell: Shell is a command-line interface that allows users to interact with the Linux operating system.
System Utilities: System utilities are small programs that perform specific tasks such as file management, process
management, network management, etc.
Daemons: Daemons are the background processes that run continuously and perform specific tasks such as
network services, log management, etc.
Configuration files: Configuration files are used to configure various aspects of the Linux operating system such as
the network settings, system settings, security settings, etc.
Linux Distributions
Linux distributions, also known as "distros," are different versions of the Linux operating system that are built and
packaged with specific versions of the architectural components, the package management system used, the
graphical user interface, and the tools and applications included in it.
Examples of Linux distros include Ubuntu, Fedora, Debian, CentOS, Mint, Red Hat Enterprise Linux (RHEL), etc.
Linux File Systems
The Linux file system is the way that the operating system organizes and stores files and directories on a
computer's hard drive or other storage devices. It consists of a hierarchical structure where files and directories are
arranged in a tree-like format starting from the root directory (represented by "/"). Each file and directory has a
unique location within the file system hierarchy, and these locations can be referred to using a path similar to how
file paths work in other operating systems.
There are several types of file systems available for use in Linux including below:
Ext4: This is the most widely used file system in Linux and it is known for its high performance, reliability, and
scalability. It is a journaling file system meaning that it keeps track of changes made to the file system in a log that
helps to ensure that data is not lost in the event of a system failure.
XFS: This is a high-performance file system that is optimized for large-scale data storage and is commonly used in
enterprise and high-performance computing environments. It supports large file sizes and high-bandwidth I/O
which makes it well-suited for use with high-speed storage devices such as solid-state drives.
Btrfs: This is a newer file system that is designed to be flexible, scalable, and easy to manage. It offers advanced
features such as snapshots which allow administrators to easily backup and restore data and it also supports data
and metadata checksums which help to prevent data corruption.
NTFS: This is a file system commonly used in Microsoft Windows and it is supported by most modern Linux
distributions. It is designed to be compatible with a wide range of devices including hard drives and removable
storage devices and it supports advanced features such as compression and encryption.
FAT32: This is an older file system that is commonly used in older Microsoft Windows systems and is also widely
used for removable storage devices such as USB drives and has limitations such as a maximum file size of 4GB.
(Note: The machine from which the screenshot has been taken is having CentOS 8 Stream)
Linux Commands
whereis - Find the path of that executable file.
List operation
ls - Shows list.
o -a - Hidden file.
o -l - Permission.
o -R - Show sub dir.
Changing dir operation
cd <folder-name> - Change Dir.
cd .. - Go one Directory back.
cd - Go to home.
cd ../<foldername> - Open a previous dir folder.
cd <path> - Open a dir with the path.
File/Folder Ope.
mkdir <new-dir-name> - Create a new folder.
mkdir -p test/test1/test2 - Create a dir between two directories.
touch <new-file-name> - create a blank file.
pwd - Present working directory.
cat <filename> - Display file content.
cat > <new-file-name> - Create a file.
dd if=/dev/zero of=bos_dosya bs=4G count=1- create empty file with zeros
cat >> <filename> - Append the file
cat <filename1> >> <filename2> - Append the content of the file filename1 at the end of the file
<filename2> .
cat <filename> <filename2> - Display 2 files at a time.
cat <filename> <filename2> > <newfile-name> - Merge both of file content in a single one.
cat <file-name> | tr > <new-file-name> - Translate the file.
cut -c 1-2 <filename> - cut the file column wise
echo "Hello" >> <file-name>
man <commad name> - Know about the command usages and options.
man <commad name> - know about the command.
File/Folder operation
cp <file-name> <new-fie-name> - Make a copy of a file in the current location.
mv <file-name> <dir-path> - Move a file from one dir to another.
mv <file-name> <new-fie-name> - Rename a file.
mv -R <dir-name> <dir-path> - Move Dir
rm <file-name> - Remove a file permanently.
rm -R <file-name> - Delete a folder with dir included.
head <file-name> - Will display first 10 lines of a file.
tail <file-name> - Will display last 10 lines of a file. --n 2 - will display last 2 lines.
diff <file-1> <file-2> - Show diff between the two files.
locate <file> - To find out the file.
find <file/folder-name> - Find a file/folder.
find <dir-name> - Find files inside the dir
find .-type d - Show only dir.
o .-type f - show only files.
o .-type f -name "*.txt" - Show only files with that specific name.
o .-type f -iname "*.txt" - Show only files with that specific name - not case sensitive (i)
o .-type f -mmin -20 - Show files which modify less than 20 min ago.
o .-type f -mmin +20 - show files which modify more than 20 min ago.
o .-type f -maxdepth 2 - Will only show 1 folder deep.
o .-size +1k - will only show file/folder with size of 1kb
System commands
ps aux - processes which are running
systemctl [option] [service] - interact with a process
o We can do 4 option with systemctl
start
stop
enable
disable
o Example, systemctl start apache2
df - Check the capacity and storage details.
o m - In megabyte or
o hg - In gigabyte.
du - Disk usages capcity
o -h (human readable)
echo - Get a output of a string
echo $PATH - Check the path variable
sudo - Admin command
sudo chown root text.txt - change owner
!<command-name> - Run the previous command
git add .; git commit -m "message" - Run multiple commands at a time
sort <file-name>" - sort the file
job - show the jobs
wget <url> - download the file from the URL
top - what processes are running
kill <process-id> -stop that process
Uname - show the system info
zip <file-1> <file-2> - Zip Two or more files
Unzip <file-name> - Unzip files
useradd <name> - add a user
passwd <name> - set a password for the user
uname -<flag> -o -m -r
lscpu - get cpu details
free - free memory
vmstat - virtual memory
lsof - list all the open file
xdg-open <file-fath> - open the folder (graphical window) of a file/folder with path.
xdg-open . - open the folder of the current directory.
vi ~/.bashrc - set your Alias
echo -n 'username' | base64 - encode the username to base64
echo -n 'encoded' | base64 -d - decode the username to base64
Networking
nslookup google.com - To check the IP address of the domain.
netstat - To check the network status.
hostname - To check the hostname.
whoami - To check the current user.
ping google.com - To check the connectivity.
Permissions
chmod u=rwx,g=rxw,o=rwx <file-name> READ, WRITE AND EXECUTE
chmod 777 <file-name> - 4- Read, 2- Write, 1 - Execute
find . -perm 777 - shows files with all permissions(rwx)
grep <keyword> <file-name> - To search if the keyword is presnt in the file or not
grep -w <keyword> <file-name> - To search if the keyword is present in the file or not (complete word)
grep -i <keyword> <file-name> - To search if the keyword is present in the file or not (not case sens)
grep -n <keyword> <file-name> - To search if the keyword is present in the file or not (Line number)
grep -B <keyword> <file-name> - Show Line before that keyword
grep -win <keyword> ./*.txt - To search if the keyword is present in the file in current dir
grep -win -r <keyword> .*
history | grep "ls -l" - Piping, we filter out the things
history
!<number-from-history>
Operators
ping google.com & ping facebook.com - run both the commands at the same time
echo "google.com" && echo "facebook.com" - second will only run if first is successful
echo "google.com" && {echo "facebook.com"; eco "pradumnasaraf.co"}
echo "google.com" || echo "pingfacebook.com" - second will only run if first is not successful
rm -r !(file.txt) - delete all files except file.txt
printevnv - to print all th env.
Unit10:An IntroductiontoLinuxOS
Linux is a true 32/64-bit operating system that run on different platforms. It is a multi-user, multi-tasking and
time sharing operating system. Linux is a very stable OS and is used in many application areas, such as by
Internet Service Providers (ISPs) and as a network OS for providing various services. The Linux OS derived its
name from its creator, Linux Torvalds. Linux started writing the Linux kernel in 1991, as a hobby, and created
one of the most powerful operating systems available today. Perhaps the most important factor for the
immense success of Linux is that it is free. It can be copied and redistributed without having to pay any fee or
royalty.
Lesson1:OverviewofLinuxOperatingSystem
LearningObjectives
On completion ofthis lesson youwill beable toknow:
TheHistoryofLinuxOperatingSystem
FeaturesofLinuxOperating System
PrivilegeofLinuxOperatingSystem
A Briefhistoryof Linux
All modern operating systems have their roots in 1969 when Dennis Ritchie and Ken Thompson developed the
C language and the UNIX operating system atAT&T BellLabs.Theysharedtheirsourcecodewith the rest of the
world, including the hippies in Berkeley California. By 1975, when AT&T started selling UNIX commercially,
HistoryofLinux
about half of the source code was written by others. The hippies were not happy that a commercial company
sold software that they had written;theresulting(legal)battleendedintherebeingtwo versions of UNIX in the
Seventies: the official AT&T UNIX, and the free BSD UNIX.
IntheEightiesmanycompaniesstarteddevelopingtheirownUNIX:IBM created AIX, Sun SunOS (later Solaris), HP
HP-UX and about a dozen other companies did the same. The result was a mess of UNIX dialects and a dozen
different ways to do the same thing. And here is the first real root of Linux, when Richard Stallman aimed to
end this era of UNIX separation and everybody re- inventingthewheelbystarting the GNU project (GNU is Not
Unix). His goal was to make an operating system that was freely available to everyone, and where everyone
could work together (like in the Seventies). Many of the command line tools that you use today on Linux or
Solaris are GNU tools.
The Nineties started with Linus Torvalds, a Swedish speaking Finnish student, buying a 386 computer and
writing a brand new POSIX compliant kernel. He put
thesourcecodeonline,thinkingitwouldneversupportanythingbut386
AnIntroductiontoLinuxOS
Hierarchical FileSystem
Linuxprovidesastandardfilestructureinwhichsystemfiles/userfilesare arranged.
Multiprogramming
Linux is a multiprogramming system means multiple applications can run at same time.
Time-Sharing
Multi-programmingismadepossibleon theLinuxsystembytime-sharingfeature.
Multi-Tasking
A program in Linux is broken down into tasks, each task is something like reading from or writing to the disk, or
waiting for input from a user. The ability of any as to handle the execution of multiple tasks is known as multi-
tasking.
Multi-User
Linux is a multiuser system means multiple users can access system resources like memory, ram, and application
programs at same time.
OpenSource
Linux source code is freely available and it is community based development project. Multiple teams’ works in
collaboration to enhance the capability of Linux operating system and it is continuously evolving.
Licensing
Linux is copyrighted under the GNU General Public License. The licensing for Red Hat Linux states that a person
can make any number of copies of the software and distribute it freely or charge a price for it. One can freely
download Linux from the Internet for use.
TheAdvantageofLinuxOperating System
Cost
The most important advantage of using Linuxis the fact that it is free to obtain. Linuxdistribution can be
installed on any number of computers, without paying a single penny.
Reliability
UsersontheLinuxoperatingsystemworkconsistentlywiththeLinuxserver, without reporting any operating system
failures.
200
AnIntroductiontoLinuxOS
BackwardCompatibility
Linuxis perfect for those old computers with barely any processing power or
memory.Thereareendlesspossibilitiestofindold 386or486computerswith barely any RAM run Linux without any
issue.
SimpleUpgrade andInstallationProcess
The installation procedure of most Linux versions is menu-driven. It includes the ability to upgrade from prior
versions. The upgrade process preserves the existing configuration files and maintains a list of its actions during
installation.
GUI Interface
The graphical interface forLinuxistheXWindowsystem.Itisdividedintotwosub systems consisting of a server and
a client. Linux has a number of graphical user interfaces called Desktop Environments, such as K Desktop
Environment (KDE)and GNU Object Model Environment (GNOME), both of which are versions of theX Window
system. They run on the X server.
ExcellentSecurityFeatures
Security- Linux provides user security using authentication features like password protection/ controlled access
to specific files/ encryption of data. This is the reason why many Internet Service Providers (ISPs) are replacing
their current OS with Linux systems.
201
AnIntroductiontoLinuxOS
Exercises
Multiplechoice questions
a. TheUNIXoperatingsystemwasfirstdevelopedat
(i) Berkeley
(ii) AT&TBelllaboratories
(iii) Sun Microsystems
(iv) Microsoftcorporation
Questionsforshortanswers
a) WhoisLinus Torvalds?Mentionhiscontribution in the historyof Linux.
b) WhenUNIXwas invented andwho were the inventors?
c) Whatdoyoumean bySamba?
d) WhatistheUtilityofCRONScheduler?
e) WhydoesISP (InternetServiceProvider)preferLinuxtootherOS?
Analyticalquestions
a) NarratehistoryofUNIX.
b) DiscusssomefeaturesofLinuxOS.
c) MentiontheadvantagesofLinuxOS.
202
AnIntroductiontoLinuxOS
Lesson2:StructureandComparisonofLinux
LearningObjectives
On completion ofthis lesson youwill beable toknow:
AboutLinux Architecture
DifferencebetweenLinuxand UNIX
DifferencebetweenLinuxand Windows
AboutdistributionofLinux
LinuxArchitecture
The Linux operating system consists of three main software components as shown below:
LinuxArchitecture
ComponentsoftheLinuxOS
Kernel
Thekernel is a program that constitutes the central core of a computer operating system. It has
complete control over everything that occurs in the system. Since the kernel communicates directly
with the hardware, the parts of the kernel must be customized to the hardware features of each
system. However, the kernel does not deal directly with a user. Instead, the login process starts up a
separate, interactive program, called the shell, for each user.
Shell
A shell isaprogramthatprovidesthetraditional,text-onlyuser interface forLinuxand other Unix-
likeoperating systems. Its primary function is to readcommandsthataretypedintoa console(i.e.,anall-
textdisplaymode)orterminal window (an all-text window) in a GUI (graphical user interface) and then
execute (i.e., run) them. The features of the shell will be discussed in lesson 2 unit 4.
203
AnIntroductiontoLinuxOS
Linux was developed considering UNIX as a reference model.Thisiswhy;thebasic architecture and most of the
features of Linux and UNIX are the same. Actually, Linux is also considered another version of UNIX. The main
difference between Linux and UNIX is that Linux is FREE. Various distributors of Linux charges aprice, which is
quite low as compared to other operating systems.
The Linux operating system requires at least 850 MB hard disk space, 64MBMemory for Red hat Linux 9.0.
ComparisonbetweenLinuxandUNIX
Most of the organizations having small, medium, or large networks require a few basic services such as file and
print services, E-mail access, Internet access, and Intranet services. Most of the tasks that you are wanting to
execute on a Windows machine can also be executed on a Linux machine.
The following table compares some of the common applications used on the Linux and Windows operating
systems.
ComparisonbetweenLinuxand Windows
Service Linux Windows
Web Server Apache InternetInformation Server
204
AnIntroductiontoLinuxOS
ThedistributorofLinux
RedHat
Red Hat is a billion dollar commercial Linux Company that puts a lot of effort in developing Linux. They have
hundreds of Linux specialists and are known for their excellent support. They give their products (Red Hat
Enterprise Linux and Fedora) away for free. While Red Hat Enterprise Linux (RHEL) is well tested before
release and supported for up to seven years after release, Fedora is a distro with faster updates but without
support.
Ubuntu
Canonical started sending out free compact discs with Ubuntu Linux in 2004 and
quicklybecamepopularforhomeusers(manyswitchingfromMicrosoftWindows). Canonicalwants Ubuntu to be
aneasy to use graphicalLinux desktop without need to ever see a command line. Of course they also want to
make a profit by selling support for Ubuntu.
Debian
ThereisnocompanybehindDebian.Insteadtherearethousandsofwell organised developers that elect a Debian
Project Leader every two years. Debian is seen asone of themoststableLinuxdistributions.Itisalsothebasisof
everyrelease of Ubuntu. Debian comes in three versions: stable, testing and unstable. Every Debian release is
named after a character in the movie Toy Story.
205
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
UNIT-I
LINUXUTILITIESANDSHELLPROGRAMMING
I. Introduction toLinux:
Linux is a Unix-like computer operating system assembled under the model of free and opensource
software development and distribution. The defining component of Linux is the Linuxkernel, an operating
system kernel first released 5 October 1991 by Linus Torvalds.
Linux was originally developed as a free operating system for Intel x86-based personal computers. It has
since been ported to more computer hardware platforms than any other operatingsystem. It isa
leadingoperatingsystemon serversandother big ironsystemssuchas mainframe computers and
supercomputers more than 90% of today's 500 fastestsupercomputers run some variant of Linux,
including the 10 fastest. Linux also runs on embedded systems (deviceswhere the operating
systemistypically built into the firmware and highly tailored to the system) such as mobile phones, tablet
computers, network routers, televisions and video game consoles; the Android system in wide use on
mobile devices isbuilt on the Linux kernel.
Basic Features
FollowingaresomeoftheimportantfeaturesofLinuxOperatingSystem.
Portable - Portability means softwares can works on different types of hardwares in
sameway.Linuxkernelandapplicationprogramssupportstheir installationonanykind of hardware platform.
Open Source - Linux source code is freely available and it is community based development
project.Multipleteamsworksincollaborationto enhancethecapability of Linux operating system and it is
continuously evolving.
Multi-User-Linuxisamultiusersystemmeansmultipleuserscanaccesssystem resources like memory/
ram/ application programs at same time.
Multiprogramming - Linux is a multiprogramming system means
multiple applications can run at same time.
HierarchicalFileSystem -Linuxprovidesastandard filestructureinwhichsystem
1
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
files/userfilesarearranged.
Shell - Linux provides a special interpreter program which can be used to execute
commandsoftheoperatingsystem.It canbeused todo varioustypesofoperations,call application programs
etc.
Security -Linuxprovidesuser securityusingauthenticationfeatureslikepassword protection/ controlled
access to specific files/ encryption of data.
LinuxAdvantages
1. Lowcost: Youdon’t needto spendtimeand moneytoobtainlicensessinceLinuxandmuch
ofitssoftwarecomewiththeGNUGeneralPublic License.Youcanstarttoworkimmediately without worrying
that your software may stop working anytime because thefree trial version expires. Additionally, there
are large repositories from which you canfreely download high quality software for almost any task you
can think of.
2. Stability:Linuxdoesn’t needtoberebootedperiodicallyto maintainperformance levels. Itdoesn’t
freeze up or slow downovertime due tomemoryleaks and such. Continuous up- times of hundreds of
days (up to a year or more) are not uncommon.
3. Performance: Linux provides persistent high performance on workstations and
onnetworks.Itcanhandleunusually largenumbersofuserssimultaneously,andcanmake oldcomputers
sufficiently responsive to be useful again.
4. Network friendliness: Linux was developed by a group of programmers over theInternet and
has therefore strong support for network functionality; client and server systems can be easily set up
on any computer running Linux. It can perform tasks such as network backups faster and more reliably
than alternative systems.
5. Flexibility: Linux can be used for high performance server applications, desktop applications,
and embedded systems. You can save disk space byonly installing the componentsneeded
foraparticularuse.Youcanrestrict theuseofspecificcomputersby installing for example only selected
office applications instead of the whole suite.
6. Compatibility:It runsallcommonUnixsoftwarepackagesandcanprocessallcommonfile formats.
7. Choice: The large number of Linux distributions gives you a choice. Each distribution is
developedandsupportedbyadifferentorganization. You canpicktheoneyou likebest;the
2
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
corefunctionalitiesarethesame;mostsoftwarerunsonmostdistributions.
8. Fastandeasyinstallation: MostLinuxdistributionscomewithuser-friendlyinstallation and setup
programs. Popular Linux distributions come with tools that make installation of additional software
very user friendly as well.
9.Fulluseofhard disk:Linuxcontinuesworkwellevenwhenthehard diskisalmostfull.
10. Multitasking: Linuxisdesignedto domanythingsatthesametime;e.g.,alargeprinting job in the
background won’t slow down your other work.
11. Security: Linux is one of the most secure operating systems. “Walls” and flexible file access
permission systems prevent access by unwanted visitors or viruses. Linux users have to option to select
and safely download software, free of charge, from online repositories
containingthousandsofhighqualitypackages.Nopurchasetransactionsrequiringcredit card numbers or
other sensitive personal information are necessary.
12. OpenSource: Ifyou develop softwarethat requiresknowledgeor modificationofthe
operatingsystemcode,Linux’ssourcecodeisat yourfingertips.MostLinuxapplications are Open Source
as well.
DifferencebetweenUNIXandLINUX
3
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
byvarious vendors.
10
4
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
5
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
10.5)/x86(10.4)/x64(10.5-10.8)
LinuxDistribution(OperatingSystem)Names
Afewpopularnames:
1.RedhatEnterpriseLinux
2.FedoraLinux
3.DebianLinux
4.SuseEnterpriseLinux
5.Ubuntu Linux
CommonThingsBetween Linux&UNIX
Bothsharemanycommonapplicationssuchas:
1.GUI,file, andwindowsmanagers(KDE, Gnome)
2.Shells(ksh,csh,bash)
3.VariousofficeapplicationssuchasOpenOffice.org
6
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
4.Developmenttools(perl,php, python,GNUc/c++compilers)
5.Posixinterface
LayeredArchitecture:
LinuxSystemArchitectureisconsistsoffollowinglayers
Hardwarelayer-Hardwareconsistsofallperipheraldevices(RAM/HDD/ CPUetc).
Kernel-CorecomponentofOperatingSystem,interactsdirectlywithhardware, provides low level services to
upper layer components.
Shell - An interface to kernel, hiding complexityofkernel's functions fromusers. Takes commands from
user and executes kernel's functions.
Utilities-Utilityprogramsgivingusermostofthefunctionalitiesofanoperating systems.
LINUXFilesystem
Linux file structure files are grouped according to purpose. Ex: commands, data files,
documentation. Parts of a Unix directory tree are listed below. All directories are grouped under
the root entry"/". That part of the directorytree is left out of the below diagram.
7
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
1. /–Root
Everysinglefile anddirectorystartsfromthe root
directory.
Onlyrootuserhaswriteprivilegeunderthis
directory.
Pleasenotethat/rootis root user’shomedirectory,whichisnotsameas
/.
2. /bin–UserBinaries
Containsbinary
executables.
Common linux commands you need to use in single-user modes are located under this
directory.
Commandsusedbyalltheusers ofthe systemarelocated
here.
Forexample:ps,ls,ping,grep,
cp.
3. /sbin– SystemBinaries
Justlike/bin,/sbinalsocontainsbinary
executables.
But,thelinuxcommandslocatedunderthisdirectoryareusedtypicallybysystem aministrator, for system
maintenance purpose.
Forexample:iptables,reboot,fdisk, ifconfig,
swapon
4. /etc –ConfigurationFiles
8
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
Containsconfigurationfilesrequiredbyall
programs.
This also contains startup and shutdown shell scripts used to start/stop individual
programs.
Forexample:/etc/resolv.conf,
/etc/logrotate.conf
5. /dev–Device Files
Containsdevice
files.
Theseincludeterminaldevices,usb, oranydeviceattached tothe
system.
Forexample:/dev/tty1,
/dev/usbmon0
6. /proc–ProcessInformation
Containsinformationaboutsystem
process.
Thisisapseudofilesystemcontainsinformationaboutrunningprocess.For
example:
/proc/{pid}directorycontains informationabouttheprocesswiththatparticular pid.
Thisisavirtualfilesystemwithtextinformationaboutsystemresources.For
example:
/proc/uptime
7. /var–VariableFiles
varstandsforvariable
files.
Contentofthefilesthatareexpected togrow canbefoundunderthis
directory.
This includes — system log files (/var/log); packages and database files (/var/lib);emails (/var/mail); print
queues (/var/spool); lock files (/var/lock); temp files needed across reboots (/var/tmp);
8. /tmp– TemporaryFiles
Directorythatcontainstemporaryfilescreatedbysystemand
users.
Files underthis directoryaredeletedwhensystemis
rebooted.
9. /usr–UserPrograms
Containsbinaries,libraries,documentation,andsource-codeforsecondlevel
programs.
/usr/bincontainsbinaryfilesforuserprograms.Ifyoucan’tfindauserbinary
under
/bin,lookunder/usr/bin.Forexample:at,awk,cc,less,scp
/usr/sbin contains binary files for system administrators. If you can’t find a system binaryunder /sbin, look
under /usr/sbin. For example: atd, cron, sshd, useradd, userdel
/usr/libcontains librariesfor/usr/binand
/usr/sbin
/usr/localcontainsusersprogramsthatyouinstallfromsource.Forexample,when
you
9
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
installapachefromsource,itgoesunder/usr/local/apache2
10. /home–Home Directories
Homedirectoriesfor alluserstostoretheir personal
files.
Forexample:/home/john,
/home/nikita
11. /boot – Boot LoaderFiles
Containsbootloaderrelated
files.
Kernelinitrd,vmlinux,grubfilesarelocatedunder
/boot
Forexample:initrd.img-2.6.32-24-generic,vmlinuz-2.6.32-24-
generic
12. /lib–SystemLibraries
Containslibraryfilesthatsupportsthebinaries locatedunder/binand
/sbin
Libraryfilenamesareeitherld*or
lib*.so.*
Forexample:ld-2.11.1.so,
libncurses.so.5.7
13. /opt–Optionaladd-onApplications
optstandsfor
optional.
Containsadd-onapplicationsfromindividual
vendors.
add-onapplicationsshouldbeinstalledundereither/opt/or/opt/sub-
directory.
14. /mnt–Mount Directory
Temporarymountdirectorywheresysadminscanmount
filesystems.
15. /media–RemovableMediaDevices
Temporarymount directoryforremovable
devices.
For
examples, /media/cdrom for CD-ROM; /media/floppy for floppy drives;
/media/cdrecorderforCD writer
16. /srv– ServiceData
srvstandsfor
service.
Containsserverspecificservicesrelated
data.
Forexample,/srv/cvscontainsCVSrelated
data.
1. LinuxUtilities:
FileHandling utilities:
Cat
10
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
Command:
cat linuxcommandconcatenates filesandprintitonthestandard output.
SYNTAX:
TheSyntax is
cat [OPTIONS] [FILE]...
OPTIONS:
-A Showall.
-b Omitsline numbersforblankspaceinthe output.
-eA$characterwillbeprintedattheendofeachlinepriorto anew line. -E Displays a $ (dollar
sign) at the end of each line.
-n Linenumbersforalltheoutputlines.
-sIftheoutputhasmultipleemptylinesit replacesit with oneemptyline. -T Displays the tab
characters in the output.
Non-printingcharacters(withtheexceptionoftabs,new-linesandform-feeds)
-vare printedvisibly.
Example:
ToCreateanewfile: cat >
file1.txt
Thiscommandcreatesanewfilefile1.txt.Aftertypingintothefilepresscontrol+d (^d)
simultaneously to end the file.
2. Todisplaya
11
J.B.INSTITUTE OF
ENGINEERINGAND
TECHNOLOGY
LinuxProgrammingNotes
file:cat
file1.txt
Thiscommanddisplaysthedatainthe file.
12
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
The above cat command will concatenate the two files (file1.txt and file2.txt) and it will displaythe
output inthe screen. Some timesthe output maynot fit the monitor screen. In such situation you
can print those files in a new file or display the file using less command.
catfile1.txtfile2.txt|less
rm COMMAND:
rmlinuxcommandisused toremove/deletethefilefromthedirectory.
SYNTAX:
TheSyntax is
rm[options..] [file|directory]
OPTIONS:
13
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
cdCOMMAND:
cdcommandisusedtochangethedirectory.
SYNTAX:
TheSyntax is
cd [directory|~|./|../|-]
OPTIONS:
-L Usethephysicaldirectorystructure.
-P Forcessymboliclinks.
EXAMPLE:
1. cdlinux-command
Thiscommandwilltake youtothesub-directory(linux-command) fromitsparent directory.
14
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
2. cd..
Thiswillchangetotheparent-directoryfromthecurrentworkingdirectory/sub-directory.
3. cd~
cpCOMMAND:
cpcommandcopyfiles fromone locationto another.Ifthedestinationisanexisting file, then the file is
overwritten; if the destination is an existing directory, the file is copied into the directory (the directory is
not overwritten).
SYNTAX:
TheSyntax is
cp[OPTIONS]...SOURCEDEST
cp[OPTIONS]...SOURCE...DIRECTORY
cp[OPTIONS]...--target-directory=DIRECTORYSOURCE...
OPTIONS:
-a sameas-dpR.
--backup[=CONTROL] makea backup ofeachexisting destinationfile
-b like--backupbutdoesnotacceptanargument.
ifanexistingdestinationfilecannotbeopened,removeitandtry
-f
again.
-p sameas--preserve=mode,ownership,timestamps.
15
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
Theabovecpcommandcopiesthecontentoffile1.phptofile2.php.
1. Tobackupthecopied file: cp -b
file1.php file2.php
Backupoffile1.phpwillbe createdwith'~'symbolasfile2.php~.
SYNTAX:
The
Syntaxis
ls[OPTIONS]... [FILE]
16
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
OPTIONS:
-l Listsallthefiles,directoriesandtheirmode,Numberoflinks,ownerofthe
file,filesize,Modifieddateandtimeandfilename.
-t Lists inorderoflastmodificationtime.
-a Listsallentries includinghiddenfiles.
-d Listsdirectoryfilesinsteadofcontents.
-p Putsslashattheendofeachdirectories.
-u Listinorderoflastaccess time.
-i Displayinodeinformation.
liststhecontentsofrootdirectory.
2. Displayinodeinformation:
ls-i
7373073book.gif
7373074clock.gif
7373082globe.gif
7373078pencil.gif
7373080child.gif
17
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
7373081email.gif
7373076indigo.gif
Theabovecommanddisplaysfilenamewithinodevalue.
lnCOMMAND:
lncommand isusedtocreatelinkto afile(or)directory.It helpstoprovidesoft link for desired files.
Inode will be different for source and destination.
SYNTAX:
The Syntaxis
ln[options]existingfile(ordirectory)namenewfile(ordirectory)name
OPTIONS:
-f Link fileswithoutquestioningtheuser,evenifthemodeoftargetforbids
writing.Thisisthedefaultifthestandardinputisnotaterminal.
-n Doesnotoverwriteexistingfiles.
-s Usedtocreatesoftlinks.
EXAMPLE:
1. ln-sfile1.txt file2.txt
Creates a symboliclink to'file1.txt' with the name of 'file2.txt'.Here inodefor 'file1.txt' and
'file2.txt' will be different.
2. ln-snimi nimi1
Createsasymboliclinkto'nimi'withthenameof'nimi1'.
chownCOMMAND:
chowncommand isusedto changetheowner/userofthe fileordirectory.Thisis an admin
command, root user only can change the owner of a file or directory.
SYNTAX:
TheSyntax is
chown[options]newownerfilename/directoryname
18
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
OPTIONS:
-R Changethepermissiononfilesthatareinthesubdirectoriesofthe directory
thatyouarecurrently in.
-c Changethepermissionforeachfile.
Preventschownfromdisplayingerrormessageswhenitisunableto change
-f
theownership ofafile.
EXAMPLE:
1. chownhioxtest.txt
Theowner ofthe'test.txt'fileisroot,Changetonewuserhiox.
2. chown-Rhioxtest
3. chown-c hioxcalc.txt
Herechangetheownerforthespecific'calc.txt'file only.
SecurityBy File
Permissionschmod
Command:
chmodcommandallows youtoalter/Changeaccessrightstofilesanddirectories.
FilePermissionisgivenforusers, groupandothersas,
ReadWrite Execute
19
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
000
SYNTAX:
TheSyntax is
chmod[options][MODE]FileName
FilePermission
# FilePermission
0 none
1 executeonly
2 writeonly
3 writeandexecute
4 readonly
5 readandexecute
6 readandwrite
7 setallpermissions
OPTIONS:
20
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
-v Outputversioninformationand exit.
EXAMPLE:
chmod777file1.txt
mkdirCOMMAND:
mkdircommandisusedtocreateoneor more directories.
SYNTAX:
TheSyntax is
mkdir[options]directories
OPTIONS:
1.Createdirectory:
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
mkdirtest
Theabovecommandisusedto createthedirectory'test'.
rmdirCOMMAND:
rmdircommandisusedtodelete/removeadirectoryand itssubdirectories.
SYNTAX:
TheSyntax is
rmdir[options..]Directory
OPTIONS:
Allowuserstoremovethedirectorydirnameanditsparentdirectorieswhich
-p
becomeempty.
EXAMPLE:
2. To deleteadirectorytree:
rm-irtmp
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
This command recursively removes the contents of all subdirectories of the tmpdirectory,
prompting you regarding the removal of each file, and then removes the tmp directory itself.
mv COMMAND:
mv command which is short for move. It is used to move/rename file fromone directoryto
another.mvcommand isdifferent fromcpcommandasit completelyremovesthe file fromthe source
and moves to the directoryspecified, where cp command just copies the content from one file to
another.
SYNTAX:
TheSyntaxis
mv[-f][-i]oldnamenewname
OPTIONS:
Thiswillnotpromptbeforeoverwriting(equivalentto--reply=yes). mv-fwill
-f
movethe file(s)withoutpromptingevenifitiswritingoveranexisting
target.-iPromptsbeforeoverwritinganotherfile.
EXAMPLE:
2. Tomoveadirectory
mvhscriptstmp
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
In the above line mv command moves all the files, directories and sub-directories from hscripts
folder/directoryto tmp directory if the tmp directoryalready exists. Ifthere is no tmp directory it
rename's the hscripts directory as tmp directory.
diffCOMMAND:
diffcommandisusedtofinddifferencesbetweentwofiles.
SYNTAX:
TheSyntaxis
diff[options..] from-fileto-file
OPTIONS:
-a Treatallfilesastextandcomparethemline-by-line.
-bIgnorechangesinamountofwhitespace. -c Use
the context output format.
-e Makeoutputthatisavalidedscript.
Useheuristicstospeed handlingoflargefilesthathavenumerousscattered
-H
smallchanges.
-iIgnorechangesincase;considerupper-and lower-caselettersequivalent.
PrintsinRCS-format,like -fexceptthateachcommandspecifiesthenumber
-n
oflinesaffected.
OutputRCS-formatdiffs;like-fexceptthateachcommandspecifiesthe
-q
numberoflinesaffected.
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
-r Whencomparingdirectories,recursivelycompareanysubdirectories found.
-s Reportwhentwofilesarethesame.
-wIgnorewhitespacewhencomparinglines. -y Use the side
by side output format.
EXAMPLE:
2c2
<hscripts.com
---
>HSCRIPTS.com
4d3
<Hioxindia.com
hscripts.com | HSCRIPTS.com
with friend ship withfriendship
Hioxindia.com <
HIOXTEST HIOXTEST
hscripts.com HSCRIPTS.com
withfriendship |withfriendship
chgrpCOMMAND:
chgrpcommand isusedtochangethegroupofthefileordirectory.Thisisan admin
command. Root user only can change the group of the file or directory.
SYNTAX:
TheSyntax is
chgrp[options]newgroupfilename/directoryname
OPTIONS:
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
-R Changethepermissiononfilesthatareinthesubdirectoriesofthedirectory
thatyouarecurrently in.
-c Changethepermissionforeachfile.
-f Force.Donotreporterrors.
Hioxindia.com <
EXAMPLE:
1. chgrphioxtest.txt
Thegroupof'test.txt'fileisroot,Change tonewgrouphiox.
2. chgrp-Rhiox test
3. chgrp -chioxcalc.txt
Theyabovecommandisusedtochangethegroupforthespecificfile('calc.txt')only.
About wc
Syntax
-c Countbytes.
-m Countcharacters.
-C Sameas -m.
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
-l Countlines.
Countwordsdelimitedbywhitespacecharactersornewlinecharacters.
DelimitingcharactersareExtendedUnixCode(EUC)charactersfromanycode set defined by iswspace()
File Nameoffiletoword count.
Examples
5 13 57myfile.txt
5=Lines
13=Words
57=Characters
About split
Split afile into pieces.
Syntax
andnofileswillbecreated.
Thepathnameoftheordinaryfileto besplit. Ifno input file isgivenorfile is -, the standard input will be used.
The prefixto be used for eachofthe files resulting fromthe split operation. If no nameargument
isgiven,xwillbeusedastheprefixoftheoutputfiles.The
combined lengthofthebasenameofprefixandsuffixlengthcannotexceed NAME_MAX bytes; see OPTIONS.
Examples
Aboutsettimeandtouch
Syntax
-c Donotcreateaspecifiedfileifitdoesnotexist. Donotwriteanydiagnostic
messagesconcerningthiscondition.
-m Changethemodificationtimeoffile.Donotchangetheaccesstimeunless-ais
alsospecified.
-r ref_file Usethecorrespondingtimesofthefilenamedbyref_fileinsteadofthecurrent
time.
oftheform:
[[CC]YY]MMDDhhmm[.SS]
-fref_file Usethecorrespondingtimesofthefilenamedbyref_fileinsteadofthecurrent
time.
Examples
settimemyfile.txt
Setsthefilemyfile.txtasthecurrenttime/date.
touchnewfile.txt
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
Aboutcomm
Selectorrejectlinescommontotwofiles.
Syntax
comm[-1][-2][-3]file1file2
-1 Suppresstheoutputcolumnoflinesuniquetofile1.
-2 Suppresstheoutputcolumn oflinesuniquetofile2.
-3 Suppresstheoutputcolumnoflinesduplicatedinfile1andfile2. file1
Name of the first file to compare.
file2 Nameofthesecondfileto compare.
Examples
commmyfile1.txtmyfile2.txt
Theaboveexamplewouldcomparethetwofiles myfile1.txtandmyfile2.txt.
Processutilities:
ps Command:
pscommandisusedtoreporttheprocessstatus. psistheshortnameforProcessStatus.
SYNTAX:
TheSyntaxis ps
[options]
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
OPTIONS:
-a Listinformationaboutallprocesses mostfrequentlyrequested:allthose
except processgroupleadersandprocessesnotassociatedwitha terminal..
-AoreListinformationforallprocesses.
-d Listinformationaboutallprocessesexceptsessionleaders.
-e Listinformationabouteveryprocessnowrunning.
-f Generatesafull listing.
-j PrintsessionIDandprocessgroup ID.
-l Generatealonglisting.
EXAMPLE:
1. ps
Output:
PIDTTY TIMECMD
2540pts/1 00:00:00bash
2621pts/1 00:00:00ps
2. ps-f
Output:
Displaysfullinformationaboutcurrentlyrunningprocesses.
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
killCOMMAND:
killcommandisusedtokillthebackground process.
SYNTAX:
TheSyntax is
kill[-s] [-l]%pid
OPTIONS:
-9 Forcetokillaprocess.
EXAMPLE:
StepbyStep process:
Open a process
music player. xmms
To kill a job or
process. kill 3956
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
Aboutnice
Invokesacommandwithanalteredschedulingpriority.
Syntax
nice[-increment|-nincrement]command[argument...]
The super-user may run commands with priority higher than normal by using a
negativeincrementsuchas-10.Anegativeincrementassignedby
Examples
Syntax
at executescommandsataspecifiedtime.
class.
-c Cshell.csh(1)isusedto executetheat-job.
-k Kornshell.ksh(1)isused toexecutethe at-job.
-f filename Specifiesthefilethatcontainsthecommandtorun.
-m Sendsmailoncethecommandhasbeen run.
-ttime Specifiesatwhattimeyouwantthecommandtoberan.Formathh:mm.am/pm
indicationcanalsofollowthetimeotherwisea24-hourclockisused.Atimezone
nameofGMT,UCTor ZULU(caseinsensitive)canfollowtospecifythatthe
timeis inCoordinatedUniversalTime.Othertimezonescanbespecifiedusing
theTZenvironmentvariable.Thebelowquicktimescanalsobe entered:
date Specifiesthedateyouwishittoberanon.Formatmonth,date,year.The
tomorrow-Indicatesthedayfollowing thecurrentday.
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
-l Liststhecommandsthathavebeensettorun.
Examples
at -m 01:35 < atjob = Run the commands listed in the 'atjob' file at 1:35AM, in addition all outputthat is
generated fromjob mailtothe user running the task. Whenthis command has been successfully enter you
should receive a prompt similar to the below example.
Diskutilities:
du(abbreviatedfromdiskusage)isastandard Unixprogramusedtoestimatefilespace usage—space used
under a particular directory or files on a file system.
du takesasingleargument,specifyingapathnamefor du towork;ifitisnotspecified,the current
directory is used. The SUS mandates for du the following options:
-a,displayanentryforeachfile(andnotdirectory)containedinthecurrentdirectory
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
-H,calculatediskusageforlinkreferencesspecifiedonthecommandline
-k,showsizesas multiplesof1024bytes, not512-byte
-L,calculatediskusage forlinkreferencesanywhere
-s,reportonlythesumoftheusageinthecurrentdirectory,notforeachfile
-x,onlytraverse filesanddirectoriesonthedeviceonwhichthepathnameargument is specified.
Other Unix and Unix-like operating systems mayadd extra options. For example, BSD and GNUduspecifya-
hoption, displayingdiskusage ina format easiertoreadbytheuser, adding units with the appropriate SI
prefix’
$du-sk*
152304directoryOne
1856548directoryTwo
Sumofdirectoriesinhuman-readableformat(Byte,Kilobyte,Megabyte,Gigabyte,Terabyteand Petabyte):
$du-sh*
149M directoryOne
1.8GdirectoryTwo
$du-sk.[!.]**|sort-n
$du-sk.[!.]**|sort–nr
Theweightofdirectories:
$ du-d1-c-h
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
dfcommand:Reportfilesystemdiskspaceusage
Dfcommandexamples-tocheckfree diskspace
Output:
Filesystem SizeUsed AvailUse%Mountedon
ducommand examples
Networkingcommands:
Theseare most usefulcommands inmylist while workingonLinuxserver,thisenables you to quickly
troubleshoot connection issues e.g. whether other system is connected or not ,
whetherotherhostisrespondingornotandwhileworkingforFIXconnectivityforadvanced trading system this
tools saves quite a lot of time .
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
ExampleofNetworkingcommandsin Unix
let's see some example of various networking command in Unix and Linux. Some of themare quite basic
e.g. ping and telnet and some are more powerfule.g. nslookup and netstat. When
youusedthesecommandsincombinationoffindandgrep youcanget anything youarelooking for e.g.
hostname, connection end points, connection status etc.
hostname
1. Toensurethatanetworkconnectioncanbeestablished.
2. Timinginformationasto thespeed oftheconnection.
ifconfig
Viewnetworkconfiguration,itdisplaysthecurrentnetworkadapterconfiguration.Itishandyto
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
netstat
Mostusefuland veryversatile forfindingconnectionto andfromthehost.Youcanfindout all the multicast
groups (network) subscribed by this host by issuing "netstat -g"
netstat -nap | grep port will display process id of application which is using that portnetstat-
aornetstat–allwilldisplayallconnectionsincludingTCPandUDP netstat --tcp or netstat –t will
display only TCP connection
netstat--udpornetstat–uwilldisplayonlyUDPconnectionnetstat
-gwilldisplayallmulticastnetworksubscribedbythis host.
nslookup
IfyouknowtheIPaddress it willdisplayhostname.TofindalltheIPaddresses foragiven domain name, the
command nslookup is used. You must have a connection to the internet for this utility to be useful.
E.g.nslookup blogger.com
traceroute
Ahandyutilityto viewthenumber ofhopsandresponsetimeto getto aremotesystemor web site is
traceroute. Again you need an internet connection to make use of this tool.
finger
Viewuser information,displaysauser’sloginname,realname,terminalnameandwritestatus. this is pretty old
unix command and rarely used now days.
telnet
Connectsdestinationhost viatelnet protocol, iftelnet connectionestablishonanyport means
connectivity between two hosts is working fine.
telnethostnameport willtelnethostname withtheportspecified.Normallyitisusedto see whether host
is alive and network connection is fine or not.
10Mostimportantlinuxnetworkingcommands
[fasil@smashtech]# arp
Address HWtypeHWaddress FlagsMask Iface
59.36.13.1 ether C eth0
[fasil@smashtech~]# /sbin/ifconfig
eth0UPBROADCASTRUNNINGMULTICASTMTU:1500Metric:1
RXpackets:126341errors:0dropped:0overruns:0frame:0 TX
packets:44441 errors:0 dropped:0 overruns:0 carrier:0 collisions:0
txqueuelen:1000
[fasil@smashtech~]# netstat
ActiveInternetconnections(w/oservers)
ProtoRecv-QSend-QLocalAddress ForeignAddress State
tcp0 0 .230.87:https ESTABLISHED
ActiveUNIXdomainsockets(w/oservers)
ProtoRefCntFlagsType State I-Node Path
unix10 [] DGRAM 4970/dev/log
unix2 [] DGRAM 6625@/var/run/hal/hotplug_socket
unix2 [] DGRAM 2952 @udevd
unix2 [] DGRAM 100564
unix3 [] STREAM CONNECTED 62438/tmp/.X11-unix/X0
unix3 [] STREAM CONNECTED 62437
unix3 [] STREAM CONNECTED 10271@/tmp/fam-root-
unix3 [] STREAM CONNECTED 10270
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
[root@smashtech~]#ping google.com
PINGgoogle.com(74.125.45.100)56(84)bytesofdata.
64 bytes from yx-in-f100.google.com (74.125.45.100): icmp_seq=0 ttl=241 time=295 ms 64 bytes
from yx-in-f100.google.com (74.125.45.100): icmp_seq=1 ttl=241 time=277 ms 64 bytes from yx-
in-f100.google.com (74.125.45.100): icmp_seq=2 ttl=241 time=277 ms
---google.comping statistics---
3packetstransmitted, 3received, 0%packetloss,time 6332ms
rttmin/avg/max/mdev=277.041/283.387/295.903/8.860ms,pipe2
5. Nslookup is a program to query Internet domain name servers. Nslookup has two modes:
interactive and non-interactive. Interactive mode allows the user to query name servers for
information about various hosts and domains or to print a list of hosts in a domain. Non-
interactivemodeisusedtoprintjustthenameandrequestedinformationforahostordomain.
[fasil@smashtech~]#nslookupgoogle.com
Server: server ip
Address: gatewayip 3
Non-authoritativeanswer:
Name: google.com
Address:209.85.171.100
Name: google.com
Address:74.125.45.100
Name: google.com
Address:74.125.67.100
6. dig (domain information groper) is a flexible tool for interrogating DNS name servers. It
performsDNS lookupsanddisplaystheanswersthat arereturned fromthenameserver(s)that were queried.
Most DNS administrators use dig to troubleshoot DNS problems because of its flexibility, ease ofuse and
clarityofoutput. Other lookuptools tend to have less functionality than dig.
[fasil@smashtech~]#diggoogle.com
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
;<<>> DiG9.2.4<<>>google.com
;;globaloptions:printcmd
;;Gotanswer:
;;->>HEADER<<- opcode:QUERY,status:NOERROR,id:4716
;;flags:qr rdra;QUERY:1, ANSWER:3, AUTHORITY:4,ADDITIONAL:4
;;QUESTIONSECTION:
;google.com. IN A
;;ANSWERSECTION:
google.com. 122 IN A 74.125.45.100
google.com. 122 IN A 74.125.67.100
google.com. 122 IN A 209.85.171.100
;;AUTHORITY SECTION:
google.com. 326567 IN NS ns3.google.com. google.com. 326567
IN NS ns4.google.com. google.com. 326567 IN NS ns1.google.com.
google.com. 326567 IN NS ns2.google.com.
;;ADDITIONALSECTION:
;;Querytime:92msec
;;SERVER:172.29.36.1#53(172.29.36.1)
;;WHEN: Thu Mar5 14:38:45 2009
;;MSGSIZErcvd: 212
7. Route manipulates the IProuting tables. Its primaryuse is to set upstatic routesto specific hosts or
networks via an interface after it has been configured with the ifconfigprogram.When the add or del
options are used, route modifies the routing tables. Without these options, route displays the current
contents of the routing tables.
[fasil@smashtech~]#route
Kernel IP routing table
Destination Gateway Genmask FlagsMetricRef Use Iface
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
Traceroute utilizes the IP protocol‘time to live’ field and attempts to elicit an ICMP
TIME_EXCEEDEDresponse fromeachgatewayalongthepathtosomehost.Theonly mandatoryparameter
is the destination host name or IP number. The default probe datagramlengthis40 bytes, butthis
maybe increasedbyspecifyingapacket length(in bytes) after the destination host name.
[fasil@smashtech~]# traceroutegoogle.com
traceroute:Warning:google.comhasmultipleaddresses;using209.85.171.100 traceroute to
google.com (209.85.171.100), 30 hops max, 38 byte packets
1***
9.W-displays information about the users currently on the machine, and their processes. The
headershows, inthis order,thecurrenttime, how longthesystemhasbeenrunning, howmany users are
currently logged on, and the system load averages for the past 1, 5, and 15 minutes.
[fasil@smashtechl~]# w
15:18:22up 4:38, 3users, loadaverage:0.89, 0.34,0.19
USER TTYFROM LOGIN@IDLEJCPUPCPU WHAT
root :0 - 10:41?xdm?24:53 1.35s/usr/bin/gnome-session
root pts/1 :0.0 10:58 1.00s 0.34s 0.00sw
root pts/2 :0.0 12:10 23:32 0.03s 0.03s bash
Filters:
moreCOMMAND:
morecommand isusedtodisplaytext intheterminalscreen.Itallowsonlybackward movement.
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
SYNTAX:
TheSyntax is
more[options]filename
OPTIONS:
-c Clearscreenbeforedisplaying.
-e Exitimmediatelyafterwriting thelastlineofthelastfileintheargument list.
-n Specifyhow manylinesareprintedinthescreenforagiven file.
+n Startsupthefilefromthe givennumber.
EXAMPLE:
1. more-c index.php
Clearsthescreenbeforeprintingthefile.
2. more -3 index.php
Printsfirstthreelinesofthegivenfile. PressEntertodisplaythefilelinebyline.
headCOMMAND:
headcommand isusedto displaythe firsttenlinesofafile,andalso specifieshowmany lines to display.
SYNTAX:
TheSyntax is
head[options]filename
OPTIONS:
-n Tospecifyhow manylinesyouwanttodisplay.
-nnumberThenumberoption-argumentmustbeadecimalintegerwhosesign affects
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
EXAMPLE:
1. headindex.php
Thiscommandprintsthefirst10linesof'index.php'.
2. head-5index.php
Theheadcommanddisplaysthefirst5linesof'index.php'.
3. head -c5index.php
Theabovecommanddisplaysthefirst5charactersof'index.php'.
tailCOMMAND:
tailcommand isusedtodisplaythe last orbottompartofthe file.Bydefault it displays last 10 lines of a
file.
SYNTAX:
TheSyntax is
tail[options]filename
OPTIONS:
Thenumberoption-argumentmustbeadecimalintegerwhosesignaffectsthe
-c number
locationinthefile,measured inbytes.
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
1. tailindex.php
Itdisplaysthelast10linesof'index.php'.
2. tail-2index.php
Itdisplaysthelast2linesof'index.php'.
3. tail-n5index.php
Itdisplaysthelast5linesof'index.php'.
4. tail-c5index.php
Itdisplaysthelast5charactersof'index.php'.
cutCOMMAND:
cut command isusedto cutout selected fieldsofeachlineofa file.Thecut command uses delimiters
to determine where to split fields.
SYNTAX:
The Syntax is
cut[options]
OPTIONS:
-c Specifiescharacterpositions.
-b Specifiesbytepositions.
-dflagsSpecifiesthedelimitersandfields.
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
EXAMPLE:
1. cut-c1-3text.txt
Output:
Thi
Cutthefirstthreelettersfromtheaboveline.
2. cut -d,-f1,2text.txt
Output:
Thisis, anexampleprogram
Theabovecommandisusedtosplitthefieldsusingdelimiterandcutthefirsttwofields.
pasteCOMMAND:
pastecommand isusedtopastethecontent fromone filetoanotherfile.Itisalso usedto set column
format for each line.
SYNTAX:
The Syntax is
paste[options]
OPTIONS:
1. pastetest.txt>test1.txt
2. ls|paste----
Listallfilesanddirectoriesinfourcolumnsforeach line.
sort COMMAND:
sortcommandisusedtosortthelinesinatextfile.
SYNTAX:
TheSyntax is
sort[options] filename
OPTIONS:
-r Sortsinreverseorder.
-u Iflineisduplicateddisplayonlyonce.
-ofilenameSendssortedoutputtoafile.
EXAMPLE:
1. sorttest.txt
Sortsthe'test.txt'fileandprintsresultinthe screen.
2. sort-rtest.txt
Sortsthe'test.txt'fileinreverseorderandprintsresultinthe screen.
Aboutuniq
Reportorfilteroutrepeatedlines inafile.
Syntax
theinput.
-d Suppressthewritingoflinesthatarenotrepeatedintheinput.
-u Suppressthewritingoflinesthatarerepeatedintheinput.
fieldsisapositivedecimalinteger.Afieldisthemaximalstringmatchedbythe
basicregularexpression:
[[:blank:]]*[^[:blank:]]*
usedfor comparison.
-schar Ignorethefirstcharscharacterswhendoingcomparisons,wherecharsisa
positivedecimalinteger.Ifspecifiedinconjunctionwiththe-foption,thefirst
charscharactersafterthefirstfieldsfieldswillbeignored.Ifcharsspecifiesmore
charactersthanremainonaninputline,anullstringwill beusedfor comparison.
-n Equivalentto -ffieldswithfieldssetton.
+m Equivalentto-scharswithcharssettom.
input_file Apathnameoftheinputfile.Ifinput_fileisnotspecified,oriftheinput_fileis-
, the
standardinputwillbeused.
output_file Apathnameoftheoutputfile.Ifoutput_fileisnotspecified,thestandardoutput
willbeused. Theresultsareunspecifiedifthefilenamedbyoutput_fileisthe
file named byinput_file.
Examples
uniqmyfile1.txt>myfile2.txt-Removesduplicatelinesinthefirstfile1.txtandoutputsthe
resultstothesecondfile.
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
Abouttr
Translate characters.
Syntax
tr[-c][-d][-s][string1][string2]
-c Complementtheset ofcharactersspecifiedbystring1.
-d Deletealloccurrencesofinputcharactersthatarespecifiedbystring1.
-s Replaceinstancesofrepeatedcharacterswithasinglecharacter.
Examples
echo "12345678 9247" | tr 123456789 computerh - this example takes an echo response of '12345678
9247' and pipes it throughthe tr replacing the appropriate numbers with the letters. In this example it
would return computer hope.
GeneralCommands:
dateCOMMAND:
datecommandprintsthedateand time.
SYNTAX:
TheSyntax is
date[options] [+format] [date]
OPTIONS:
canadjustthetime.
Format:
%a Abbreviated weekday(Tue).
%A Fullweekday(Tuesday).
%b Abbreviatedmonthname(Jan).
%B Fullmonthname(January).
%c Country-specificdateandtimeformat..
%D Dateintheformat%m/%d/%y.
%j Juliandayofyear(001-366).
%n Insert anewline.
%p Stringtoindicatea.m.or p.m.
%T Time intheformat%H:%M:%S.
%t Tabspace.
TheabovecommandwillprintWed Jul2310:52:34IST2008
1. Tousetab space:
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
date+"Dateis%D%t Timeis%T"
2. Toknowtheweeknumberoftheyear, date -V
Theabovecommandwillprint30
3. Tosetthe date,
date-s"10/08/200811:37:23"
TheabovecommandwillprintWedOct0811:37:23IST2008
whoCOMMAND:
who commandcanlist thenamesofuserscurrentlylogged in,theirterminal,thetimethey have
been logged in, and the name ofthe host fromwhich they have logged in.
SYNTAX:
TheSyntax is
who[options][file]
OPTIONS:
withinthelastminute.
-m Sameaswhoami.
-qPrintsonlytheusernamesandtheusercount/totalnoofuserslogged in. -T,- w Include user's
message status in the output.
EXAMPLE:
1.who–Uh
Output:
2.whoami
whoamicommandprintsthe username.
echo COMMAND:
echocommand printsthegiveninputstringtostandardoutput.
SYNTAX:
TheSyntax is
echo[options..] [string]
OPTIONS:
-n donot outputthetrailingnewline
-eenable interpretationofthebackslash-escapedcharacterslistedbelow -E disable
interpretation of those sequences in STRINGs
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
Without-E,thefollowingsequencesarerecognizedandinterpolated:
EXAMPLE:
echocommand
echo "hscriptsHioxIndia"
TheabovecommandwillprintashscriptsHioxIndia
1. Touse backspace:
echo-e"hscripts\bHiox\bIndia"
TheabovecommandwillremovespaceandprintashscriptsHioxIndia
2. Tousetabspaceinechocommand echo -e
"hscripts\tHiox\tIndia"
Theabovecommandwillprintashscripts Hiox India
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
passwdCOMMAND:
passwdcommandisusedtochange your password.
SYNTAX:
The Syntax is
passwd[options]
OPTIONS:
-a Showpasswordattributesforallentries.
-l Lockspasswordentryforname.
Deletespasswordforname. Theloginnamewillnotbepromptedfor
-d
password.
Forcetheuser to changepassword atthenextloginbyexpiring thepassword
-f
forname.
EXAMPLE:
1. passwd
CurrentPassword:
NewPassword:
ConfirmNew Password:
pwdCOMMAND:
pwd-Print WorkingDirectory.pwdcommandprintsthefullfilenameofthecurrent working directory.
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
SYNTAX:
The Syntax is
pwd[options]
OPTIONS:
-P Thepathnameprintedwillnotcontainsymboliclinks.
-L Thepathnameprintedmaycontainsymboliclinks.
EXAMPLE:
1. Displaysthecurrentworkingdirectory. pwd
Ifyouareworking inhomedirectorythen,pwdcommanddisplaysthecurrent working directory as /home.
calCOMMAND:
calcommandisusedtodisplaythe calendar.
SYNTAX:
TheSyntax is
cal[options][month][year]
OPTIONS:
-1 Displayssinglemonthasoutput.
-3 Displaysprev/current/nextmonthoutput.
-sDisplayssundayasthe first dayoftheweek. -m Displays Monday as
the first day of the week.
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
-j DisplaysJuliandates(daysone-based,numberedfromJanuary1).
-y Displaysacalendar forthecurrentyear.
EXAMPLE:
1. cal
Output:
September2008
Su Mo TuWeThFrSa
12 3 4 56
7 8 9101112 13
141516 17181920
212223 24252627
282930
calcommanddisplaysthecurrentmonthcalendar.
2.cal-352008
Output:
HerethecalcommanddisplaysthecalendarofApril,MayandJunemonthofyear2008.
loginCommand
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
Signsintoanewsystem.
Syntax
-p Usedtopassenvironmentvariablestotheloginshell.
-ddevice loginacceptsadeviceoption,device.deviceistakento bethepathnameofthe
terminal
theremotehost.
Examples
logincomputerhope.com-Wouldattempttologintothecomputerhope domain.
unamecommand
Printnameofcurrentsystem.
Syntax
-a Printbasicinformationcurrentlyavailablefromthesystem.
-i Print thenameofthehardwareimplementation(platform).
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
-n Printthenodename(thenodenameisthenamebywhichthesystemisknown to
acommunications network).
-p Printthecurrenthost'sISAorprocessor type.
-r Printtheoperatingsystemreleaselevel.
-v Printtheoperatingsystemversion.
systemname,node,release,version,machine,andnumberofCPUs.
BusType,Serial,and Users(setto"unknown"inSolaris)
OEM#andOrigin#(setto0and1,respectively)
-S Thenodenamemaybechangedbyspecifyingasystemnameargument.The
systemname systemnameargumentisrestrictedtoSYS_NMLNcharacters. SYS_NMLNis
animplementationspecific valuedefined in<sys/utsname.h>.Onlythesuper-
user is allowed
thiscapability.
Examples
uname-arv
uname-p
Displaythe Linuxplatform.
SED:
What issed?
Anon-interactivestream
editor
Interpretssedinstructionsandperforms
actions
Usesed
to:
Automaticallyperformeditsonfile(s)
Simplifydoingthesameeditsonmultiplefiles
Writeconversionprograms
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
SedCommandSyntax(Sed Scripts):
SedOperation
HowDoessedWork?
lineofinputiscopiedintoatemporarybuffercalledpatternspace
editingcommandsareapplied
sedreadslineofinput
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
(unless–noptionwas used)
lineisremovedfrompatternspace
sedreadsnextlineofinput,untilendof file
Note:
input file is unchanged
sedinstructionformat(SedAddresses):
addressdetermineswhichlinesinthe input file aretobeprocessedbythe
command(s)
ifnoaddressisspecified,thenthecommandisappliedtoeachinputline
address
types:
Single-Lineaddress
Set-of-Linesaddress
Rangeaddress
Nested address
Single-LineAddress
Specifiesonlyonelineintheinput
file
special:dollarsign($)denoteslastlineofinputfile
Examples:
showonlyline3
sed-n-e'3p'input-file
showonlylast line
J.B.INSTITUTEOFENGINEERING
ANDTECHNOLOGY
sed-n-e'$p'input-file
substitute“endif”with“fi”online10
Set-of-LinesAddress
useregularexpressiontomatch
lines
writtenbetweentwo slashes
processonlylinesthatmatch
maymatchseveral lines
linesmayormaynotbeconsecutives
Examples:
sed-e‘/key/s/more/other/’input-file sed -n -
e ‘/r..t/ p’ input-file
RangeAddress
Definesasetofconsecutivelines
Format:
start-addr,end-addr (inclusive)
Examples:
10,50 line-number,line-number
10,/R.E/ line-number,/RegExp/
/R.E./,10 /RegExp/,line-number
J.B.INSTITUTEOFENGINEERINGAND
TECHNOLOGY
LinuxProgrammingNotes
/R.E./,/R.E/ /RegExp/,/RegExp/
Example: Range Address
%sed-n -e‘/^BEGIN$/,/^END$/p’ input-file
PrintlinesbetweenBEGINandEND,
inclusive
BEGIN
Line1ofinput
Line2ofinput Line3
of input END
Line4ofinput
Line5ofinput
NestedAddress
Nestedaddresscontainedwithinanotheraddress
Example:
printblanklinesbetweenline20and30
20,30{
/^$/p
Addresswith!
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
address withanexclamationpoint(!):
instructionwillbeappliedtoalllinesthat do notmatchtheaddress Example:
printlinesthatdonotcontain“obsolete”
sed-e‘/obsolete/!p’input-file sed
commands
LineNumber
linenumbercommand(=)writesthecurrentlinenumberbefore eachmatched/output line
Examples:
sed-e'/Two-thirds-time/='tuition.data sed -e
'/^[0-9][0-9]/=' inventory
modifycommands
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
InsertCommand:i
addsoneormore lines directlyto theoutputbeforethe
address:
inserted“text”neverappearsinsed’spatternspace
cannotbeusedwitharangeaddress;canonlybeusedwiththesingle-lineand set-of-lines address types
Syntax:
[address]i\ text
AppendCommand:a
addsoneormorelinesdirectlyto theoutput afterthe
address:
Similartotheinsertcommand (i),appendcannotbeusedwitharange address.
Appended“text”doesnotappearinsed’spatternspace.
Syntax:
[address]a\ text
ChangeCommand:c
replacesanentirematchedlinewithnew
text
acceptsfouraddress
types:
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
single-line,set-of-line,range,andnestedaddresses.
Syntax:
[address1[,address2]]
c\ text
DeleteCommand:d
deletestheentire pattern
space
commands following the delete command are ignored since the deleted text isno longer in the
pattern space
Syntax:
[address1[,address2]
]d Substitute
Command (s) Syntax:
[addr1][,addr2]s/search/replace/[flags]
replacestextselectedbysearchstringwithreplacement
string
searchstringcanberegular
expression
flags:
global(g),i.e.replacealloccurrences
specificsubstitutioncount(integer), default1
RegularExpressions:usewithsed
J.B.INSTITUTEOFENGINEERING
ANDTECHNOLOGY
SubstitutionBack References
Example:ReplacementString&
$catdatafile
J.B.INSTITUTEOFENGINEERINGAND
TECHNOLOGY
PatriciaHemenway 4.0 .7 4 17
$sed-e‘s/[0-9][0-9]$/&.5/’datafile
TransformCommand(y)
Syntax:
[addr1][,addr2]y/a/b/
translatesonecharacter'a'toanother'b'
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
cannotuseregularexpression metacharacters
cannotindicatearangeofcharacters
similarto“tr”command
Example:
Input (next)Command:nandN
Forcessedtoreadthenextinput
line
Copiesthecontentsofthepatternspacetooutput
Deletesthecurrentlineinthepattern space
Refillsitwiththenextinputline
Continueprocessing
N(uppercase)
Command
addsthenextinputlinetothecurrentcontentsofthepatternspace
useful whenapplyingpatternstotwoormorelinesatthesametime
OutputCommand:pandP
PrintCommand
(p)
copiestheentirecontentsofthepatternspacetooutput
willprintsamelinetwiceunlesstheoption“–n”isused
Printcommand:
P
printsonlythefirstlineofthepatternspace
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
printsthecontentsofthepatternspaceuptoandincluding anewline character
anytext followingthefirst newlineisnot printed
ListCommand(l)
Thelistcommand:
l
showsspecialcharacters(e.g.tab,etc)
Theoctaldumpcommand(od-c) canbeusedtoproducesimilar
result
HoldSpace
temporarystorage
area
h,H
g,G
Filecommands
allowstoreadandwritefrom/tofilewhileprocessingstandard
input
read:r
command
write:w
command
ReadFilecommand
Syntax:rfilename
queue the contents of filename to be read and inserted into the output streamat the end of the current
cycle, or when the next input line is read
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
iffilenamecannotberead,it istreatedasifit were anemptyfile,without any error indication
singleaddressonly
WriteFilecommand Syntax:w
filename
Writethe patternspacetofilename
The filenamewillbe created(ortruncated)beforethefirstinputlineis read
allwcommandswhichrefertothesamefilenameareoutputthroughthesame FILE stream
BranchCommand(b)
Changetheregularflowofthecommandsinthescriptfile
Syntax:
[addr1][,addr2]b[label]
Branch(unconditionally)to‘label’or endofscript
If“label”issupplied,executionresumesatthelinefollowing:label;otherwise, control passes to the end of the script
Branch
label
:mylabel
Example:Thequit(q)Command Syntax:
[addr]q
Quit(exitsed)whenaddrisencountered.
Example:Displaythefirst50linesandquit
%sed-e’50q’datafile
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNote
Sameas:
%sed-n-e‘1,50p’ datafile
%head-50datafile
Awk
What isawk?
createdby:Aho,Weinberger,and
Kernighan
scriptinglanguage usedfor manipulatingdataandgenerating
reports
versionsof
awk
awk,nawk,mawk,pgawk,…
GNUawk: gawk
What canyoudowithawk?
awk
operation:
scansafilelinebyline
splitseachinputlineinto fields
comparesinputline/fieldsto pattern
performsaction(s)on matched lines
Useful
for:
transform datafiles
produceformattedreports
Programming
constructs:
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
formatoutputlines
arithmeticandstring operations
conditionals and loops
TheCommand:awk
Basicawk Syntax
awk[options]‘script’
file(s)
awk[options]–fscriptfile
file(s)
Options:
-F tochangeinputfieldseparator
-f tonamescript file
Basicawk Program
consistsofpatterns&actions:
pattern{action}
ifpatternismissing,actionisappliedtoalllines
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
ifactionismissing,thematchedline isprinted
must haveeitherpatternor action
Example:
awk'/for/'testfile
printsalllinescontainingstring“for”in testfile
BasicTerminology:input file
Afieldisaunitofdatainaline
Eachfieldisseparatedfromtheotherfieldsbythe
fieldseparator
defaultfieldseparatoriswhitespace
A
recordisthecollection offieldsinaline
Adatafileismadeupof
records
ExampleInputFile
Buffers
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
awk supportstwotypesofbuffers:recordandfield
field
buffer:
oneforeach fieldsinthecurrent record.
names:$1,$2, …
recordbuffer
:
$0holdstheentirerecord
Some SystemVariables
FS Fieldseparator (default=whitespace)
RS Recordseparator (default=\n)
NF Numberoffields incurrentrecord
NR Number ofthecurrentrecord
OFS Outputfieldseparator(default=space)
ORS Outputrecordseparator(default=\n)
FILENAME Current filename
Example:Recordsand Fields
J.B.INSTITUTEOFENGINEERING
ANDTECHNOLOGY
%catemps
%awk'{printNR,$0}'emps
Example:SpaceasField Separator
%catemps
TomJones 44245/12/66543354
3 SallyChang 650000
4 BillyBlack336500
Example:ColonasFieldSeparator
%catem2
TomJones:4424:5/12/66:543354
MaryAdams:5346:11/4/63:2876
5
SallyChang:1654:7/22/54:65000
0 Billy
Black:1683:9/23/44:336500
%awk-F:'/Jones/{print$1,$2}'
em2 Tom Jones 4424
awkScripts
awkscriptsaredividedintothreemajor
parts:
J.B.INSTITUTEOFENGINEERING
ANDTECHNOLOGY
commentlinesstartwith
#
BEGIN:pre-processing
performs processing that must be completed before the file
processing starts (i.e., before awk starts reading records
from the input file)
useful for initialization tasks such as to initialize variables
and to create report headings
BODY:
Processing
contains main processing logic to be applied to input records
likea loopthatprocesses inputdataone recordata time:
if a file contains 100 records, the bodywill be executed
100times, one for each record
END:post-
processing
contains logic to be executed after all input data have been
processed
logic such as printing report grand totalshould be
performed in this part of the script
Pattern/Action Syntax
J.B.INSTITUTEOFENGINEERING
ANDTECHNOLOGY
CategoriesofPatterns
ExpressionPattern types
match
entireinputrecord
regularexpressionenclosed by‘/’s
explicitpattern-matchingexpressions
~(match),!~(notmatch)
expressionoperators
arithmetic
relational
logical
Example: matchinputrecord
%catemployees2
TomJones:4424:5/12/66:543354
J.B.INSTITUTEOFENGINEERING
ANDTECHNOLOGY
Mary Adams:5346:11/4/63:28765
J.B.INSTITUTEOFENGINEERINGAND
TECHNOLOGY
SallyChang:1654:7/22/54:650000 Billy
Black:1683:9/23/44:336500
% awk –F: '/00$/' employees2
SallyChang:1654:7/22/54:650000 Billy
Black:1683:9/23/44:336500 Example:
explicit match
%catdatafile
%awk'$5~/\.[7-9]+/'datafile
Examples: matchingwithREs
+ Add x+y
- Subtract x–y
* Multiply x*y
J.B.INSTITUTEOFENGINEERINGAND
TECHNOLOGY
/ Divide x/y
% Modulus x%y
^ Exponential x^y
Example:
%awk'$3*$4>500{print$0}'file
RelationalOperators
== Equalto x==y
!= Notequalto x!=y
LogicalOperators
|| LogicalOR a||b
J.B.INSTITUTEOFENGINEERINGAND
TECHNOLOGY
! NOT !a
Examples:
%awk'($2>5)&&($2<=15) {print$0}'file
%awk'$3==100||$4>50'file
RangePatterns
Matchesrangesofconsecutiveinputlines
Syntax:
pattern1,pattern2 {action}
patterncanbeanysimple
pattern
pattern1
turnsactionon
pattern2
turnsactionoff
RangePatternExample
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
awkActions
awk expressions
Expressionisevaluatedandreturns
value
consistsofanycombinationofnumericandstring constants,variables,operators, functions, and regular expressions
Caninvolve
variables
Aspartofexpressionevaluation
Astargetof assignment
awk variables
Ausercandefine anynumberofvariableswithinanawk
script
Thevariablescanbenumbers,strings,or
arrays
Variablenamesstart withaletter,followedbyletters,digits,and
underscore
Variables comeintoexistence thefirst time they are referenced; therefore, they donot need to be
declared before use
Allvariablesareinitiallycreatedasstringsandinitializedtoanullstring
“”
J.B.INSTITUTEOFENGINEERINGAND
TECHNOLOGY
LinuxProgrammingNotes
awkVaria
bles
Format:
variable=expression
Examples:
%awk'$1~/Tom/
{wage=$3*$4;printwage}' filename
awkassignmentoperators
= assignresultofright-hand-sideexpressionto left-hand-side
variable
++ Add 1 to variable
-- Subtract 1fromvariable
+= Assignresultofaddition
-= Assignresultofsubtraction
*= Assignresultofmultiplication
/= Assignresultofdivision
%= Assignresultofmodulo
^= Assignresultofexponentiation
Awk example
J.B.INSTITUTEOFENGINEERING
ANDTECHNOLOGY
File:grades
john85927894 88
andrea8990759086
jasper84 88 80 92 84
awkscript:
average
#averagefive grades
{total=$2+$3+$4+$5+
OutputStatements
printeasyandsimple output
printf
printformatted(similar toCprintf)
sprintf
formatstring(similartoC sprintf)
Function: print
J.B.INSTITUTEOFENGINEERING
ANDTECHNOLOGY
Writestostandardoutput
Outputisterminatedby
ORS
defaultORSis newline
Ifcalledwithnoparameter,itwillprint
$0
Printedparametersareseparatedby
OFS,
default OFSisblank
Printcontrolcharactersare
allowed:
\n\f \a\t\\…
printexample
%awk'{print}'grades john 85
92 78 94 88
andrea8990759086
%awk '{print($0)}'
gradesjohn8592789488
andrea8990 759086
Redirectingprint output
Print outputgoestostandardoutput
unlessredirectedvia:
>“file”
J.B.INSTITUTEOFENGINEERINGAND
TECHNOLOGY
LinuxProgrammingNotes
>> “file”
|“command”
willopen fileor command only
once
subsequentredirectionsappendtoalreadyopen
stream
print Example
%awk'{print$1,$2 >"file"}'grades
%catfile
john85
andrea89
jasper84
john85
%awk'{print$1,$2|"sort–k2"}'grades jasper 84
john85
andrea89
%date
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
2008%date|
awk'{print"Month:"$2 "\nYear:",
$6}'Mo0nth:Nov Year:
2008
printf:Formattingoutput
Syntax:
printf(format-string,var1,var2,…)
workslikeC printf
each formatspecifierin“format-string”requiresargumentofmatchingtype
Formatspecifiers
%c singlecharacter
%s string ofcharacters
%f floatingpointnumber
%o octalnumber
%x hexadecimalnumber
%e scientificfloatingpointnotation
J.B.INSTITUTEOFENGINEERINGAND
TECHNOLOGY
%10s
%7d
%10.4f
%-20s
meaning:
widthoffield,fieldisprintedrightjustified
precision:numberofdigitsafterdecimalpoint
“-”willleftjustify
sprintf:Formattingtext Syntax:
sprintf(format-string,var1,var2,…)
Workslikeprintf,butdoesnotproduce output
Insteaditreturnsformattedstring
Example:
print text
awkArray
awk
allows one-dimensional arrays to
store strings or numbers
indexcanbenumberor
string
arrayneednotbe
declared
itssize
itselements
arrayelementsarecreatedwhenfirst
used
initializedto0or“”
Arraysinawk Syntax:
arrayName[index]= value
Examples:
Illustration:AssociativeArrays
awkarrays canusestringas
index
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
Awkbuiltinsplitfunctions
split(string, array, fieldsep)
dividesstringintopiecesseparatedbyfieldsep,andstoresthepiecesinarray
ifthefieldsepisomitted, thevalueofFSisused.
Example:
split("auto-da-fe",a,"-")
setsthecontentsofthearrayaasfollows:
a[1] = "auto"
a[2]= "da"
a[3]= "fe"
Example:processsales data
input
file:
J.B.INSTITUTEOFENGINEERING
ANDTECHNOLOGY
output:
summaryofcategorysales
Illustration:processeachinputlin
e
J.B.INSTITUTEOFENGINEERING
ANDTECHNOLOGY
Illustration:processeachinputline
Summary:awkprogram
Example:completeprogram
%catsales.awk
deptSales[$2]+=$3
END{
for(xindeptSales)
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
printx,deptSales[x]
%awk–fsales.awksales awk
builtin functions tolower(string)
returnsacopyofstring,witheachupper-casecharacterconvertedtolower- case. Nonalphabetic
characters are left unchanged.
Example:tolower("MiXeDcAsE
123")returns"mixedcase123"
toupper(string)
103:sway bar:49.99
101:propeller:104.99 104:fishing
line:0.99
113:premiumfishbait:1.00
106:cup holder:2.49
107:cooler:14.89
112:boatcover:120.00
J.B.INSTITUTEOFENGINEERINGAND
TECHNOLOGY
109:transom:199.00
110:pulley:9.88
105:mirror:4.99
108:wheel:49.99
111:lock:31.00
102:trailerhitch:97.95 awk
Example: output Marine
Parts R Us Main catalog
Part-idname price
======================================
======================================
print"======================================"
printf("%3d\t%-20s\t%6.2f\n",$1,$2,$3) count++
}
J.B.INSTITUTEOFENGINEERINGAND
TECHNOLOGY
END{
print"======================================"
print"Cataloghas"count" parts"
Applications:
Awkcontrolstructures
Conditional
if-else
Repetition
for
withcounter
witharrayindex
while
do-while
also:break, continue
ifStatement
Syntax:
if(conditionalexpression)
statement-1
else
statement-2
Example:
J.B.INSTITUTEOFENGINEERING
ANDTECHNOLOGY
if( NR<3 )
print$2
else
print$3 for
Loop Syntax:
for(initialization;limit-test; update)
statement
Example:
for(i=1;i<=NR;i++)
total+=$i count++
}
forLoopforarrays Syntax:
for(varinarray) statement
J.B.INSTITUTEOFENGINEERING
ANDTECHNOLOGY
Example:
for(xindeptSales)
printx,deptSales[x]
WhileLoop
Syntax:
while(logicalexpression) statement
Example:
i=1
while(i <=NF)
printi,$i i++
}
do-whileLoop
Syntax:
do
statement
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
while(condition)
statementisexecutedatleastonce, evenifconditionisfalseatthebeginning
Example:
i=1 do{
print$0 i++
}while(i <=10)
loopcontrolstatements
break
exitsloop
continue
skipsrestofcurrentiteration,continueswithnextiteration
ShellProgramming
The shell has similarities to the DOS command processor Command.com (actually Dos was
designasapoorcopyofUNIXshell), it'sactuallymuchmorepowerful,reallyaprogramming language in its own
right.
A shell is always available on even the most basic UNIX installation. You have to go through theshellto
getotherprogramstorun. Youcanwriteprogramsusingtheshell. Youusetheshell to administrate your UNIX
system. For example:
ls-al|more
isashortshellprogramtoget along listingofthepresent directoryandroutetheoutput through the more
command.
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
Whatis aShell?
Herearesomecommonshells.
Introduction- WorkingwithBourneShell
• The Bourne shell, or sh, was the default Unix shell of Unix Version 7. It wasdeveloped by Stephen
Bourne, of AT&T Bell Laboratories.
• A Unix shell, also called "the command line", provides the traditional user interfacefor the Unix
operating systemand for Unix-like systems. Users direct the operation of the computer by entering
command input as text for a shell to execute.
• Therearemanydifferentshells inuse.Theyare
– Bourneshell(sh)
– Cshell(csh)
– Kornshell(ksh)
Bourne Again shell (bash)
111
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
• When we issue a command the shell is the first agency to acquire the information.
Itacceptsandinterpretsuserrequests.Theshellexamines&rebuildsthecommands &leaves the
execution work to kernel. The kernel handles the h/w on behalf of these commands &all processes
in the system.
• Theshellisgenerallysleeping.Itwakesupwhenaninputiskeyedinattheprompt. This input is actually input to
the program that represents the shell.
•
Shellresponsibilities
1. ProgramExecution
2. VariableandFilenameSubstitution
3. I/ORedirection
4. PipelineHookup
5. EnvironmentControl
6. Interpreted Programming
Language 1.Program Execution:
• Theshellisresponsiblefortheexecutionofallprogramsthatyourequestfrom your terminal.
• Eachtimeyoutypeinalinetotheshell,theshellanalyzesthelineandthen determines what to do.
• The line that is typed to the shell is known more formally as the command line. The
shellscansthiscommand lineanddeterminesthenameoftheprogramto beexecuted and what arguments
to pass to the program.
2. VariableandFilenameSubstitution:
• Likeanyotherprogramminglanguage,theshellletsyouassignvaluestovariables. Whenever you
specifyone of these variables on the command line, preceded by a dollar sign, the shell substitutes
the value assigned to the variable at that point.
3. I/O Redirection:
• It is the shell's responsibility to take care of input and output redirection on thecommand line. It scans the
command line for the occurrence of the special redirection characters <, >, or >>.
4. PipelineHookup:
• Just astheshellscansthecommandlinelookingforredirectioncharacters,it alsolooks for the pipe character
|. For each such character that it finds, it connects the standard output fromthecommand preceding
the| tothestandard input oftheone following the
|.Ittheninitiatesexecutionofbothprograms.
5. EnvironmentControl:
• The shell provides certain commands that let you customize your environment. Your
environmentincludes homedirectory,thecharacters thattheshelldisplaystopromptyou
112
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
6. InterpretedProgrammingLanguage:
• The shell has its own built-in programming language. This language is interpreted, meaning that the shell
analyzes each statement in the language one line at a time and
thenexecutesit.Thisdiffersfromprogramming languagessuchasCandFORTRAN, in which the programming
statements are typically compiled into a machine-executable form before they are executed.
• Programsdeveloped in interpreted programming languagesare typicallyeasier to debug and modify than
compiled ones. However, they usually take much longer to executethan their compiled equivalents.
PipesandRedirection
Pipesconnectprocessestogether.TheinputandoutputofUNIXprogramscanbe redirected.
Redirecting Output
The>operatorisusedtoredirectoutputofaprogram.For example: ls -l > lsoutput.txt
redirectstheoutputofthelistcommandfromthescreentothefilelsoutput.txt.
To0appendtoafile,usethe>> operator.
ps>>lsoutput.txt
Redirecting Input
Youredirect inputbyusingthe<operator.Forexample: more < killout.txt
Pipes
Wecanconnectprocessestogetherusingthepipe operator( |).Forexample,the following
program means run the ps program, sort its output, and save it in the file pssort.out
ps|sort>pssort.out
Thesort commandwillsortthe listofwordsinatextfile into alphbeticalorderaccordingtothe ASCII code
set character order.
HereDocuments
113
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
How ItWorks
Itexecutestheheredocumentasifitwereinputcommands.
Running aShellScript
You cantype inasequenceofcommandsand allowtheshellto executetheminteractively, or youu can sotre
these commands in a file which you can invoke as a program.
InteractivePrograms
A quick wayof trying out small code fragments is to just type in the shell script onthe command
line.Here isashellprogramto compileonlyfilesthat containthestringPOSIX.
TheShellasaProgrammingLanguage Creating a
Script
Tocreateashellscript first useatexteditorto createafilecontainingthecommands.For example, type the
following commands and save them as first.sh
114
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
Note:commandsstartwitha#.
Theline
#!/bin/sh
isspecialandtellsthesystemtousethe/bin/shprogramtoexecutethisprogram.
Thecommand
exit0
Causesthescriptprogramtoexitandreturnavalueof0,whichmeanstherewerenot errors.
MakingaScriptExecutable
Therearetwowaysto executethescript.1) invoketheshellwiththenameofthescript fileas a parameter,
thus:
/bin/sh first.sh
Or2)changethe modeofthescript toexecutableandthenafterexecuteit byjust typing its name.
chmod +x first.shfirst.sh
Actually,youmayneedtotype:
./first.sh
tomakethefileexecuteunlesthepathvariablehasyour directoryinit.
ShellSyntax
The modernUNIXshellcanbeusedtowritequitelarge,structuredprograms.
Shellmetacharacters
115
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
Typesofmetacharacters:
1. Filesubstitution
2. I/Oredirection
3. Processexecution
4. Quotingmetacharacter
s 5.Positional parameters
6. Specialcharacters
7. Commandsubstitution
Filenamesubstitution:
Thesemetacharactersareusedtomatchthefilenamesinadirectory.
Metacharacter significance
* matchesanyno.ofcharacters
? matchesasinglecharacter
[ijk] matchesasinglecharactereitheri,j,k
[!ijk] matchesasinglecharacterthatisnotanI,j,k
ShellVariables
116
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
Ucandefine&usevariablesbothinthecommandlineandshellscripts.These variables are called shell variables.
Notypedeclarationisnecessarybeforeucanuseashellvariable.
Variablesprovidetheabilitytostoreandmanipulatetheinformationwithinthe shell program. The variables are
completely under the control of user.
VariablesinUnixare oftwotypes.
1) User-definedvariables:
Generalizedform:
variable=value. Eg: $x=10
$echo$x
10
To remove a variable
use unset. $unset x
Allshellvariablesareinitializedtonullstringsbydefault.Toexplicitlysetnullvaluesuse
x= orx=‘’ or x=“”
Toassignmultiwordstringstoavariableuse
$msg=‘uhaveamail’
117
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
2) EnvironmentVariables
They areinitialized when the shell script starts and normally capitalized todistinguish them from
user-defined variables in scripts
Todisplayallvariablesinthelocalshellandtheir values,typethesetcommand
Theunsetcommandremovesthevariablefromthe currentshellandsubshell
EnvironmentVariables Description
$PS1 Commandprompt
$PS2 Secondaryprompt
$SHELL Currentloginshell
$0 Nameoftheshellscript
$# No.ofparameterspassed
$$ ProcessIDoftheshellscript
CommandsubstitutionandShellcommands:
read:
Thereadstatementisatoolfortakinginputfromtheuseri.e.making
scriptsinteractive.Itisusedwithoneormorevariables.Inputsupplied
throughthestandardinputisreadintothesevariables.
$readname
Whateveruenteredisstoredinthevariable name.
printf:
Printfisused to print formatted
o/p.printf"format"arg1arg2... Eg:
$printf"Thisisanumber: %d\n"10
118
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
This isanumber:10
Printfsupportsconversionspecificationcharacterslike%d,%s,%x
,%o….Exitstatusofa command:
Everycommandreturnsa valueafter execution.Thisvalueiscalledtheexitstatus or
return value of a command.
This value is said to be true if the command executes successfully and false if it fails.
There is special parameter used by the shell it is the $?. It stores the exit status
ofacommand.
exit:
U don’t need to place exit at the end of every shell script becausetheshell knows when script
execution is complete.
set:
Setisusedtoproducethelistofcurrentlydefinedvariables.
$set
Setisusedtoassignvaluestothepositionalparameters.
$setwelcometoUnix
Thedo-nothing(:)Command It
is a null command.
Insomeolder shellscripts,colonwasusedat thestartofalinetointroducea
comment,butmodernscripts uses#now.
expr:
Theexprcommandevaluatesitsargumentsasanexpression:
$expr8+6
14
$x=`expr12 /4`
$echo $x
3
export:
119
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
There isawaytomakethe value ofa variable knowntoasubshell, andthat's
byexporting it with the export command. The format of this command is
exportvariables
wherevariablesisthelistofvariablenamesthatyouwantexported.Foranysub
shellsthatgetexecutedfromthatpointon,thevalueoftheexportedvariableswill
bepasseddowntothe subshell.
eval:
isevalcommand-line Eg:
$catlast
evalecho\$$#
$lastonetwothreefour four
${n}
${10}
Shiftcommand:
Shift
whatever was previously stored inside $2 will be assigned to $1, whatever was previously
storedin$3willbeassignedto $2, andsoon. Theoldvalueof$1willbe irretrievablylost.
TheEnvironment-EnvironmentVariables
Itcreatesthevariablesalutation,displaysitsvalue,andsomeparameter variables.
120
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
ParameterVariables
• Ifyourscriptis invokedwithparameters, someadditionalvariablesarecreated.
Quoting
makesuretomakeitexecutablebytypingthecommand:
121
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
thefileis:
How ItWorks
Thetest,or[]Command
Therearearithmeticcomparison.
122
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
Therearefileconditions.
ControlStructures
The shellhasasetofcontrolstructures.
if
tatementisvarysimilarotherprogramminglanguagesexcept itendswithafi. if condition
then
statements
else fi statements
elif
123
J.B.INSTITUTE OF
ENGINEERINGAND
TECHNOLOGY
LinuxProgrammingNotes
#!/bin/sh
124
J.B.INSTITUTEOFENGINEERING
ANDTECHNOLOGY
LinuxProgrammingNotes
else
125
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
o"Sorry,$timeofdaynotrecognized.Enteryesorno"exit 1 fi
exit0
How ItWorks
Theabovedoesasecondtestonthevariabletimeofdayifitisn'tequaltoyes.
AProblemwithVariables
Ifavariableissettonull, the statement
if[$timeofday="yes"]
lookslike if[="yes"]
sillegal.Thisproblemcanbe fixed byusingdoublequotesaroundthevariablename. if [ "$timeofday" = "yes" ]
.
for
The forconstruct isused forloopingthrougharangeofvalues, whichcanbe anysetof strings. The syntax is:
forvariableinvalues do
statements
done
Tryoutthefollowingscript:
#!/bin/sh
forfooinbarfud43 do
echo$foo
done exit0
Whenexecuted,the outputshould
be:bar fud0 43
How ItWorks
126
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
How ItWorks
while
While loops will loop as long as some condition exist. OF course something in the body
statementsoftheloopshouldeventuallychange theconditionandcausethe loopto exit. Here is the while
loop syntax.
whileconditiondo
statements
done
Hereisawhilloopthat loops20
times.#!/bin/sh foo=1
while[ "$foo"-le20
]do
echo"Herewegoagain"foo=
done exit0 $(($foo+1))
How ItWorks
ovescript usesthe[]commandtotestfoofor<=thevalue20.Theline foo=$(($fo0o+1))
incrementsthevalueoffooeachtimetheloop executes..
until
Theuntilstatementloopsuntilaconditionbecomestrue!Itssyntax is:
127
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
until conditiondo
statements
done
Hereisascriptusinguntil.
#!/bin/sh
untilwho|grep"$1">/dev/null do
Sl0eep 60
done
#nowringthebellandannouncetheexpecteduser. echo -e \\a
echo"****$1hasjustlooggedin****"
exit0
case
Thecasestatement allowsthetestingofavariable for morethenonevalue.Thecase statement
ends with the word esac. Its syntax is:
casevariablein
pattern[|pattern]...)statements;; pattern[|pattern]...) statements;;
...
esac
Here isasamplescriptusingacasestatement:
#!/bin/sh
case"$timeofday"in
"yes") echo "Good Morning";; "no")echo"GoodAfternoon";;
0"y" ) echo "Good Morning";; "n" ) echo "Good Afternoon";;
*)echo "Soory,answernot recognized";;
esac exit0
128
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
How ItWorks
Theabovehassever stringstestedfor eachpossiblestatement.
acasestatementthatexecutesmultiplestatementsforeachcase. case "$timeofday" in
"yes" | "y" | "Yes" | "YES" ) echo"GoodMorning"
echo"Upbrightandearlythis morning"
;;
[nN]*) echo"Good Afternoon"
;;
*)
echo "Sorry, answer not
recognized"echo"Pleaseanswer yes
or noo" exit 1
;;
esac
How ItWorks
Arithmeticinshell
The$((...))isabetteralternativetotheexprcommand,whichallowssimplearithmetic commands to be
processed.
x=$(($x+1))
ParameterExpansion
129
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
}aroundavariabletoprotectitagainstexpansion. #!/bin/sh
foriin12 do
my_secret_process${i}_tmp
done
Herearesomeoftheparameterexpansion
How ItWorks
Thetryit outexerciseusesparameter expansiontodemonstratehowparameterexpansionworks.
ShellScriptExamples Example
#!/bin/sh
echo"Good afternoon"
fi
130
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
exit0
elif-DoingfurtherChecks
#!/bin/sh
echo"Isitmorning?Pleaseansweryesorno" read
timeofday
if[$timeofday="yes"];then echo"Good
Morning"
elif[$timeofday="no"];then echo"Good
afternoon"
else echo"Wronganswer!Enteryesorno" exit 1
fi exit0
InterruptProcessing-trap
pcommand isused forsecifyingtheactionstotakeonreceipt ofsignals.It syntaxis: trap command signal
Hereare some ofthe signals.
How ItWorks
Thetryit outsectionhas youtype inashellscript totestthetrapcommand. Itcreatesafile and keeps saying
that it exists until youu cause a control-C interrupt. It does it allagain.
131
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
Functions
ndefinefunctionsintheshell.Thesyntaxis: function_name () {
statements
}0
Hereisasamplefunctionanditsexecution.
#!/bin/sh
foo() {
echo"Functionfoo isexecuting"
}
anothersamplescriptwithafunctioninit.Saveitasmy_name #!/bin/sh
yes_or_no(){
echo"Parameters are
$*"whiletrue do
echo-n"Enteryesor no" read x
0case"$x"in
y|yes)return0;; n|no)return1;;
*)echo"Answeryesorno"
esac
done
}
echo"Originalparametersare$*"
132
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
ifyes_or_no"ISyournaem$1"then
else fi echo"Hi$1"
0exit0 echo"Nevermind"
How ItWorks
Whenmy_nameisexecutewiththestatement:
my_nameRickandNeil
.givestheoutputof:
OriginalparametersareRickandNeil Parameters are Is your
name Rick Enter yes or no
no
Never mind
Commands
break
The:Command
Thecoloncommandisanullcommand.Itcanbeusedforanaliasfortrue..
Continue
TheCommand
Thedotcommandexecutesthecommandinthecurrentshell:
.shell_script
.
133
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
echo
Eval
Theevalcommand evaluatesargumentsand givesthe results.
exec
exitn
Theexit commandcausesthescriptto exit withexit coden.Anexit codeof0 meanssuccess. Here are some
other codes.
export
Theexportcommandmakesthevariablenamedas itsparameteravailableinsubshells.
expr
Theexprcommandevaluatesitsargumentsasanexpression.
0x= `expr$x+ 1`
Herearesomeofitsexpressionevaluations
134
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
printf
Theprintfcommand isonlyavailable inmorerecent shells.Itworkssimilartothe echo command. Its
general form is:
printf"formatstring"parameter1parameter2...
Herearesomecharactersandformatspecifiers.
return
Thereturncommandcauses functionstoreturn. Itcanhaveavalueparameterwhichitreturns.
set
135
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
Thesetcommandsetstheparametervariables fortheshell.
shift
unset
Theunsetcommandremovesvariablesorfunctionsfromtheenvironment.
CommandExecution
Theresult of$(command) issimplytheoutputstring fromthecommand,whichisthen available to the
script.
DebuggingShellScripts
136
J.B.INSTITUTEOFENGINEERINGAND
TECHNOLOGY
LinuxProgrammingNotes
UNIT-II:
FilesandDirectoriesFileConcept
Filetypes
FileSystemStructure File
metadata
Inodes
kernelsupportfor files
ystem calls for file I/O operations
open,create,read,write,close,
lseek,dup2filestatusinformation-statfamily
fileandrecordlocking-fcntlfunction file
permission- chmod, fchmod
e ownership- chown, lchown,fchown links-
softlinks&hardlinks-
Symlink,Link,ulink 2.11Directories creating-
mkdir, removing - rmdir
changing directories -chdir
obtainingcurrentworkingdirectory-getcwd
scanningdirectories-opendir,readdir,closedir,rewinddirfunctions
.
137
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
UnitII
FilesandDirectories
WorkingwithFiles
Inthischapter welearnhowtocreate,open,read,write,andclosefiles.
UNIXFile Structure
InUNIX, everythingis afile.
Directories,too,arespecialsortsof files.
File types
MostfilesonaUNIXsystemareregular filesordirectories, butthereareadditionaltypesof files:
1. Regular files:The most commontypeoffile,whichcontainsdataofsome form.There is no distinction to the
UNIX kernel whether this data is text or binary.
2. Directory file: A file contains the names of other files and pointers to information on these
files.Anyprocessthat hasreadpermissionforadirectoryfilecanreadthecontents of the directory, but only the
kernel can write to a directory file.
3. Characterspecialfile:Atype offile usedforcertaintypesofdevicesona system.
4. Block special file: Atype of file typicallyused for disk devices. All devices on a system are either character
special files or block special files.
5. FIFO:Atypeoffileusedforinterprocesscommunicationbetweenprocesses.It’ssometimes called a named pipe.
6. Socket: Atype offile used for network communicationbetweenprocesses. Asocket can also be used for
nonnetwork communication between processes on a single host.
7. Symbolic link:Atypeoffilethatpointstoanother file.
138
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
Macro Typeoffile
S_ISDIR() Directoryfile
S_ISCHR() Characterspecialfile
S_ISBLK() Blockspecialfile
S_ISFIFO() PipeorFIFO
S_ISLNK() Symboliclink
S_ISSOCK() Socket
FileSystemStructure
Filesarearrangedindirectories, whichalsocontainsubdirectories.
Filesand Devices
139
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
$mount-tiso9660/dev/hdc
/mnt/cd_rom$cd/mnt/cd_rom
/dev/console-thisdevicerepresentsthesystemconsole.
/dev/tty-Thisspecialfile isanalias(logicaldevice)forcontrollingterminal(keyboard and screen, or
window) of a process.
/dev/null-Thisisthenulldevice.Alloutputwrittentothisdeviceis discarded.
FileMetadata Inodes
• Astructurethatismaintainedina separateareaoftheharddisk.
• Fileattributesarestored intheinode.
• Everyfileisassociatedwithatablecalledtheinode.
• Theinodeisaccessedbytheinode number.
• Inodecontainsthefollowingattributesofa file: file type, file
permissions , no. of links
UID of the owner, GID of the group owner, file size dateandtimeoflast
modification,lastaccess,change.
File attributes
Attribute valuemeaning
Filetype typeofthe file
Accesspermission fileaccesspermissionforowner,groupandothers
Hardlinkcount no.ofhardlinksofa file.
UID fileowneruserID.
GID thefile groupID.
Filesize filesizeinbytes.
Inodenumber systeminodenumber ofthefile.
FilesystemIDfilesystemIDwherethefileis stored.
KernelSupport ForFiles:
140
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
UNIX supports the sharing of open files between different processes. Kernel has three data structures are
used and the relationship among them determines the effect one process has on another with regard to file
sharing.
1. Everyprocess has an entry in the process table. Within each process table entry is a table
ofopenfiledescriptors,whichistakenasavector,withoneentryperdescriptor.
Associatedwitheach filedescriptorare
a. Thefiledescriptor flags.
b. Apointer toafiletableentry.
2. Thekernelmaintainsafiletablefor allopenfiles.Eachfiletableentrycontains
a. Thefilestatusflagsforthefile(read,write,append,sync,nonblocking,etc.),
b. Thecurrentfileoffset,
c. Apointertothe v-node table entryforthe file.
3. Each open file (or device) has a v-node structure. The v-node contains information about the type of file
and pointers to functions that operate onthe file. For mostfiles the v-nodealso containsthe i-nodeforthe
file. This informationisread fromdiskwhenthe file is opened, so that allthe pertinent information about the
file is readilyavailable.
The arrangement of these three tables for a single process that has two differentfiles open one file is open
on standard input(file descriptor 0) and the other is open standard output(file descriptor 1).
Here, the first process has the file open descriptor 3 and the second process has file open descriptor 4. Each
process that opens the file gets its own file table entry, but only a single v- node table entry. One reason
each process gets itsownfile table entry is so that each process has its own current offset for the file.
After each‘write’ is complete, the current file offset inthe file table entryis incremented by the number of
bytes written. If this causes the current file offset to exceed the current file size, the current file size, in the
i-node table the entry is to the current file offset(Ex: file is extended).
Ifa file is opened withO_APPEND flag, acorresponding flag is set in the filestatus flags of the file table entry.
Each time a ‘write’ is performed for a file with this append flag
141
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
It is possible for more than a descriptor entry to point to the same file table only. The file descriptor
flag is linked with a single descriptor in a single process, while file status flags are descriptors in any
process that point to given file table entry.
SystemCallsandDeviceDrivers
Systemcallsareprovided byUNIXtoaccessandcontrolfilesanddevices. A
number of device drivers are part of the kernel.
Thesystemcallstoaccessthe devicedriversinclude:
LibraryFunctions
142
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
Low-levelFile Access
Withthisknowledge,let'swriteourfirstprogram,simple_write.c:
143
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
Hereishowto runtheprogramanditsoutput.
$ simple_write Hereissomedata
$
read
Ifyouruntheprogram,youshouldsee:
144
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
open
Tocreateanewfiledescriptor weneedtousetheopen systemcall.
openestablishesanaccesspathto afileordevice.
145
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
InitialPermissions
Whenwe create a file using the O_CREAT flag withopen, we must use the three parameter form. mode,
thethirdparameter, is made formabitwiseORofthe flagsdefined inthe header file sys/stat.h. These are:
Forexample
umask
Thevalueisathree-digitoctalvalue.EachdigitistheresultsofANDingvaluesfrom1,2,or4.
146
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
close
147
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
Weuseclosetoterminatetheassociationbetweenafiledescriptor,fildes, anditsfile.
ioctl
148
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
Nowtrytheprogram,firstremovingtheoldoutputfile:
149
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
OtherSystemCalls forManagingFiles
Herearesome systemcallsthat operateontheselow-levelfile descriptors.
lseek
dup anddup2
150
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
The membersofthestructure,stat,mayvarybetweenUNIXsystems,butwillinclude:
151
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
Othermodeflagsinclude:
Therearesomemacrosdefinedtohelpwithdeterminingfiletypes. Theseinclude:
152
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
Fileandrecordlocking-fcntlfunction
• File locking isapplicableonlyforregular files.
• It allows a process to impose a lock on a file so that other processes cannot modifythe file until it is
unlocked by the process.
• Write lock: it prevents other processes from setting any overlapping read / write lockson the locked
region of a file.
• Readlock: itprevents other processesfrom setting any overlapping writelocks on the locked region of a
file.
#include<fcntl.h>
intfcntl(intfdesc,intcmd_flag,….);
FilePermission-chmod
chown
153
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
Asuperusercanchangetheownerofafileusingthechownsystemcall.
Links-softlinkandhardlink
Softlink(symbolic links):Refertoasymbolicpathindicatingtheabstractlocationof another file.
Usedtoprovidealternative meansofreferencingfiles.
Usersmaycreatelinksforfilesusinglncommandbyspecifying–s option.
hardlinks:Refer to thespecificlocationofphysicaldata.
AhardlinkisaUNIXpathnamefor afile.
Most of the files have only one hardlink. However users may create additional hardlinksfor files using ln
command.
Limitations:
Userscannotcreatehardlinksfordirectoriesunlesstheyhavesuperuser privileges.
Userscannotcreatehardlinksonafilesystemthatreferencesfilesonadifferentsystems.
unlink,link,symlink
Wecanremoveafileusingunlink.
Directories
154
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
mkdir, rmdir
Wecancreateandremovedirectoriesusingthemkdirandrmdirsystemcalls.
Themkdirsystemcallmakesa newdirectorywithpathasitsname.
Thermdirsystemcallremovesanemptydirectory.
chdir
Aprogramcannaviagate directoriesusingthechdirsystemcall.
CurrentWorkingDirectory-getcwd
Aprogramcandetermineitscurrentworkingdirectorybycalling thegetcwdlibraryfunction.
155
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
ScanningDirectories
opendir
Theopendirfunctionopensadirectoryand establishesadirectorystream.
readdir
156
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
telldir
Thetelldirfunctionreturnsavaluethatrecordsthecurrentposition inadirectorystream.
seekdir
Theseekdirfunctionsetsthedirectoryentrypointerinthedirectorystreamgivenbydirp.
12.13.3closedir
157
J.B.INSTITUTEOFENGINEERING
ANDTECHNOLOGY
LinuxProgrammingNotes
2. Nowwemoveontothemainfunction:
158
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
How ItWorks
After some initial error checking, using opendir, to see that the directoryexists, printdir makes a
call to chdir to the directoryspecified. While the entries returned by readdir aren't
null,theprogramcheckstoseewhethertheentryisadirectory.Ifit isn't,it printsthefileentry with
indentation depth.
159
J.B.INSTITUTEOFENGINEERING
ANDTECHNOLOGY
Hereisonewaytomaketheprogrammore general.
Youcanrunitusingthecommand:
$printdir /usr/local|more
160
J.B.INSTITUTEOFENGINEERING
ANDTECHNOLOGY
LinuxProgrammingNotes
UNIT-III
3. Process
ProcessconceptLayout of a C
program Image in main
memory
Processenvironment
Environment list
Environment variables- getenv,
setenvKernelsupportforprocess Process
identification
Processcontrol
ProcessCreation
ReplacingAProcessImage Waiting For A
Process
ProcessTermination Zombie
Process,
OrphanProcess
SystemCall InterfaceForProcess Management
Fork, vfork Exit Wait
Waitpid
ExecFamily Process
Groups
Sessions & Controlling Terminal
DifferencesBetweenThreads&Processes
161
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
Signals–Introductiontosignals
Signalgeneration and handling Kernel
support for signals
nalfunction-UnreliableSignals,Reliablesignals kill, raise
alarm,pause
3.17.3abort
3.17.4sleepfunctions.
Unit 3 ProcessesandSignals
III.Processesand Signals
WhatisaProcess
TheX/OpenSpecificationdefinesaprocessasanaddressspaceandsinglethreadofcontrolthat executes within
that address space and its required system resources.
162
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
LayoutofaCprogram
Here ishowacoupleofprocessesmightbearrangedwithintheoperationgsystem.
Imageinmain memory
ViewingProcesses
Wecanseewhat processesarerunningbyusingthe pscommand. Here is some
sample output:
163
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
164
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
Processenvironment
Herewecanseeoneveryimportant processindeed:
Theseareshowninthepsoutputlikethis:
165
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
When interacting with your server through a shell session, there are many pieces of information that your
shellcompilesto determine itsbehavior and accessto resources.Some ofthese settings are contained within
configuration settings and others are determined by user input.
One way that the shell keeps track of all of these settings and details is through an area it
maintainscalledtheenvironment. Theenvironmentisanareathattheshellbuildseverytimethat it starts a
session that contains variables that define system properties.
Every time a shell session spawns, a process takes place to gather and compile information that should be
available to the shell process and its child processes. It obtains the data for these settings from a variety of
different files and settings on the system.
Basically the environment provides a medium through which the shell process can get or set settings and,
in turn, pass these on to its child processes.
EnvironmentList
The environment is implemented as strings that represent key-value pairs. If multiple values are passed,
they are typically separated by colon (:) characters. Each pair will generally will look something like this:
KEY=value1:value2:...
KEY="valuewithspaces"
The keys inthese scenarios are variables. Theycan be one oftwo types, environmentalvariables or shell
variables.
166
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
By default, a child process inherits the environment variables of its parent process. Programs started by the
command processor inherit the command processor's environment variables. To specify a different
environment for a child process, create a new environment block and pass a pointer to it as a parameter to
the CreateProcess function.
The command processor provides the set command to display its environment block or to create new
environment variables. You canalso view or modifythe environment variables byselecting System from the
Control Panel, selectingAdvanced system settings, and clicking Environment Variables.
Eachenvironmentblockcontainstheenvironmentvariablesinthefollowingformat:
Var1=Value1\0
Var2=Value2\0
Var3=Value3\0
...
VarN=ValueN\0\0
Thenameofanenvironmentvariablecannotincludeanequalsign(=).
The GetEnvironmentStrings function returns a pointer to the environment block of the calling process. This
should be treated as a read-only block; do not modify it directly. Instead, use the SetEnvironmentVariable
function to change an environment variable. When you are finished
167
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
Kernelsupport forprocess
The kernelruns the show, i.e. it manages allthe operations in a Unix flavored environment. The kernel
architecture must support the primary Unix requirements. These requirements fall in two categories
namely, functions for process management and functions for file management (files include device files).
Process management entails allocation of resources including CPU, memory, and offers services that
processes may need. The file management in itself involves handling all the files required by processes,
communication with device drives and regulating transmissionofdatato and fromperipherals. The
kerneloperationgives the user processes a feel of synchronous operation, hiding all underlying
asynchronism in peripheral and hardware operations (like the time slicing by clock). In summary, we can
say that the kernel handles the following operations :
1. Itisresponsibleforschedulingrunningofuserandother processes.
2. Itisresponsibleforallocating memory.
3. Itisresponsibleformanagingtheswappingbetweenmemoryand disk.
4. Itisresponsibleformovingdatatoandfromtheperipherals.
5. itreceivesservicerequests fromtheprocessesandhonorsthem.
ProcessIdentification:
Everyprocess has a unique process ID, a non-negative integer. There aretwo specialprocesses.
ProcessID0isusuallythescheduleprocessand is oftenknownasthe‘swapper’. No programon disk corresponds
to this process – it is part of the kernel and is known as a systemprocess, process ID1is usuallythe ‘init’
process and is invoked bythe kernelat the end of the bootstrap procedure. The program files for this
process loss /etc/init in older version of UNIX and is
/sbin/init isnewerversion.‘init’usuallyreadsthesystemdependent initializationfilesandbrings the system to a
certain state. The ‘init’ process never dies. ‘init’ becomes the parent process of any orphaned child process.
Processcontrol
One furtherpsoutputexampleistheentryforthepscommanditself:
168
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
This indicatesthatprocess192isinarunstate(R)andisexecutingthecommandps-ax.
itwouldhavebeenallocatedanicevalueof+10.
Process Creation
StartingNewProcesses
169
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
hasbeengiventoashell.
Try ItOut-system
1. Wecanusesystemtowritea programto run psforus.
2. Whenwecompileandrunthisprogram,system.c,wegetthefollowing:
170
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
3. Thesystemfunctionusesashelltostartthedesired program.
How ItWorks
Inthefirstexample,theprogramcallssystemwiththestring"ps-ax",whichexecutes
thepsprogram.Ourprogramreturnsfromthecalltosystemwhenthe pscommandisfinished.
In the second example, the call tosystem returns as soon as the shell command finishes.
Theshellreturnsassoonasthe psprogramisstarted,just aswould happenifwehadtyped,
atashellprompt.
171
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
ReplacingaProcessImage
There isawhole familyofrelated functionsgroupedunderthe exec heading.Theydiffer inthe way that they
start processes and present program arguments.
TryItOut-exclp
Let's modifyour exampletousean exexlp call.
172
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
Notealsothatthereisnoreferencetoaprocesscalledpexecinthe output:
How ItWorks
Theprogramprintsitsfirst messageandthencalls execlp,whichsearchesthedirectoriesgiven by the PATH
environment variable for a program called ps.
WaitingforaProcess
173
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
Wecaninterrogatethestatusinformationusingmacrosdefinedinsys/wait.h. Theseinclude:
TryItOut-wait
174
J.B.INSTITUTEOFENGINEERINGAND
TECHNOLOGY
LinuxProgrammingNotes
2. Thissectionoftheprogramwaitsforthechildprocessto finish:
175
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
How ItWorks
176
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
ZombieProcesses
Try ItOut-Zombies
Herearetherelevantlinesofcode:
How ItWorks
177
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
OrphanProcess
• WhentheparentdiesfirstthechildbecomesOrphan .
• Thekernelclearstheprocesstableslotfortheparent.
Systemcallinterfaceforprocessmanagement
pid_tgeppid(void); Returns:parentprocessIDOFcallingprocess
forkFunction
178
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
The only way a new process is created by the UNIX kernel is when an existing process calls the fork
function.
#include<sys/types.h>
#include<unistd.h>pid_t
fork(void);
Return:0ischild,processIDofchildinparent,-1onerror
The new process created byfork is called child process. This is called once, but returntwice that is the return
value in the child is 0, while the return value in the parent is the process ID of the new child. The reason the
child’s process ID is returned to the parent is because a process can have more than one child, so there is
no function that allows a process to obtain the process IDs of its children. The reason fork return 0 to the
child is because a process can have only a single parent, so that child can always call getppid to obtain the
process ID of its parent.
Boththe child and parent containexecuting withthe instructionthat follows the callto fork. The child is copy
of the parent. For example, the child gets a copy of the parent’s data space, heapand stack. This is a copy
for the child the parent and children don’t share these portions of memory. Often the parent and child
share the text segment, if it is read-only.
Therearetwo usersforfork:
1. When a process wants to duplicate itself so that the parent and child can each execute different sections of
code at the same time. This is common for network servers_ the parent
waitsforaservicerequestsfromaclient.Whentherequest arrives,theparent calls fork and lets the child handle
the request. The parent goes back to waiting for the next service request to arrive.
When a process wants to execute a different program,this is common for shells. Inthis case the child
does an exec right after it returns from the fork.
vforkFunction
179
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
The function vfork has the same calling sequence and share return values as fork. But the semantics ofthe
two functions differ. vfork is intended to create anew process whenthe purpose of the new process is to
exec a new program. vfork creates the new process, just like fork,without fully copying the address space of
the parent into the child, since the child won’t reference the address space – the child just calls exec right
after the vfork. Instead, while thechild is running, untilit calls either exec or exit, the child runs in the
address space ofthe parent. This optimization provides an efficiencygain on some paged virtual memory
implementations of UNIX.
Anotherdifferencebetweenthetwo functionsisthat vforkguaranteesthatthechildrunsfirst, until the parent
resumes.
exit Function
Therearethreewaysforaprocesstoterminatenormally,andtwoformsofabnormal termination.
1. Normaltermination:
a. Executingareturnfromthemainfunction.Thisis equivalenttocallingexit
b. Callingtheexitfunction
c. Callingthe_exitfunction
2. Abnormaltermination
a. Callingabort:ItgeneratestheSIGABRTsignal
b. Whentheprocessreceivescertainsignals. Thesignalcanbegeneratedbythe process itself
Regardless ofhow a process terminates, the same code in the kernel is eventuallyexecuted. This kernelcode
closes allthe opendescriptors fortheprocess, releases the memorythat it was using, and the like.
For any of the preceding cases we want the terminating process to be able to notify its parenthow it
terminated. For the exit and _exit functions this is done by passing an exit status as the argument to these
two functions. In the case of an abnormal termination however, the kernel generates a termination status
to indicate the reason for the abnormal termination. In any case,the parent of the process can obtain the
termination status from either the wait or waitpid
180
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
function.The exit status is converted into a termination status by the kernel when _exit is finally called. If
the child terminated normally, then the parent can obtain the exit status of the child.
If the parent terminates before the child, then init process becomes the parent process of any process,
whose parent terminates; that is the process has been inherited by init. Whenever a
processterminatesthekernelgoesthroughallactiveprocessesto seeiftheterminatingprocess is the parent of
any process that still exists. If so, the parent process ID of the still existing process is changed to be 1 to
assume that everyprocess has a parent process.
When a child terminates before the parent, and if the child completely disappeared, the parent wouldn’t
be able to fetch its termination status, when the parent is ready to seek if the child had terminated. But
parent get this information by calling wait and waitpid, which is maintained by the kernel.
waitandwaitpidFunctions
When a process terminates, either normally or abnormally, the parent is notified by the kernel sending the
parent SIGCHLD signal. Since the termination of a child is an asynchronous event, this signal is the
asynchronous notification from the kernel to the parent. The default action for this signal is to be ignored.
A parent may want for one of its children to terminate and thenaccept it child’s termination code by
executing wait.
Aprocessthatcallswaitandwaitpidcan
181
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
pid_twait(int*statloc);
pid_twaitpid(pid_tpid,int*statloc,intoptions); Both
return: process ID if OK, o or -1 on error The
difference between these two functions is
1. waitcanblockthecalleruntilachildprocessterminates,whilewaitpidhasanoption that prevents it from
blocking.
2. waitpiddoes notwaitfor thefirstchildtoterminate,ithas anumberof options that control which
process it waits for.
If a child has already terminated and is a zombie, wait returns immediately with that child’s
status.Otherwise, it blocksthecalleruntilachildterminates: ifthecallerblocksand hasmultiple children, wait
returns when one terminates, we can know this process by PID return by the function.
For both functions, the argument statloc is pointer to an integer. If this argument is not a null pointer,the
terminationstatusofthe terminated process isstored inthe locationpointed to bythe argument.
If we have more than one child, wait returns on termination of any of the children. A function that waits for
a specific process is waitpid function.
Theinterpretationofthepidargumentforwaitpiddependsonitsvalue:
182
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
1. waitpidletsustowaitforoneparticularprocess
2. waitpidprovidesanon-blockingversionofwait
3. waitpidsupportsjobcontrol(wittheWUNTRACEDoption)
exec Function
The fork function can create a new process that then causes another program to be executed by calling one
of the exec functions. When a process calls one of the exec functions, that process is completely replaced
by the new program and the new program starts executing at its main function. The process ID doesn’t
change across an exec because a new process is not created. exec merely replaces the current process with
a brand new program from disk.
There are six different exec functions. These six functions round out the UNIX control primitives. With fork
we can create new processes, and with the exec functions we can initiate new programs. The exit function
and the two wait functions handle termination and waiting for termination. These are the only process
control primitives we need.
#include<unistd.h>
183
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
intexecve(constchar*pathname,char*constargv[],char*constenvp[]); int
execlp(const char *pathname, const char *arg0, . . . /* (char *) 0*/); int
execvp(const char *filename, char *const argv[]);
Allsixreturns:-1onerror,noreturnonsuccess.
Specifiesfourdirectoriestosearch,where lastoneiscurrentworkingdirectory.
If either of the two functions, execlp or execvp finds an executable file using one of the path prefixes, but
the file isnot amachine executable that wasgeneratedbythe link editor,it assumes the file is a shell script
and tries to invoke /bin/sh with filename as input to the shell.
The next difference concerns the passing ofargument list. The function execl, execlp and execle require
each of the command-line arguments to the new program to be specified as separate arguments.
Theendoftheargument should bea nullpointer. Fortheotherthreefunctionsexecv, execvp and execve, we
have to build an array of pointers to the arguments, and the address ofthis array is the argument to these
three functions.
The finaldifference isthe passing ofthe environment list tothe new program. The two functions execle and
execve allow us to pass a pointer to an array of pointer to an array of pointer to an array of pointers to the
environment strings. The other four functions, however, use the environ variable in the calling process to
copythe existing environment for the new program.
184
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
DifferencesBetweenThreadsandProcesses
UNIXprocessescancooperate;theycansendeachother messagesandtheycaninterrupt one another.
Signals
185
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
Additionalsignals include:
Ifthe shelland terminaldriver are configured normally, typing the interrupt character (Ctrl-C)
atthekeyboardwillresult inthe SIGINTsignalbeingsenttotheforegroundprocess.Thiswill cause the program
to terminate.
Wecanhandlesignalsusingthesignal libraryfunction.
Signalgeneration&Handling
186
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
187
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
How ItWorks
Theprogramarranges forthefunctionouchtobecalledwhenwetypeCtrl-C, whichgives the SIGINT
signal.
KernelsupportforSignals-Sending Signals
Aprocessmaysend asignaltoitselfbycallingraise.
Signalsprovideuswithausefulalarmclockfacility.
188
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
TryItOut-AnAlarmClock
1. Inalarm.c,thefirstfunction,ding,simulatesanalarmclock:
189
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
How ItWorks
Thealarmclocksimulationprogramstartsanewprocessvia fork. Thischildprocesssleeps for five
seconds and then sends a SIGALRM to its parent.
ARobustSignalsInterface
X/Openspecificationrecommendsanewerprogramminginterfaceforsignalsthat is more
robust: sigaction.
190
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
Try ItOut-sigaction
191
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
192
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
How ItWorks
Signal Sets
Thefunctionsigismemberdetermineswhether thegivensignalisamemberofasignalset.
193
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
Theprocesssignalmaskissetorexaminedbycallingthefunctionsigprocmask.
Thisisamoregeneralformofthepausefunctionwemetearlier.
194
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
Signal Functions
Thesystemcallsrelatedtosignalsareexplainedinthefollowing sections.
Unreliablesignals
The signals could get lost – a signal could occur and the process would never know about it. Here, the
process has little control over a signal, it could catch the signal or ignore it, but blocking of a signal is not
possible.
Reliablesignals
killandraiseFunctions
195
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
Therearefourdifferentconditionsforthepidargumenttokill:
pid>0ThesignalissenttotheprocesswhoseprocessIDispid.
0 The signal is sent to all processes whose process group ID equals the process
groupIDofthesenderandforwhichthesenderhaspermissionto send the signal.
The signal is sent to all processes whose process groupID equals
theabsolute value of pid and for which the sender has permission to send the signal.
pid= -1 unspecified.
The alarm function allows us to get a timer that will expire at a specified time in the future.When the timer
expires, the SIGALRM signal is generated. If we ignore or don’t catch this signal, its default action is to
terminate the process.
#include<unistd.h>
unsignedintalarm(unsignedintseconds);
Returns:0ornumberofsecondsuntilpreviouslysetalarm.
The seconds value is the number of clock seconds in the future when the signal should be generated. There
is only one of the alarm clocks per process. If, when we call alarm, there is a previouslyregistered
alarmclock for the process that has not yet expired, the number of seconds left for that alarm clock to
return as the value of this function. That previously registered alarm clock is replaced by the new value.
If there is a previously registered alarm clock for the process that has not yet expired and if the seconds
value is 0, the previous alarm clock is cancelled. The number of seconds left for that previous alarm clock is
still returned as the value of the function.
Although the default action for SIGALRM is terminating the process, most processes use an alarm clock
catch this signal.
196
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
abortfunctioncausesabnormalprogramtermination. #include<stdlib.h>
voidabort(void);
Thisfunctionneverreturns.
#include<unistd.h>
unsignedintsleep(unsignedintseconds);
Returns:0or number ofunsleptseconds.
Thisfunctioncausesthecallingprocesstobesuspendeduntileither:
1. Theamountofclockthatisspecifiedbysecondshaselapsedor
2. Asignaliscaughtbytheprocessandthesignalhandlerreturns.
Incase1thereturnvalue is0whensleep returnsearly, becauseofsomesignalbeingcaught case 2, the
return value is the number of unslept seconds.
Sleepcanbe implementedwithanalarmfunction. Ifalarmisused, however, therecanbe interaction
between the two functions.
197
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
UNIT-IV:
IV.InterprocessCommunication
IntroductiontoIPC
IPC between processes on a single computer
systemIPCbetweenprocessesondifferent systems Pipes-
creation
IPCbetweenrelatedprocessesusingunnamedpipes
FIFOs-creation,IPCbetweenunrelatedprocessesusingFIFOs(named pipes) Differences
Between Unnamed And Named Pipes
popen&pcloselibraryfunctions.
4.9MessageQueues-Kernelsupportfor messages
APIsformessagequeues client/server
example.
Semaphores-Kernelsupportforsemaphores
APIsforsemaphores
FilelockingwithSemaphores
198
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
UnitIV
Interprocesscommunication,MessageQueuesandSemaphores
IntroductiontoIPC
Interprocess communication (IPC) includes thread synchorization and data exchange between
threadsbeyond theprocessboundaries. Ifthreads belongtothesameprocess, theyexecuteinthe same address
space, i.e. theycan access global(static) data or heap directly, without the help of the operating system.
However, ifthreads belong to different processes, theycannot accesseach others address spaces without
the help of the operating system.
TherearetwofundamentallydifferentapproachesinIPC:
processesareresidingonthesame computer
processesareresidingondifferentcomputers
The first case is easier to implement because processes can share memory either in the userspace or in the
system space. This is equally true for uniprocessors and multiprocessors.
In the second case the computers do not share physical memory, they are connected via I/O device(for
example serial communication or Ethernet). Therefore the processes residing in different computers can
not use memory as a means for communication.
IPCbetweenprocessesonaSingle System
MostofthischapterisfocusedonIPConasingle computersystem, including four general
approaches:
Shared memory
Messages
Pipes
Sockets
199
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
h=init_CS("xxx");
h=init_semaphore(20,"xxx"
); h = init_event("xxx");
h=init_condition("xxx");
h=init_message_buffer(100,"xxx");
IPCbetweenprocessesondifferentsystems
IPCbetweenprocessesondifferent systems
IPCisInterProcessCommunication, moreofatechniquetosharedataacrossdifferentprocesses
withinonemachine,insuchawaythatdatapassing bindsthecouplingofdifferentprocesses.
Thefirst,isusingmemorymappingtechniques,whereamemorymapiscreated,and others
open the memory map for reading/writing...
The second is, using sockets, to communicate with one another...this has a high overhead,
aseachprocesswouldhavetoopenupthesocket,communicateacross... althougheffective
Thethird,istouseapipeor anamedpipe,averygoodexample
PIPES:
A pipe is a serial communication device (i.e., the data is read in the order in which it was
written),which allows a unidirectional communication. The data written to end
isreadbackfromtheotherend.
The pipe is mainly used to communicate between two threads in a single process or between
parent and child process. Pipes can only connect the related process. In shell,
thesymbolcanbeusedtocreateapipe.
In pipes the capacity of data is limited. (i.e.) If the writing process is faster than the reading
process which consumes the data, the pipe cannot store the data. In this situation the writer
process will block until more capacity becomes available. Also if the reading process tries toread
data when there is no data to read, it will be blocked until the data becomes available. By this,
pipes automatically synchronize the two process.
Creatingpipes:
The pipe() function provides a means of passing data between two programs and also allows to
read and write the data.
#include<unistd.h>
intpipe(intfile_descriptor[2]);
pipe()function is passed with an arrayof file descriptors. It will fillthe arraywith new file
descriptorsandreturnszero. Onerror,returns -1andsetstheerrnotoindicatethereasonof
200
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
failure.
PipesareoriginallyusedinUNIXandaremadeevenmorepowerfulinWindows95/NT/2000.
Pipes are implemented in file system. Pipes are basically files with onlytwo file offsets: one for
readinganotherforwriting. Writingto apipeandreading fromapipe isstrictlyin FIFO manner. (Therefore pipes
are also called FIFOs).
For efficiency, pipes are in-core files, i.e. they reside in memory instead on disk, as any other global data
structure. Therefore pipes must be restricted in size, i.e. number ofpipe blocks must be limited. (In UNIX
the limitation is that pipes use only direct blocks.)Since the pipes have a limited size and the FIFO access
discipline, the reading and writing processes are synchronized in a similar manner as incase of message
buffers. The access functions for pipesare the same as for files: WriteFile() and ReadFile().
Pipesusedasstandardinput andoutput:
Wecaninvokethestandardprograms,onesthatdon’t expect afiledescriptorasaparameter. #include<unistd.h>
intdup(intfile_descriptor);int
dup2(intfile_descriptor_1,int file_descriptor_2);
//pipes.c #include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
201
J.B.INSTITUTEOFENGINEERING
ANDTECHNOLOGY
LinuxProgrammingNotes
#include<string.h>int
main()
{
intdata_processed; int
file_pipes[2];
constcharsome_data[]=
"123";pid_tfork_result;
if(pipe(file_pipes)==0)
{
fork_result=fork();
if(fork_result==(pid_t)-1)
{
fprintf(stderr,"forkfailure");
exit(EXIT_FAILURE);
}
202
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
if(fork_result==(pid_t)0)
{
close(0); dup(file_pipes[0]);
close(file_pipes[0]);
close(file_pipes[1]);
execlp("od","od","-c",(char*)0);
exit(EXIT_FAILURE);
}
else
{
close(file_pipes[0]); data_processed=write(file_pipes[1],
some_data,strlen(some_data));close(file_pipes[1]);
printf("%d-wrote%dbytes\n",(int)getpid(),data_processed);
}
}exit(EXIT_SUCCESS);
}
The programcreates a pipe and then forks. Nowbothparent and child process will have its
ownfiledescriptorsforreadingandwriting.Thereforetotallythereare fourfiledescriptors.
Since the parent never read the pipe, it starts byclosing the read end that is file_pipe[0]. When
writingprocessofdatahasbeenfinished, thewriteendoftheparent isclosedandexited. Asthere are no file
descriptor open to write to pipe, the odcommand will be able to read the three bytes written to pipe,
meanwhile the reading process willreturn 0 bytes indicating the end of the file.
Therearetwotypesofpipes:
Namedpipes.
Unnamedpipes(Anonymouspipes)
FIFOcreation:
203
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
intmkfifo(constchar*pathname,mode_tmode);
- makesaFIFOspecialfilewithname pathname.
(modespecifiestheFIFO'spermissions,ascommoninUNIX-like file systems).
- AFIFOspecialfileissimilartoapipe,exceptthatitiscreatedin
adifferent way.Insteadofbeingananonymouscommunicationschannel,a FIFO special file is
entered into the file system by calling mkfifo()
Creationof FIFO:
WecancreateaFIFOfromthecommandlineand withinaprogram.
intmkfifo(const char
*filename,mode_tmode);
int mknod(const char *filename,
mode_tmode|S_IFIFO,(dev_t)0);
If we want to use the mknod function we have to use ORing process of fileaccess mode with
S_IFIFOandthedev_tvalueof0.Instead ofusingthiswecanusethesimplemkfifo function.
Accessing FIFO:
LetusfirstdiscusshowtoaccessFIFOincommand lineusing filecommmands.The useful feature of named
pipes is, as theyappear inthe file system, we canuse them in commands.
Wecanreadfromthe FIFO(empty)
$cat</tmp/my_fifo
Now,letuswritetotheFIFO.
$echo"Simple!!!">/tmp/my_fifo
(Note:Thesetwocommandsshouldbeexecutedindifferentterminalsbecausefirst command will
be waiting for some data to appear in the FIFO.)
204
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
FIFOcanalso beaccessedas likea file intheprogramusing low-levelI/O functionsorC library I/O functions.
The only difference between opening a regular file and FIFO is the use of open_flag with the
optionO_NONBLOCK.Theonlyrestrictionisthatwecan’topenFIFO forreadingandwriting with O_RDWR mode.
//fifo1.c
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h> #include
<string.h> #include
<fcntl.h> #include
<limits.h>
#include<sys/types.h>
#include <sys/stat.h>
int main()
{
intpipe_fd; int
res;
intopen_mode=O_WRONLY; int
bytes_sent = 0;
charbuffer[BUFFER_SIZE+1];
if(access(FIFO_NAME,F_OK)==-1){ res =
mkfifo(FIFO_NAME, 0777);
if(res!= 0){
fprintf(stderr,"Couldnotcreatefifo%s\n",
FIFO_NAME); exit(EXIT_FAILURE);
}
}
printf("Process%dopeningFIFOO_WRONLY\n",getpid()); pipe_fd =
open(FIFO_NAME, open_mode);
printf("Process%dresult%d\n",getpid(),pipe_fd); if
(pipe_fd != -1) {
while(bytes_sent<TEN_MEG){
res=write(pipe_fd,buffer,BUFFER_SIZE);if (res == -1) {
fprintf(stderr,"Writeerroronpipe\n");
exit(EXIT_FAILURE);
}
205
J.B.INSTITUTEOFENGINEERINGAND
TECHNOLOGY
LinuxProgrammingNotes
}
(void)close(pipe_fd);
}
else{ exit(EXIT_FAILURE);
}
printf("Process %d finished\n",
getpid());exit(EXIT_SUCCESS);
}
//fifo2.c
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h> #include
<string.h> #include
<fcntl.h> #include
<limits.h>
#include<sys/types.h>
#include <sys/stat.h>
#defineFIFO_NAME"/tmp/my_fifo" #define
BUFFER_SIZE PIPE_BUF int main()
{
intpipe_fd; int
res;
int open_mode =
O_RDONLY;charbuffer[BUFFER_SIZE+1];in
t bytes_read = 0;
memset(buffer,'\0',sizeof(buffer));
printf("Process %d opening FIFO O_RDONLY\n",
getpid());pipe_fd=open(FIFO_NAME,open_mode); printf("Process
%d result %d\n", getpid(), pipe_fd);
if(pipe_fd!=-1)
{do{
res = read(pipe_fd, buffer,
BUFFER_SIZE);bytes_read+=res;
} while (res > 0);
(void)close(pipe_fd);
}
else { exit(EXIT_FAILURE);
}
printf("Process%dfinished,%dbytesread\n",getpid(), bytes_read);
exit(EXIT_SUCCESS);
}
Bothfifo1.candfifo2.cprogramsusetheFIFOinblockingmode.
206
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
Firstfifo1.cisexecuted.Itblocksandwaitsfor readerto open the named pipe. Now writer unblocks and starts
writing data to pipe. At the same time, the reader starts reading data from the pipe.
4.5Unnamedpipes(Anonymous Pipes)
Anonymous pipes are typically used to "pipe" two programs: standard output fromone program is
redirected to the pipe input (write handle), while the standard inputof the second program is redirected to
from the pipeoutput (read handle). The pipe is created by the parent (usually the login shell), and the pipe
handles are passedto children through the inheritance mechanism.
Anonymous pipes cannot be used across a network. Also, anonymous pipes are unidirectional- in order to
communicatetworelatedprocessesinbothdirections,two anonymous pipes must be created.
ExampleofWin32anonymouspipesusedforprogram piping:
//*********************************************
**********************
//Thisprogramimplementspipingof
programs p1.exe
and p2.exe
// through
an anonymouspipe.The
sa.n Len program creates
sgath.bInheritHandle= two child
= processes
TRUE;//Handlesare //(which execute
s iz e images p1.exe
i n h eritable(defaultis
and p2.exe)
and a pipe,
o f (S
F A LS E ) HANDLErh,wh;thenpasses
EC o f t h e p ip e // thepipehandlesto the
/ / Readandwritehandles "TC Yo _ u ld n 't
cUoR children.
icAfr(Te!aCtreepatipePeipe(&rh,&wh,
//
"T&R<sa<I,0)) // The
BGUetLastErroTrE( )S<< program is
)e;ndl;return (s 1a.)l; invokedas:
pSe curit yDe scri ptor pipe p1 p2
=0;
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
(nocommandlinearguments)
//******************************************************************* #include
<windows.h>
#include<iostream.h>
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
si1.hStdInput=GetStdHandle(STD_INPUT_HANDLE);
si1.hStdOutput= wh; // Stdoutputofp1isinputtothe pipe
si1.dwFlags=STARTF_USESTDHANDLES;
CreateProcess(argv[1], //Nameofthep1'simage(without ".exe."
0,0,0,
TRUE, //Eachopeninheritablehandleofthe
//parentwillbeinheritedbythechild
0,0,0,
&si1,&pi1);
CloseHandle(wh); //Pipehandlenolongerneeded
// Createthesecondchild processp2
PROCESS_INFORMATIONpi2;
STARTUPINFOsi2;
GetStartupInfo(&si2); //Getdefaultstartupstructure
si2.hStdInput=rh; //Stdinputofp2isotputfromthepipe
si2.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
si2.dwFlags=STARTF_USESTDHANDLES;
CreateProcess(0,argv[2], //Nameofthep1'simage(without".exe."
0,0,
TRUE, //Eachopeninheritablehandleofthe
//parentwillbeinheritedbythechild
0,0,0,
&si2,&pi2); WaitForSingleObject(pi1.hProcess,INFINITE);
CloseHandle(pi1.hProcess);
WaitForSingleObject(pi2.hProcess,INFINITE);
CloseHandle(pi2.hProcess);
CloseHandle(rh); return(0);
}
Comment:
Inorderto communicatetwo processes (P1 and P2) throughanonymous pipes byredirecting the standard
I/O,the processesdon't have to be awareofthe existence ofpipes, i.e.their sourcesand images don't have to
be modified.
4.8Pipeprocessing:(popen&pcloselibraryfunctions)
208
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
popen():
The popen function allows a program to invoke another program as a new process and either writethe
datato it orto read fromit. The parameter command is the name ofthe programto run.
Theopen_modeparameterspecifiesinwhichmode it istobe invoked,it canbeonlyeither"r"or "w". On failure
popen() returns a NULL pointer. If you want to perform bi-directional communication you have to use two
pipes.
pclose():
By using pclose(), we can close the filestream associated with popen() after the process started by it has
been finished. The pclose() will return the exit code ofthe process, which is to be
closed.Iftheprocesswasalreadyexecuteda waitstatement beforecallingpclose,theexit status willbelost
becausetheprocesshasbeenfinished.Afterclosingthefilestream,pclose()willwait for the child process to
terminate.
Messagequeue:
This is an easy way of passing message between two process. It provides a way of sending a block of data
from one process to another. The main advantage of using this is, each block of data is considered to have
a type, and a receiving process receives the blocks of data having different type values independently.
Creationandaccessingofamessage queue:
Youcancreateand accessamessagequeueusingthemsgget()
function. #include<sys/msg.h>
intmsgget(key_tkey,intmsgflg);
The first parameter is the key value, which specifies the particular message queue. Thespecial constant
IPC_PRIVATE will create a private queue. But on some Linux systems the message queue may not
actually be private.
Thesecondparameter istheflagvalue,whichtakesninepermission flags.
Addinga message:
Themsgsnd() functionallowsto addamessagetoamessage queue.
#include<sys/msg.h>
intmsgsnd(intmsqid,constvoid*msg_ptr,size_tmsg_sz,intmsgflg);
Retrievingamessage:
Thesmirch()functionretrievesmessagefromthemessagequeue.
209
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
#include<sys/msg.h>
intmsgsnd(intmsqid,constvoid*msg_ptr
,size_tmsg_sz,longintmsgtype ,intmsgflg);
On success, msgrcv returns the number on bytes placed in the receive buffer, the message is copied
intotheuser-allocatedbufferandthedataisdeletedfromthe messagequeue.Itreturns -1 on error.
Controllingthemessagequeue:
Thisisverysimilarthatofcontrolfunctionofsharedmemory.
#include<sys/msg.h>
intmsgctl(intmsgid,intcommand, struct
msqid_ds *buf);
Thesecond parametertakesthevaluesasgivenbelow:
210
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
queue.
Client /serverExample:
//msgq1.c
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<errno.h>
#include<unistd.h>
#include<sys/
msg.h>struct
my_msg_st
{
long int my_msg_type;
charsome_text[BUFSIZ
];
};
int main()
{
intrunnin
g=
1;intmsgi
d;
structmy_msg_stsome_da
ta; long int
msg_to_receive = 0;
msgid=msgget((key_t)1234,
0666 | IPC_CREAT);
if(msgid==-1)
{
fprintf(stderr,"failedtoget:\n");
exit(EXIT_FAILURE);
}
211
J.B.INSTITUTEOFENGINEERING
ANDTECHNOLOGY
LinuxProgrammingNotes
while(running)
{
if(msgrcv(msgid,(void*)&some_data, BUFSIZ,msg_to_receive,0) == -1)
{
fprintf(stderr,"failedtoreceive:
\n");exit(EXIT_FAILURE);
}
}
}
if(msgctl(msgid,IPC_RMID, 0)==-1)
{
fprintf(stderr,"failedtodelete\n");
exit(EXIT_FAILURE);
} exit(EXIT_SUCCESS);
}
//msgq2.c
structmy_msg_st
{
longintmy_msg_type;
charsome_text[MAX_TEXT];
};
int main()
{
intrunning=1;
212
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
structmy_msg_stsome_data; int
msgid;
charbuffer[BUFSIZ];
msgid=msgget((key_t)1234, 0666 |
IPC_CREAT);
if(msgid==-1)
{
fprintf(stderr,"failedtocreate:\n");
exit(EXIT_FAILURE);
}
while(running)
{
printf("Enter Some Text: "); fgets(buffer,
BUFSIZ, stdin); some_data.my_msg_type
= 1;
strcpy(some_data.some_text,buffer);
if(msgsnd(msgid,(void*)&some_data,MAX_TEXT,0)==-1)
{
fprintf(stderr,"msgsndfailed\n");
exit(EXIT_FAILURE);
}
if(strncmp(buffer,"end",3)==0)
{
running= 0;
}
} exit(EXIT_SUCCESS);
}
Themsgq1.c program will create the message queue using msgget() function.
Themsgididentifierisreturnedbythemsgget().Themessagearereceivedfrom the queue
usingmsgrcv()functionuntilthe string "end" is encountered. Then the queueis
deletedusingmsgctl()function.
Themsgq2.c program uses themsgsnd()function to sendthe enteredtext to the queue.
Semaphore:
While we are using threads in our programs in multi-user systems, multiprocessing system, or a combinationoftwo,
wemayoftendiscover critical sectionsinthecode.Thisisthesectionwhere
wehavetoensurethatasingleprocesshasexclusiveaccess totheresource.
Forthispurposethesemaphoreisused.It allowsin managing theaccesstoresource.
Toprevent theproblemofoneprogramaccessing thesharedresourcesimultaneously,weare in
213
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
need to generate and use a token which guarantees the access to only one
threadofexecutioninthecriticalsectionatatime.
It is counter variable, which takes only the positive numbers and upon which programs can only act
atomically. The positive number is the value indicating the number of units of the shared resources are
available for sharing.
Creationofsemaphore:
Theshmget() functioncreatesanewsemaphoreorobtainsthesemaphorekeyofan existing
semaphore.
#include<sys/sem.h>
intshmget(key_tkey,intnum_sems, intsem_flags);
The first parameter, key, is an integral value used to allow unrelated process to access the same
semaphore. The semaphorekeyis used onlybysemget. Allothers usethe identifier returnbythe semget().
There is a special key value IPC_PRIVATE whichallows to create the semaphore and
to be accessed only by the creating process.
The second parameteristhe numberof semaphores required,it isalmost always1.
Thethirdparameter isthesetofflags.Theninebitsarethepermissionsforthe semaphore.
Changingthe value:
Thefunctionsemop()isusedforchangingthevalueofthe semaphore.
#include<sys/sem.h>
intsemop(intsem_id,structsembuf
*sem_ops,size_tnum-_sem_ops);
Thefirstparameteristheshmidistheidentifierreturnedbythesemget().
struct
sembuf{ shortsem
_num; short
sem_op; short
sem_flg;
}
214
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
The first member is the semaphore number, usually0 unless it is an arrayofsemaphore. The sem_op is
the value by which the semaphore should be changed. Generally it takes-1,which isoperationto wait
forasemaphoreand+1, whichistheoperationto signaltheavailabilityof semaphore.
#include<sys/sem.h>
intsemctl(intsem_id,intsem_num,int
command,.../*union semun arg */);
unionsemun
{
intval;
structsemid_ds*buf;
unsigned
short
*array;};
215
J.B.INSTITUTEOFENGINEERINGAND
TECHNOLOGY
LinuxProgrammingNotes
staticint set_semvalue()
{
unionsemunsem_union;
sem_union.val = 1;
if(semctl(sem_id,0,SETVAL,sem_union)==-1) return(0);
return(1);
}
//
staticvoiddel_semvalue()
{
unionsemunsem_union;
if(semctl(sem_id,0,IPC_RMID,sem_union)==-
1)fprintf(stderr,"Failedtodeletesemaphore\n");
}
//
staticint semaphore_p()
{
struct sembuf sem_b; sem_b.sem_num
= 0; sem_b.sem_op = -1; /* P() */
sem_b.sem_flg=SEM_UNDO;
if(semop(sem_id,&sem_b,1) ==-1)
{
fprintf(stderr,"semaphore_pfailed\n"); return(0);
}
return(1);
}
//
staticint semaphore_v()
{
struct sembuf sem_b; sem_b.sem_num
= 0; sem_b.sem_op = 1; /* V() */
sem_b.sem_flg=SEM_UNDO;
if(semop(sem_id, &sem_b, 1)==-1)
{ fprintf(stderr,"semaphore_vfailed\n");
return(0);
}
216
J.B.INSTITUTEOFENGINEERINGAND
TECHNOLOGY
LinuxProgrammingNotes
return(1);
}
intmain(intargc,char*argv[])
{
inti;
int pause_time;
charop_char='O';
srand((unsignedint)getpid());
sem_id=semget((key_t)1234,1,0666|IPC_CREAT); if (argc > 1)
{
if(!set_semvalue())
{
fprintf(stderr,"Failedtoinitializesemaphore\n"); exit(EXIT_FAILURE);
}
op_char='X';
sleep(2);
}
for(i=0;i<10;i++)
{
if(!semaphore_p())exit(EXIT_FAILURE);
printf("%c", op_char);
fflush(stdout);
pause_time=rand()% 3;
sleep(pause_time);
printf("%c",op_char);fflush(stdout);
if (!semaphore_v())
exit(EXIT_FAILURE);pause_time= rand() % 2;
sleep(pause_time);
}
printf("\n%d-finished\n", getpid()); if
(argc > 1)
{
sleep(10);
del_semvalue();
} exit(EXIT_SUCCESS);
}
217
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
In the function semaphore_v(),the semop member of the structure sembuf is set to 1.By this the semphore
becomes available for the other processes because it is released.
218
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
UNIT-V:
SharedMemory
Sockets Introduction to
Berkeley SocketsIPCoveranetwork
Client/server model
SocketAddressstructures(UNIXdomain&internetdomain)
cket systemcallsforconnectionorientedprotocolandconnectionlessprotocol Example-client/server
programs- single client/server connection,
Multiplesimultaneousclients
Socketoptions
setsockopt
fcntlsystem calls
ComparisionofIPCmechanisms.
219
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
UNITV
SharedMemoryandSockets
SharedMemory:
Shared memoryis a highlyefficient wayofdatasharing betweenthe runningprograms. It allows two unrelated
processesto access the same logical memory. It is the fastest formofIPC because all processes share the
same piece of memory. It also avoidscopyingdataunnecessarily.
As kernel does not synchronize the processes, it should be handled by the user. Semaphore can also be
used to synchronize the access to shared memory.
Usageofsharedmemory:
To use the shared memory, first of all one process should allocate the segment, and then each process
desiring to access the segment should attach the segment. After accessing the segment, each process
should detach it. It is also necessary to deallocate thesegmentwithoutfail.
Allocating the shared memory causes virtual pages to be created. It is important to note that allocating the
existing segment would not create new pages, but will return theidentifierfortheexistingpages.
Unixkernelsupport forsharedmemory
Eachentryofthetablesstorethefollowing
data:
1. Name
2. CreatoruserIDandgroupID.
3. Assignedowner userIDandgroupID.
4. Read-writeaccesspermissionofthe region.
5. Thetimewhenthelastprocessattachedtothe region.
6. Thetimewhenthelastprocessdetachedfromthe region.
7. Thetime whenthelastprocesschangedcontroldataoftheregion.
8. Thesize,inno.ofbytesofthe region.
UNIXAPIsforsharedmemory shmget
Openandcreateashared
memory.
220
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
Function
prototype:
#include<sys/types.h> #include<sys/ipc.h>
#include<sys/shm.h>
intshmget(key_tkey,intsize, intflag );
Functionreturnsapositivedescriptorifitsucceedsor-1ifit
fails.
Shmat
Attachasharedmemorytoaprocessvirtualaddress
space.
Function
prototype:
#include<sys/types.h> #include<sys/ipc.h>
#include<sys/shm.h>
void*shmat(intshmid,void*addr,intflag);
Functionreturnsthemappedvirtualaddressofhesharedmemoryifitsucceedsor-1ifit
fails.
Shmdt
Detachasharedmemoryfromthe processvirtualaddress
space.
Function
prototype:
#include<sys/types.h> #include<sys/ipc.h>
#include<sys/shm.h>int shmdt ( void
*addr );
Functionreturns0ifitsucceedsor-1ifit
fails.
Shmctl
Queryor changecontroldataofasharedmemoryordeletethe
memory.
221
J.B.INSTITUTEOFENGINEERINGAND
TECHNOLOGY
LinuxProgrammingNotes
Functionprototype:
#include<sys/types.h> #include<sys/ipc.h>
#include<sys/shm.h>
intshmctl(intshmid,intcmd,structshmid_ds*buf);
Functionreturns0ifitsucceedsor-1ifit
fails.
SharedmemoryExample
//shmry1.c
#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<sys/shm.h>
#defineTEXT_SZ2048
struct shared_use_st
{
intwritten_by_you;
charsome_text[TEXT_SZ];
};
int main()
{
intrunning=1;
void *shared_memory = (void
*)0; struct shared_use_st
*shared_stuff; int shmid;
srand((unsignedint)getpid(
)); shmid =
shmget( (key_t)1234,
sizeof(struct
shared_use_st), 0666 |
IPC_CREAT );
if(shmid==-1)
{
fprintf(stderr,"shmget failed\n");
222
J.B.INSTITUTEOFENGINEERING
ANDTECHNOLOGY
exit(EXIT_FAILURE);
}
shared_memory=shmat(shmid,(void*)0,0); if
(shared_memory == (void *)-1)
{
fprintf(stderr,"shmatfailed\n");
exit(EXIT_FAILURE);
}
printf("MemoryAttachedat
%x\n", (int)shared_memory);
shared_stuff=(structshared_use_st
*)shared_memory;shared_stuff-
>written_by_you=0;while(running)
{
if(shared_stuff->written_by_you)
{
sleep(rand()%4);shared_stuff-
>written_by_you=0;
if(strncmp(shared_stuff->some_text,
"end", 3)== 0)
{
running= 0;
}
}
}
if(shmdt(shared_memory)==-1)
{
fprintf(stderr,"shmdtfailed\n");
exit(EXIT_FAILURE);
}
if(shmctl(shmid,IPC_RMID,0)==-1)
{
fprintf(stderr,"failedtodelete\n");
223
J.B.INSTITUTEOFENGINEERINGAND
TECHNOLOGY
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
//shmry2.c
#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<sys/shm.h>
#defineTEXT_SZ2048 struct
shared_use_st
{
intwritten_by_you;
charsome_text[TEXT_SZ];
};
int main()
{
intrunning=1
void *shared_memory = (void *)0;
struct shared_use_st *shared_stuff;
char buffer[BUFSIZ];
intshmid;
shmid=shmget((key_t)1234, sizeof(struct
shared_use_st),
0666|IPC_CREAT);
if(shmid==-1)
{
fprintf(stderr,"shmgetfailed\n");
exit(EXIT_FAILURE);
}
shared_memory=shmat(shmid,
(void *)0, 0);
if(shared_memory==(void*)-1)
{
fprintf(stderr,"shmatfailed\n");
exit(EXIT_FAILURE);
224
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
The shmry1.c program will create the segment using shmget() function and returns the
identifiershmid.Thenthatsegment isattachedtoitsaddressspaceusing shmat()function.
5.2Sockets
225
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
A socket is a bidirectional communication device that can be used to communicate withanother process on
the same machine or with a process running on other machines.Sockets are the only interprocess
communication we’ll discuss in this chapter thatpermit communication between processes on different
computers. Internet programs such as Telnet, rlogin, FTP, talk, and the World Wide Web use sockets.
For example, you can obtain the WWW page from a Web server using theTelnet program because they
both use sockets for network communications.To open a connection to a WWW server at
www.codesourcery.com, use telnet www.codesourcery.com 80.The magic constant 80 specifies a
connection to the Web server programming running www.codesourcery.com instead ofsome other
process.Trytyping GET / after the connection is established.This sends a message through the socket to the
Web server, which replies by sending the home page’s HTML source and then closing the connection—for
example:
%telnetwww.codesourcery.com 80
Trying 206.168.99.1...
Connectedtomerlin.codesourcery.com(206.168.99.1). Escape
character is ‘^]’.
GET/
<html>
<head>
<metahttp-equiv=”Content-Type”content=”text/html;charset=iso-8859-1”>
...
3. Notethat only WindowsNT cancreate a named pipe;Windows9x programs can formonly client
connections.
4. Usually,you’duse telnettoconnecta Telnetserverforremote logins.Butyoucan also use telnet to
connect to a server ofa different kind and then type comments directly at it.
IntroductiontoBerkeleysockets
Berkeley sockets (or BSD sockets) is a computing library with an application programminginterface (API) for
internet sockets and Unix domain sockets, used for inter-processcommunication (IPC).
ThislistisasummaryoffunctionsormethodsprovidedbytheBerkeley sockets API library:
socket()createsanewsocketofacertainsockettype,identifiedbyanintegernumber, and allocates system
resources to it.
bind()istypicallyusedontheserverside,andassociatesasocketwithasocket address structure, i.e. a
specified local port number and IP address.
listen()isusedontheserverside, andcausesaboundTCPsockettoenterlisteningstate.
connect() is used on the client side, and assigns a free local port number to a socket.In case of a TCP
socket, it causes an attempt to establish a new TCP connection.
226
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
accept()isused onthe server side. It acceptsa received incoming attempt to create anew TCP connection
from the remote client, and creates a new socket associated with the socket address pair of this
connection.
send()andrecv(),orwrite()andread(),orsendto()andrecvfrom(),areusedforsending and receiving data
to/from a remote socket.
close() causesthesystemto releaseresourcesallocatedtoasocket.IncaseofTCP,the connection is
terminated.
gethostbyname()andgethostbyaddr()areusedtoresolvehostnamesandaddresses. IPv4 only.
select()isusedtopend,waitingforoneormoreofaprovidedlistofsocketstobe ready to read, ready to write, or
that have errors.
poll()is used tocheck on the state of a socketin a set of sockets. The setcan be tested to see if any socket
can be written to, read from or if an error occurred.
getsockopt() is used toretrievethe currentvalueof a particular socketoption for the specified socket.
setsockopt()isusedtosetaparticularsocketoptionforthespecified socket.
IPCoveranetwork
Socket Concepts
Whenyoucreateasocket, youmustspecifythree parameters:
communicationstyle,
namespace,
protocol.
Acommunicationstylecontrolshowthesocket treatstransmitted dataand specifies
thenumberofcommunicationpartners.Whendataissent throughasocket,it isackaged into
chunks called packets.The communication style determines how these
packetsarehandled and howtheyareaddressedfromthesendertothe receiver.
Connection styles guarantee delivery of all packets in the order they were sent. If
packetsarelostorreorderedbyproblems inthe network,thereceiverautomatically
requests their retransmission from the sender.
Aconnection-stylesocket islikeatelephonecall:Theaddressesofthesender and
receiver are fixed at the beginning of the communication when the connection
is established.
Datagram stylesdo notguaranteedeliveryorarrivalorder.Packetsmaybe lost or
reordered in transit due to network errors or other conditions. Each packet must
be labeled with its destination and is not guaranteed to be delivered.The system
guarantees only “best effort,” so packets may disappear or arrive in a different
order than shipping.
Adatagram-stylesocketbehavesmorelikepostalmail.Thesenderspecifies the
receiver’s address for each individual message.
Asocket namespace specifies how socket addresses are written. Asocket address identifies one
endofasocket connection.Forexample,socket addressesinthe “localnamespace”areordinary
227
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
filenames. In “Internet namespace,” a socket address is composed of the Internet address (also known as
an Internet Protocol address or IP address) of a host attached to the network and aport number.The port
number distinguishes among multiple sockets on the same host.
A protocol specifies how data is transmitted. Some protocols are TCP/IP, the primary
networkingprotocolsused bytheInternet;theAppleTalknetworkprotocol;andtheUNIXlocal communication
protocol. Not all combinations of styles, namespaces,and protocols are supported.
Client-serverdatagramsocket— example
Toexperiment withdatagramsocketsintheUNIXdomainwewillwritea client/server application
where:
theclienttakesanumberofargumentsonitscommandlineandsendthemtothe server using separate
datagrams
for each datagram received, the server converts it touppercase and senditback tothe client
theclientprintsserverrepliestostandardoutput
Forthisto workwewillneed tobindallinvolved socketstopathnames.
Client-serverdatagramsocketexample—protocol
#include <ctype .h> #include
<sys/un.h>
#include<sys/socket.h>
#include <unistd .h> #include
" helpers .h"
#define SRV_SOCK_PATH " /tmp/uc_srv_socket "
#defineCLI_SOCK_PATH"/tmp/uc_cli_socket.%ld " #define
MSG_LEN 10
#include"uc proto.h"
int main( int argc , char *argv [ ] )
{ structsockaddr_unsrv_addr,cli_addr; int
srv_fd , i ;
ssize_t bytes ;
socklen_tlen;
charbuf[MSG_LEN];
if( (srv_fd=socket(AF_UNIX,SOCK_DGRAM,0)) <
0)err_sys("socket error");
memset(&srv_addr,0,sizeof(structsockaddr_un)); srv_addr .
sun_family = AF_UNIX ;
strncpy(srv_addr.sun_path,SRV_SOCK_PATH, sizeof
( srv_addr . sun_path ) 1) ;
if(access(srv_addr.sun_path,F_OK)== 0) unlink ( srv_addr .
sun_path ) ;
if(bind(srv_fd,(structsockaddr*)&srv_addr, sizeof ( struct
sockaddr_un ) ) < 0)
err_sys("bind error");
228
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
for(;;){
len= sizeof(structsockaddr_un);
if((bytes=recvfrom(srv_fd,buf,MSG_LEN, 0, ( struct
sockaddr * ) &cl i_addr , &len ) ) < 1) err_sys ( " recvfrom
error " ) ;
printf( "server received%ldbytes from%s\n"
,(long)bytes,cli_addr.sun_path); for ( i = 0; i <
bytes ; i ++)
buf[i]=toupper((unsignedchar)buf[i])
;if(sendto (srv_fd ,buf,bytes,0,
( structsockaddr *)&cli_addr ,len) !=bytes
)err_sys("sendto error");
}
}
#include"uc proto.h"
int main( int argc , char *argv [ ] )
{ structsockaddr_unsrv_addr,cli_addr; int
srv_fd , i ;
size_t len ;
ssize_tbytes;
charresp [MSG_LEN]
;if(argc<2)
err_quit("Usage : uc c lientMSG...");
if( (srv_fd=socket(AF_UNIX,SOCK_DGRAM,0)) <
0) err_sys("socket error");
memset(&cli_addr,0,sizeof(structsockaddr_un)); cl i_addr .
sun_family = AF_UNIX ;
snprintf( cli_addr .sun_path, sizeof( cli_addr .sun_path)
,CLI_SOCK_PATH,(long)getpid( ));
if(bind(srv_fd,(structsockaddr*)&cli_addr, sizeof ( struct
sockaddr_un ) ) == 1)
err_sys("bind error");
Notes:
theserverispersistentandprocessesonedatagramatatime,nomattertheclientrocess,
i.e.thereisnonotionofconnectionmessageslargerthan10bytesaresilentlytruncated
229
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
charsun_path[108];/*socketpathname, NULL
terminated */
}
The fieldsun_pathcontainsaregularpathname,pointingto aspecialfileoftypesocket(.pipe) which will be
created at bind time.
Duringcommunicationthe filewillhaveno content,it isusedonlyasa rendez-vouspoint between
processes.
Internet-DomainSockets
UNIX-domain sockets can be used only for communication between two processes on the same computer.
Internet-domain sockets, on the other hand, may be used to connect processes on different machines
connected by a network.
Sockets connecting processes through the Internet use the Internet namespace represented by
PF_INET.The most common protocols are TCP/IP.The Internet Protocol (IP), a low-level protocol, moves
packets through the Internet, splitting and rejoining the packets, if necessary. It guarantees only “best-
effort” delivery, so packets may vanish or be reordered during transport. Every participating computer is
specified using a unique IP number.The Transmission Control Protocol (TCP), layered on top of IP, provides
reliable connection-ordered transport. It permits telephone-like connections to be established between
computers and ensures that data isdelivered reliably and inorder.
DNSNames
Because it is easier to remember names than numbers, the Domain Name Service (DNS) associates names
such as www.codesourcery.com with computers’ unique IP numbers. DNS is implemented by a worldwide
hierarchy of name servers, but you don’t need to understand DNS protocols to use Internet host names in
your programs.
Internet socket addresses contain two parts: a machine and a port number.This information is stored in a
struct sockaddr_in variable. Set the sin_family field to AF_INET to indicate that this is an Internet
namespace address.The sin_addr field stores the Internet address of the desired machine as a 32-bit
integer IP number.A port number distinguishes a given machine’s different sockets. Because different
machines store multibyte values in different byte orders, use htons to convert the port number to
network byte order. See the man page for ip for more information.To convert human-readable hostnames,
either numbers in standard dot notation (such as 10.0.0.1) or DNS names (such as www.codesourcery.com)
into 32-bit IP numbers, you can use gethostbyname.This returns a pointer to the struct hostent structure;
the h_addr field contains the host’s IP number.
SystemCalls
Socketsaremoreflexiblethanpreviouslydiscussedcommunication techniques.These are
the system calls involving sockets:
230
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
listen—Configuresasockettoacceptconditionsaccept—
Acceptsaconnectionandcreatesanewsocket forthe connection Sockets
are represented by file descriptors.
CreatingandDestroying Sockets
SocketsareIPCobjectsthatallowtoexchangedatabetweenprocessesrunning: either on
the same machine (host), or on different ones over a network.
The UNIX socket API first appeared in 1983 with BSD 4.2. It has been finally standardized for the first time in
POSIX.1g (2000), but has been ubiquitous to everyUNIX implementation since the 80s.
The socket API is best discussed in a network programming course,which this one isnot. Wewill onlyaddress
enoughgeneralsocketconceptsto describe how to use a specific socket family: UNIXdomain sockets.
ConnectionOriented Protocol
231
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
ConnectionlessProtocol
Client-serversetup
Let’sconsideratypicalclient-serverapplicationscenario —no matteriftheyare locatedon the same or
different hosts.
Socketsareusedasfollows:each
application: create a socket
idea:communicationbetweenthetwoapplications will flowthroughanimaginary“pipe”that
willconnectthetwosocketstogether
server: binditssockettoawell-knownaddress
wehavedonethesametosetuprendez-vouspoints forother IPCobjects.
e.g.FIFOs
client:locateserversocket(via itswell-knownaddress)and “initiatecommunication”1withthe server.
Socketoptions:
232
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
TomanipulateoptionsatthesocketsAPIlevel: SOL_SOCKET
To manipulate options at a protocol level, that protocol number should be used;
forexample, forUDPit isIPPROTO_UDPorSOL_UDP(bothareequal17);see include/linux/in.h
and include/linux/socket.h
● SOL_IPis0.
● Therearecurrently19Linuxsocketoptionsandoneanotheronoptionfor BSD
compatibility.
● ThereisanoptioncalledIP_PKTINFO.
233
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
fcntlsystem calls
Thefcntlsystemcallprovides furtherwaystomanipulatelowlevelfiledescriptors.
Itcanperformmiscellaneousoperationsonopenfiledescriptors. The
call,
234
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
5.9ComparisionofIPCmechanisms.
IPCmechanismsaremianly5types
1. pipes:it isrelateddataonlysend fromonepipe outputisgivingto anotherpipe inputto share resouses pipe are used drawback:itis only related process only communicated
2. messagequeues:messagequeuesareunrelatedprocessarealso communicatewith message queues.
3. sockets:socketsalsoipcitiscomunicateclientsandserver
withsocketsystemcallsconnectionorientedandconnectionlessalso
4. PIPE:Onlytwo related(eg:parent&child)processesscanbe communicated.Data reading would be first in first out manner.
NamedPIPEorFIFO:Onlytwoprocesses(canbe relatedorunrelated)cancommunicate. Data read from FIFO is first in first out manner.
5. MessageQueues: Anynumberofprocessescanread/writefrom/tothequeue. Datacan be read selectively. (need not be in FIFO manner)
6. Shared Memory: Part of process's memory is shared to other processes. other processes can read or write into this shared memory area based on the permissions. Accessing
Shared memory is faster than any other IPC mechanism as this does not involve any kernel levelswitching(Shared memory resides on user memory area).
7. Semaphore:Semaphoresareusedforprocesssynchronisation.Thiscan'tbeusedforbulk data transfer between processes.
Introduction to Linux
OperatingSystem
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
Tableofcontents
LinuxProgrammingNotes
• Operatingsystemtasks
• UNIXhistory,Linuxhistory
• Linuxdistributions
• Linuxbasicfeatures
• BuildingOSkernels
• Linuxkernelmodules
• eBPF
• Kernelreports–whatisgoingoninthekernel
• Linuxstructureandkernelfunctions
• Basicconcepts
2
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes Computersystemlayers(source:Stallings,OperatingSystems)
OperatingSystemisaprogramthatmediatesbetweentheuserandthecomputerhardware.
• Hideshardwaredetailsofthecomputersystembycreatingabstractions(virtualmachines).
• Managesresources:memory,processor(CPU),input/output,communicationports
• Otheractivities:security,jobaccounting,errordetectingtools,etc.
3
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
UNIXhistory
LinuxProgrammingNotes
• 1974:presentedonACMSymposiumonOperatingSystemsandinCACM,quicklygainingpopularity.
• Forhobbyists:Unixhistory,Unix,Linux,andvarianthistory.
• TheearlydaysofUnixatBellLabs,BrianKernighan(LCA2022online).
• KenThompsoninterviewedbyBrianKernighanatVCFEast2019.
4
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
5
UnixHistoryDiagram-shortversion(source:Wikipedia)
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
6
Linuxhistory
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY Linus Torvaldsin 2024
inconversation
LinuxProgrammingNotes
LinusTorvalds, Finland, born in withDirkHohndel
thesameyearasUNIX,i.e.1969, creator of the Linux kernel atOSSVienna
and the Git version control sysem. Linus Torvalds CurrentlyhiredbytheLinuxFoundation
announcingLinux 1.0,
30.03.1994
Richard Stallman, founder of the GNU project and theFree SoftwareFoundation,co- Richard Stallmanin 2019
creatoroftheGNUGPLlicense,creator of the Emacs editor, GCC compiler, GDB debugger.
– 2025-02-16,latestmainline6.14-rc3
Numberingofthekernelversions–seeWikipedia AndrewTanenbaumin27023
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
Linuxstatisticsandfacts
LinuxProgrammingNotes
• 100%oftheworld’stop500supercomputersrunonLinux.(Blackdown)
• Linuxpowers53.1%ofwebsiteswhoseoperatingsystemisknown.(W3Techs)
• 96.3%oftheworld’stoponemillionwebservers runonLinux.(ZDNet)
• 90%ofallcloudinfrastructureoperatesonLinux,andpracticallyallthebestcloudhostsuse it.
• 90%ofHollywood’sspecialeffectsaremadeonLinux.(Wertsa)
• 47%ofprofessionaldevelopersuseLinux-basedoperatingsystems.(Statista)
• InJuly2022,2.76%ofalldesktopoperatingsystemsworldwideranonLinux.
• InJune2024,Linuxheldamarketshareof4.03%oftheglobaldesktopmarket.(ZDNet)
• Linux,thethirdmostpopulardesktopOS,hasamarketshareof2.09%.(Statista)
• Linuxpowers85%ofsmartphones.(HaydenJames)
https://truelist.co/blog/linux-statistics/
8
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
JonathanCorbetinOct.2024Kerne lReport:
LinuxProgrammingNotes
Linuxkernelversions(source:Wikipedia) 9
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
Linuxdistributions
LinuxProgrammingNotes
A set of ready-to-install, precompiled packages; tools for package installation and uninstallation
(RPM:RedHatPackageManager);kernel,butalsomanyserviceprograms;toolsforfilesystems management, creation and maintenance of user accounts, network management etc.
DistroWatchisawebsitewhichprovidesnews,popularity rankings, and other general information about Linux distributions as well as other free software/open source Unix-like
operating systems.
Debianused inlabsinMIMUW
2025-02-23(10topoutof270)
10
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
Linuxbasic features
LinuxProgrammingNotes
• Multi-accesssystem(withtimesharing)andmulti-tasking.
• Multiprocesssystem,simplemechanismstocreatehierarchyofprocesses,kernel preemption.
• Availableformanyarchitectures.
• Simplestandarduserinterfacethatcanbeeasilyreplaced(shellcommand interpreter).
• Hierarchicalfilesystems.
• Filesareseenasstringsofbytes(easytowritefilters).
• Loadingprogramsondemand(forkwithcopyonwrite).
• Virtualmemorywithpaging.
• Dynamicharddiskcache.
• Sharedlibraries,loadedintomemorydynamically(onecodeusedsimultaneouslyby
manyprocesses).
• CompliancewiththePOSIX1003.1standard.
• Differentformatsofexecutablefiles.
11
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
BuildingOS kernels
LinuxProgrammingNotes
• Monolithickernel(theonlysolutionuntilthe1980s)–Linuxbelongstothiscategory.
– thewholekernelrunsinasingleaddressspace,
– communicationviadirectfunctioninvocation.
• Microkernel(e.g.Mach,MINIX).
– functionalityofthekernelisbrokendownintoseparateprocesses(servers),
– someserversruninkernelmode,butsomeinusermode–allservershaveownaddressspaces,
– communicationishandledviamessagepassing,
– contextswitchandcommunicationgenerateextraoverheadsocurrentlyusermodeserversare
rarelyused.
• Macrokernelor„Hybridkernel"(e.g.theMicrosoftWindowsNTkernelthatpowersalloperating systems in the Windows NT family, up to and including Windows11 and Windows
Server 2022, and powers Windows Phone 8, Windows Phone 8.1, Windows 10 Mobile, and the Xbox One and Xbox Series consoles).
12
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
LinusTorvalds:
Structureofmonolithickernel,microkernelandhybridkernel-basedoperatingsystems(source:Wikipedia)
“Astothewhole‘hybridkernel’thing-it’sjustmarketing.It’s‘oh,thosemicrokernelshadgoodPR,howcan wetry to get goodPR for our working kernel? Oh, I know, let’s use a cool
name and try to imply that it has all the PR advantagesthat that other system has’.”
Readings
1. Tanenbaum–Torvaldsdebateonkernelarchitecture(MINIXvsLinux)
• Wikipedia
• O’Reilly
2. ComparingLinuxandMinix,February5,2007,JonathanCorbet
13
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
Linuxkernelmodules
LinuxProgrammingNotes
• Linuxborrowsmuchofthegoodfrommicrokernels:modulardesign,capabilitytopreemptitself, support for kernel threads, capability to dynamically load separate binaries
(kernel modules).
• Modules–separatelycompiled,loadedintomemoryondemandanddeletedwhentheyarenolonger needed.
• Examples:adevicedriver,afilesystem,anexecutablefileformat.
• Advantages: saving memory (occupies memory only when it is needed), the error in the module does not cat/proc/modules
suspendthesystem,butonlyremovesthemodulefrom thememory,onecanuseconflictingdriverswithoutthe
need to restart the system, etc.
• Disadvantages???
• nameofthemodule
• memorysizeofthemodule,inbytes
• howmanyinstancesofthemodulearecurrently loaded
• ifthemoduledependsuponanothermodule(s)
14
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
But–eBPFmakesachange...
LinuxProgrammingNotes
16
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
ExtendedBPF:ANewType of
Software,Brendan Gregg at Ubuntu
Masters Conf 2019
(presentation,slides)
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
http://brendangregg.com 17
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
2018,newCode of Conduct
LinuxProgrammingNotes
LinuxDevelopment LinuxDevelopmen
WhatisBPF?
Highlyefficientsandboxedvirtual machine in
the Linux kernel making the Linux kernel
programmable at native execution speed.
Programmability allows to
continuouslyadapttochanging requirements
and innovate quickly.
HowtoMakeLinuxMicroservice-AwarewithCiliumandeBPF,ThomasGraf,QCon2018, (presentation, transcript)
18
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
eBPF–RethinkingtheLinux
Kernel,ThomasGraf,QCon 2020
(presentation,transcript)
19
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
Accessedviafiledescriptors 2
eBPF–RethinkingtheLinux
Kernel,ThomasGraf,QCon 2020
(presentation,transcript)
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
eBPF–
summary
LinuxProgrammingNotes
• eBPFisaprogramminglanguageandruntimetoextendoperatingsystems.
• eBPFislikeJavaScriptbutforkerneldevelopers.
AlexeiStarovoitov
• In-kerneljust-in-timecompiler.
• eBPFfeatures
– Secure(runtimeverification,sandboxconcept,programsigning),
– Efficient(JITcompiler,embeddedinOS,per-CPUdatastructures), TheBeginner’sGuidetoeBPF,LizaRice(live
programming+sourcecode) WhatiseBPF?–
– Portable(genericbytecode,datatypediscovery,stableAPItoOS). eBPF portal
• Manyplacestoattachprograms:packetfilters,tracepoints, BPFatFacebook,PerformanceSummit2019,Alexei
Starovoitov
securitypolicies,...
atFacebook,(slides)KernelRecipes2019,Alexei Starovoitov
• Enabletheadditionofnewfunctionality–nokernelhacking required. AthoroughintroductiontoeBPF(fourarticlesin lwn.net), Matt Fleming, December 2017.
compilercollection(BCC-ToolsforBPF-basedLinux IO analysis, networking,
• Highlyflexiblekernelconfiguration. monitoring, and more )
• eBPFiscontrolledbytheeBPFFoundation(Meta,Google, Isovalent, Microsoft, RedHat, Netflix, …)
21
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
eBPF–officialdocumentary
LinuxProgrammingNotes
https://www.youtube.com/watch?v=Wb_vD3XZYOA(30min)
In 2014,a group of engineers at Plumgrid needed to find an innovative and cost-effective solution to
handlenetworktrafficinSDNenvironments.Whattheycreatedwasalandmarkintheindustryknown as the extended Berkeley Packet Filter (or eBPF). This vital technology allows user-level
code execution inside the Linux Kernel, transforming network traffic handling for SDN environments.
Whethertheseengineersknewitornot,theyhadjustrevolutionizedtheLinuxKernel.
– GrowthofLinuxandSDN
– PLUMgrid ThomasGraf
DanielBorkmann Chris
– InitialPatchSubmission Wright
– eBPFMergedintotheLinuxKernel Liz Rice
PurviDesai(Google)
– HyperscalersAdopt eBPF
DavidMiller(networkkernelmaintainer) Alexei Starovoitov
– CiliumBringeBPFtoEndUsers BrendanGregg
– DockerCon2017eBPFTakesOff DaveThaler(Microsoft)
– eBPFExpandstoSecurity
– eBPFonWindows
– eBPFEverywhere
https://ebpfdocumentary.com/
22
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
Whatisgoingoninthekernel–kernelreports
LinuxProgrammingNotes
This talk will review recent events in the kernel development community, discussthecurrentstateofthekern
• TheKernelReport,JonathanCorbet,OpenSourceSummit2024Japan(October2024,21min)
• TheKernelReport,JonathanCorbet,OpenSourceSummit2024NorthAmerica(April2024,42 min)
• TheKernelReport,JonathanCorbet,OpenSourceSummit2023
• TheKernelReport,JonathanCorbet,OpenSourceSummit2022
• TheKernelReport,JonathanCorbet,LinuxPlumbersConference2021(startingfrom6:45)
• TheKernelReport,JonathanCorbet,LPC2020,2020edition.
• TheKernelReport,JonathanCorbet,linux.conf.au2019edition.
• LinuxWeeklyNews
– Kernelindex
JonathanCorbet,Executive Editor, LWN.net & Kernel Documentatio
– Conferenceindex
23
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
TheKernelReport2024–whattoexpectin2025?
LinuxProgrammingNotes
• Rust(CommitingtoRustinthekernel)
– Memory-safesystemsprogramming,eliminateswhole classesofbugs.
– FirstmergedinOctober2022for6.1(asanexperiment),slowdevelopment.
– ConclusionfromtheKernelMaintainersSummitin2024:Rustinthekernelisviable.
– Rustconcerns–noteasytolearn,languagestability,Rust/CAPIcorrespondence,getting abstractions upstream – if they are not used
yet.
– Whatmightbemerged(lotsifinfrastructure,Nova–NVIDIAGPUdriver,AppleGPU
driver).
• CPUscheduler creativity
– FordecadestherecouldbeonlyoneCPUscheduler.
– Sched_ext–anewschedulingclass,basedonBPF,mergedinNovember2024for6.12.
• AnybodycanwriteaCPUscheduler.
• Quickandsafeiteration.
• Focusononeusecase.
• Security
– TheproblemswithCVE(CommonVulnerabilitiesandExposure)numbers
• ManyvulnerabilitiesnevergetCVEs.
• ThekernelisnowaCNA–CertficateNumberingAuthority.
2
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
TheKernelReport2024–whattoexpectin2025?
LinuxProgrammingNotes
• Bettertools
– Thekernelcommunityhasbeenslowtoadoptnewtools.
– Patchmanagement(b4continuestoimprove).
– Bugtracking(bugzilla,bugspray–beingdeveloped,regressiontracking–currentlyunfunded).
– Testing(kernelselftestsgrowing,KernelCI,stableupdatetesting).
• Darkareas inthekernel
– Documentation.
– Buildsystem. Summary
– Manycore-kernel areas.
– Driversforolderhardware.
– Maintainers(lacksupport,burned-out,frustrated,…)
25
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
TheKernelReport2024
LinuxProgrammingNotes
• BPF
– BPFTokens(fine-grainedcapabilitiesforloadingBPFprograms)–moresecurity.
– BPFArena–mechanismforsharingmemorybetweenBPFprogramsrunninginthekernelanduser
spaceprograms.
– Fuse-BPF.
– BPFnetworkdevice. Notimefor
– Challenges(complexity,resistancelikesched_ext).
• Confidentialcomputing
– Securityinahostileenvironment.
– Howtore-establishtrust?
– Asecure,verifiedbootchain.
– CPUattestation.
– Encryptedmemory.
– Vendorsworkingonsupportingthisidea(AMDSEV+SNP,IntelTDX,ARMCCA).
Slides:https://lwn.net/talks/2024/kr-ossna.pdf
26
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
2024KernelMaintainersSummitgroupphoto
LinuxProgrammingNotes
27
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
TheKernelReport2023
LinuxProgrammingNotes
• Rust
– Hasalottooffer(astrongertypesystem,noundefinedbehavior,attractivetonewer developers).
– Whynot Rust in the kernel (a new language adds complexity, the language is still evolving –
quickly,maintainerswillneedtolearnRust,adifficultlearningcurve,lotsofgluecode,some things are hard to do in Rust, conservatism).
– InitialRustinfrastructurehasbeenmergeintoLinux6.1(October2022).
– Moresupportcodeinsubsequentkernels(accesstoexistingtypesandfunctions…butsafer).
– ApairodRustkernelmodules(NVMExpressdriver,9Pfilesystemserver)
– Nothinginaproductionkernelyet,nothingthatanybodyisactuallyusing.
– Rustsupportwasmergedasanexperiment.
– TheRustdecisionpointiscomingsoon.
Rust-for-LinuxdeveloperMiguelOjeda
28
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
TheKernelReport2023
LinuxProgrammingNotes
• Themaintainershipcrisis
– Increasingdemands.
– Understaffing.
– Lackofemployersupport(manymaintainers arenotpaidtomaintain).
– Kernelfuzzers(badqualitybugreports).
– Maintainers.
• Anunparalleleddepthofskillsand experience.
• Butalsoresistancetochange(e.g.Rust),lackofdiversity,increasinglytired single points of failure.
• Preparingforchange(sharedmaintenanceduties,documentation, investment in tools).
– Linux Kernel Contribution Maturity Model https://www.kernel.org/doc/html/latest/process/contribution-maturity-model.html
Slides:https://lwn.net/talks/2023/kr-osseu.pdf
29
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
2023KernelMaintainersSummitgroupphoto
LinuxProgrammingNotes
30
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
TheKernelReport2022
LinuxProgrammingNotes
• Io_uring
– Systemcallsslowdownyourprogram.
– Sharedmemoryarea(user,kernel).
– Whatit brings
• Asynchronousoperations.
• Submission/resultswithoutsystemcalls.
• Registeredfilesandbuffers.
• Awide rangeof commands.
• Chained operations.
When the kernel encounters a chain, it will only initiate the first operation; the next
operationinthechainwillonlyrunafterthe first completes.
– User-spaceblockdriverusingio_uring(ublk)
io_uringisanalternative,high-performanceAPIthatruns
withinthe kernel
– Isio_uring thebasisforfuturemicrokernelarchitecture?
31
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
tions Basic concepts
LinuxProgrammingNotes
• Linuxstructureandkernelfunctions
• Basicconcepts–process,usermodeandkernelmode,contextswitch,systemcalls, user stack and kernel stack, process state transitions
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
Linux–thestructureandfunctionsofthekernel
LinuxProgrammingNotes
33
Source:Wikipedia
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
Process,addressspace,context
LinuxProgrammingNotes
• Processisaprograminexecution;executionrunssequentially,accordingtotheorderof instructions in a process address space.
• Processaddressspaceisacollectionofmemoryaddresses,referencedbytheprocessduring execution.
• Processcontextisitsoperationalenvironment.Itincludescontentsofgeneralandcontrol registers of the processor, in particular:
– programcounter(PC),
– stackpointer(SP),
– processorstatusword(PSW),
– memorymanagementregisters(allowaccesstocodeanddataofa process).
• Linuxisamultiprogrammingsystem.Thekerneldynamicallyallocatesresourcesnecessaryfor processes to operate and provides security.
Forthispurpose,itneedshardwaresupport:
– processorexecutingintwomodes:usermodeandsystemmode(kernelmode),
– privilegedinstructionsandmemoryprotection,
– interruptsandexceptions.
34
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
Kerneladdressspace
LinuxProgrammingNotes
Systemaddress space or kernel space comprises code and kerneldatastructures. Access to them is only possible in system mode. The kernel has direct access to the address
space of the currentprocess.Occasionally,itcanreachuptoaddressspaceoftheotherprocessthanthecurrentone.
Kernelthreadisexecutedinkernelmode.
Thetransitiontotheexecutionofthekernelcodecanoccurasaresultofseveralevents:
– Theprocesscallsthesystemfunction(systemcall).Theuserprocessinstructsthekernelto perform certain actions (e.g. I/O operations) on its behalf.
– Theprocessorreportsexceptionwhileexecutingtheprocess,e.g.anon-existentinstruction.The kernel handles an exception on behalf of the process that caused it.
– An external device reports an interrupt to the CPU informing about the occurrence of an asynchronousevent,e.g.completionofaninput-
outputoperation.Interruptsupportishandled in the interrupt handling routine.
35
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
Contextswitching
LinuxProgrammingNotes
36
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
Transitionsbetweenuserandkernelmode,source:Bovet,Cesati
Interleavingofkernelcontrolpaths,source:Bovet, 37
Cesati
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
Systemfunctioncallwithint0x80
LinuxProgrammingNotes
The details of the system function call depend on the architecture (the figure illustrates i386). The registereax
isusedtotransmitthenumberofthefunctionbeingcalled.Themachineinstructionint 0x80 calls the program interrupt 0x80 (decimal 128) –context switching and calling the kernel
functionsystem_call.The function transfers control to the proper system function (uses system_call_table with eax treated as an index).
Afterreturningfromthesystemfunction,thesyscall_exitfunctionisexecuted,theresume_userspace
38
functioncallreturnsthecontrolbacktotheuserspace.
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
Systemcallandprocessstacks
LinuxProgrammingNotes
Eachprocessusestwostacks:
– userstack–usedinusermode(growsdynamicallyduringprogramexecution),
– kernelstack–inkernelmode(hasafixed,small size);isusuallyallocatedinaddressspaceofthe process, but it can not be accessed in the user mode.
39
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
Systemcall–sequenceofsteps
LinuxProgrammingNotes
System calls: https://linux-kernel-labs.github.io/refs/heads/master/lectures/syscalls.htmlThis is what happens during a system call:
1. Theapplicationissettingupthesystemcallnumberandparametersanditissuesatrap
instruction.
2. Theexecutionmodeswitchesfromusertokernel;theCPUswitchestoakernelstack;theuser stack and the return address to user space is saved on the kernel stack.
3. Thekernelentrypointsavesregistersonthekernelstack.
4. Thesystemcalldispatcheridentifiesthesystemcallfunctionandrunsit.
5. Theuserspaceregistersarerestoredandexecutionisswitchedbacktouser(e.g.calling IRET).
6. Theuserspaceapplicationresumes.
Seealso:Shadowstacksforuserspace,Jonathan CorbetFebruary21,2022
Whenever a function is called, the return address is pushed onto both the regular stack and the shadow
stack.Whenthatfunctionreturns,thereturnaddressesarepoppedoffbothstacksandcompared;ifthey
failtomatch,thesystemgoesintoredalertandkillstheprocessinvolved. 40
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
Systemcallconventions
LinuxProgrammingNotes
DefinitionofthesystemfunctionfromtheClevel(fileinclude/linux/syscalls.h):
asmlinkagelongsys_exit(interror_code);
asmlinkagetellscompilertolookonthekernelstackforthefunctionparameters,insteadofregisters.
Inarchitecturex86theregisters ebx,ecx,edx,esiandediareusedtopassthefirstfiveparameters.If there are more parameters, it is throughone register that a pointer to the user's address
space is transferred, where all parameters are placed.
Thevaluepassedfromthesystemfunctionisplacedintheeaxregister.
Otherregistersareusedin64-bitarchitecture:
– x64Architecture,registers,callingconventions,addressingmodes
– syscallnumbers
Copyingdatabetweenthekernelspaceandtheuserspaceis done using copy_to_user() and copy_from_user().
Whenexecutingthesystemfunction,thekernelworksinthe contextof theprocess (thevariablecurrentpointstothe
currentprocess). 41
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
Sysenterandsysexit
LinuxProgrammingNotes
Machineinstructionssysenterand sysexitwereaddedtox86processors(newer
thanPentiumII).Theyallowafastertransition(return)tothekernelmodetoperformasystemfunctionthanusingtheintstatement.Supportforthis mechanism has been added to the Linux
kernel (SysenterBased System Call Mechanism in Linux 2.6).
Callingthex86 function
– 64-bitversion–defined inthe filearch/x86/entry/entry_64.S
– 32-bitversion–defined inthe filearch/x86/entry/entry_32.S
Contentofthesystemfunctiontable
– 64-bitversion–defined inthe filearch/x86/entry/syscalls/syscall_64.tbl
– 32-bitversion–defined inthe filearch/x86/entry/syscalls/syscall_32.tbl
Thisisthe beginning
Inotheroperatingsystems,therearemanymorefunctionsthan435inLinux5.6(32-bit). 42
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
Processandsystemcontext
LinuxProgrammingNotes
Contextofexecution–summary:
– usercodeisexecutedinusermodeandinprocesscontext,canonlyreachtheaddress space of the process,
– systemfunctionsandexceptions(e.g.dividingbyzeroorviolationofmemoryprotection) are supported in system mode, but in context of the process, they have access to the
process and system address space.
Thekernelactsonbehalfofthecurrentprocess(e.g.byexecutingasystemfunction),itcan reference the address space of the process and the process stack. It can also block the current
process if it has to wait for resources.
– interruptsarehandledinsystemmodeinthecontextofthesystemwithaccessonlytothe
systemaddressspace.
System-wide operations, such as recalculating priorities or handling an external interrupt. Notperformedonbehalfofanyparticularprocessandthereforetakeplaceinthecontextof the
system. The kernel does not reach to the address space or the stack of the current process, also it can not block.
43
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
Processstatetransitions
TheLinuxkernelispreemptableandre-entrant,itcansupportdifferentprocessesconcurrently. The process during execution changes
state.
Thebasicstatesoftheprocess are:
– new:theprocesshasbeencreated,
– ready:theprocessiswaitingforthe
processortobeallocated,
– executed(mor
eprecisely:executedin
usermodeorexecutedinsyste
mmode): process
instructions are executed,
– waiting:th
eprocessiswaitingforan
event to occur,
– fin
ished:theprocess
completed
execution.
Processstatesandstatetransitions,source:U.Vahalia,UNIXInternals:TheNewFrontiers
INTRODUCTION TO LINUXOPERATINGSYSTEM
ThirdSchoolonLHCPhysics
TaimoorKhurshid
ExpHighEnergyPhysicsGroup National Center for Physics
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
--------------------------------------------
LinuxProgrammingNotes
Asanillustrationofthewaythattheshellandthe
kernel work together, suppose a user types rm
myfile (which has the effect of removing the file
LINUXOPERATINGSYSTEM myfile). The shell searches the filestore for
LinuxoperatingsystemconsistsofKernel,ShellandFileSystem: thefilecontainingtheprogramrm,andthenrequest
Thekernel s the kernel, through system calls, to execute
KernelisheartofLinuxOS
the program rm on myfile. When the process
rm
ItmanagesresourceofLinuxOS
myfilehasfinishedrunning,theshellthenreturns
the UNIX prompt % to the user, indicating that
Itrunsyourprograms(orsetuptoexecutebinaryfiles).
Thekernelactsasanintermediary betweenthecomputer hardware and various programs/application/shell.
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
LINUXOPERATINGSYSTEM
LinuxoperatingsystemconsistsofKernel,ShellandFileSystem:
TheShell
Theshellisaprogramthatactsasaninterfacebetweenusersand kernel
Itisacommandinterpreterandalsohasprogrammingcapabilityof its own.
ShellTypes
BourneShell(sh)(FirstshellbyStephenBourne)
CShell(sh)
KornShell(ksh)
BourneAgainShell(bash)
FilenameCompletionorTABcompletion
History-Theshellkeepsalistofthecommandsyouhavetypedin
FileSystem
Linuxtreatseverythingasafileincludinghardwaredevices. Arranged as a directory hierarchy.
Thetopleveldirectoryisknownas“root(/)”.
J.B.INSTITUTEOFENGINEERINGANDTECHNOLOGY
LinuxProgrammingNotes
DIFFERENT SHELLS AVAILABLEINLINUX
Program Name
Shell Description
DIRECTORYSTRUCTURE
Allthefilesaregroupedtogetherinadirectory structure.
Thefile-systemisarrangedinahierarchical structure, like an inverted tree.
Thetopofthehierarchyistraditionallycalled
root(writtenasaslash/)
Thefullpathtothefilereport.docis
"/home/its/ug1/ee51vn/report.doc"
DIRECTORIESINLINUX
Directory:/bin
/bincontainsthebinarieswhichareneededtorunLINUX. Directory: /boot
/boothasallthefilesrequiredforbootingLINUXonsystem.
Directory:/dev
/devhasthedevicesforallthefiles.
Directory:/etc
/etccontainstheconfigurationfilesofthevarioussoftware.
Normallynoonetouchthisdirectory.
Directory:/home
/homeislikeMyDocumentsinWindows.
Thiscontainstheusernameasthesubdirectory.
Directory:/lib
/libcontainsthesharedlibrariesrequiredforthesystemfiles.
DIRECTORIESINLINUX
Directory:/lost+found
/lost+foundcontainsthefileswhicharedamagedorwhichare not linked to any directory.
Thesedamagesareduetotheincorrectshutdown.
Directory:/mnt
Thisisthedirectoryinwhichwemountthedevices andother
file systems. Directory:/opt
Heretheoptionalsoftwaresareinstalled.
Directory:/root
Thedirectoryfortheuserroot
LINUXCOMMANDS
Commandstelltheoperatingsystemtoperformsetofoperations.
Thesyntaxformofthecommandsare
Commandoptions arguments
WecandivideLinuxcommandsintofollowingcategories
FileHandling Archival
TextProcessing Network
SystemAdministration FileSystems
ProcessManagement viCommands
LINUXCOMMANDS
Primary–man(manual)pages.
man<command>
showsallinformationaboutthecommand
<command>--help
showstheavailableoptionsforthatcommand
Secondary–Booksand Internet
FILEHANDLING
mkdir:
makedirectories
Usage:mkdir[OPTION]DIRECTORY...
eg.mkdirLHC_School
ls: list directory contents Usage:ls[OPTION]...[FILE]…
eg.ls,ls-l,lsLHC_School
changes directories Usage:cd[DIRECTORY]
cd: eg.cdLHC_School
FILEHANDLING
pwd: printsthenameofcurrentworkingdirectories Usage: pwd
eg.mkdirLHC_School
ViImproved,aprogrammerstexteditor Usage: vim [OPTION] [file]...
vim: eg.vimfile1.txt
copyfilesanddirectories
Usage:cp[OPTION]...SOURCE/DEST
cp: eg.cpsample.txtsample_copy.txt cp sample_copy.txt target_dir
FILEHANDLING
mv: move(rename)files
Usage:mv[OPTION]...SOURCE/DEST
eg.mvsource.txttarget_dir mv old.txt new.txt
remove files or directories Usage:rm[OPTION]…[file]...
rm: eg.rmfile1.txt,
rm-rfsome_dir
FILEHANDLING
find: searchforfilesinadirectoryhierarchy Usage:find[OPTION] [path] [pattern]
eg.findfile1.txt
find-namefile1.txt
history:
printsrecentlyusedcommands Usage: history
PATTERN
APatternisanexpressionthatdescribesasetofstrings which is used to give a concise description of a set, without having to list all elements.
Example:
ab*cdmatchesanythingthatstartswithaband ends with cd etc.
ls*.txt–printsalltextfiles
rm*.obj–removesallobjectfiles
TEXTPROCESSING
cat: concatenatefilesandprintonthestandard output…
Usage:cat [OPTION] [FILE]… eg.catfile1.txtfile2.txt
cat-nfile1.txt
displayalineoftext
Usage:echo[OPTION][string]... eg. echo I love Pakistan
echo: echo$HOME
--------------------------------------------
TEXTPROCESSING
grep: printlinesmatchingapattern
Usage:grep[OPTION]PATTERN[FILE]...
eg.grep-iapplesample.txt
wc: printthenumberofnewlines,words,andbytes in files…
Usage:wc[OPTION]...[FILE]...
eg.wc file1.txt wc-Lfile1.txt
TEXTPROCESSING
sort: sortlinesoftextfiles
Usage:sort[OPTION]...[FILE]...
eg. sort file1.txt sort-rfile1.txt
LINUXFILEPERMISSIONS
3typesoffilepermissions–read,write,execute
10bitformatfrom'ls-l'command
N
changefileaccesspermissions
Usage:chmod[OPTION][MODE][FILE]
eg.chmod744calculate.sh
chown:
changefileownerandgroup
Usage:chown[OPTION]...OWNER[:[GROUP]]FILE...
eg.chownremomyfile.txt
su: changeuserIDorbecomesuper-user Usage: su [OPTION] [LOGIN]
eg.suremo,su
SYSTEMADMINISTRATIO
passwd:
N
updateauser’sauthenticationtokens(s) Usage:passwd [OPTION]
eg.passwd
who: showwhoisloggedon Usage: who [OPTION]
eg.who,who-b,who-q
PROCESSMANAGEMENT
ps:
reportasnapshotofthecurrentprocesses Usage:ps [OPTION]
eg.ps,ps-el
kill: tokillaprocess(usingsignalmechanism)
Usage:kill[OPTION]pid eg. kill -9 2275
ARCHIVAL
tar: toarchiveafile
Usage:tar[OPTION]DESTSOURCE
tar-cvf/home/archive.tar/home/original tar -xvf /home/archive.tar
packageandcompress(archive)files Usage: zip [OPTION] DEST SOURSE
zip: eg.ziporiginal.ziporiginal
list,testandextractcompressedfilesinaZIParchive
unzip: Usage:unzipfilename
eg.unziporiginal.zip
NETWORK
SSH: SSHclient(remoteloginprogram)
“sshisaprogramforloggingintoaremotemach
ineandforexecutingcommandsonaremotemachine”
:ssh[options][user]@hostname eg. ssh -Y guest@10.105.11.20
scp:
securecopy(remotefilecopyprogram)
“scpcopiesfilesbetweenhostsonanetwork”
Usage:scp[options][[user]@host1:file1][[user]@host2:file2]
eg.scpfile1.txtguest@10.105.11.20:~/Desktop/
FILESYSTEM
fdisk: partition manipulator eg.sudofdisk-l
mount:
mountafilesystem
Usage:mount-ttypedevicedir
eg.mount/dev/sda5/media/target
umount:
unmountfilesystems
:umount[OPTIONS]dir|device... eg.umount /media/target
FILESYSTEM
du: estimate file space usage Usage:du[OPTION]...[FILE]...
eg.du
reportfilesystemdiskspaceusage Usage:df [OPTION]... [FILE]...
df: eg.df
quota:
displaydiskusageandlimits Usage: quota [OPTION]
eg.quota-v
EDITORCOMMANDS
Examples:
aliasrm='rm-i' (changethebehaviourofrmtoconfirmdeletes)
alias ll ='ls-l| more' (createanewcommandforfriendlyfile listings)
ENVIRONMENTVARIABLE
S
Environment variables refers to global settings that control the function of theshellandotherLinuxprograms.Theyaresometimescalledglobalshell
variables
orinsimplewords
Theydefinetheuserenvironmentandarereadfrominitializationfileseach time a user logs in…
Toviewthevalueofavariable,type:
printenvVARNAME OR echo$VARNAME
Tocheckyourenvironment,type
printenv OR env
ToSetEnvironmentVariable
setenvEDITOR“vim”
Somecommonenvironmentvariables:
HOME: Yourhomedirectory(oftenbeabbreviatedas“~”)
PWD: Currentworking directory
EDITOR: User’spreferredtexteditor
ENVIRONMENTVARIABLE
S
Somecommonenvironmentvariables:
Setstheeditortobeusedbyprogramssuchasmail clients
PATH: Specifiesdirectoriestobesearchedforexecutables
SHELL: Thedefaultlogin shell
USER: Currentloggedinuser’sname
Thetypeofterminalyouarerunning(forexamplevt100,xterm, and ANSI)
Itisacolonseperatedsetofdirectorieswhere libraries should be searched for
Toreloadanyinitializationfilewithouthavingtologoutandloginagain,type
source<filename>
e.g.source~/.bashrc source ~/.tcshrc
Profilefiles:sessionstartupfilesarehiddenfiles,findthemviacommand“ls–a”….
Open source software are user friendly and are available as commercial and free licenced
software.Therefore,theseareeasytousebyanyone,anywhere.Opensourceiseasilymodifiable as its core
code is publicly accessible.
OS(OperatingSystem)
LinuxoperatingSystem
Linux is a free open-source operating system based on Unix. Linux was originally created by Linus
Torvalds with the assistance of developers from around the globe. Linux is free to download, edit and
distribute. Linux is a very powerful operating system and it is gradually becoming popular throughout
the world.
AdvantagesofLinux:
Low cost: There is no need to spend time and huge amount money to obtain licenses since Linux and
much of it's software come with the GNU General Public License. There is no need to worry about any
software's that you use in Linux.
Stability: Linux has high stability compared with other operating systems. There is no need to reboot
the Linux system to maintain performance levels. Rarely it freeze up or slow down. It has a continuous
up-times of hundreds of days or more.
Performance: Linux provides high performance on various networks. It has the ability to handle large
numbers of users simultaneously.
Networking: Linux provides a strong support for network functionality; client and serversystems can be
easily set up on any computer running Linux. It can perform tasks like network backup more faster than
other operating systems.
Flexibility: Linux is very flexible. Linux can be used for high performance server applications, desktop
applications, andembeddedsystems.Youcaninstallonly theneededcomponentsfora particular use. You
can also restrict the use of specific computers.
Compatibility: It runs all common Unix software packages and can process all common file formats.
Wider Choice: There is a large number of Linux distributions which gives you a wider choice.
Eachorganization developandsupportdifferentdistribution.Youcan picktheoneyou likebest; the core
function's are the same.
Better use ofhard disk: Linux uses its resourceswell enough evenwhen the hard disk is almost full.
Multitasking: Linux is a multitasking operating system. It can handle many things at the same time.
Security: Linux is one of the most secure operating systems. File ownership and permissions make linux more
secure.
ComponentsofLinux System
LinuxOperatingSystemhasprimarilythreecomponents
Kernel − Kernel is the core part of Linux. It is responsible for all major activities of this operating system. It
consists of various modules and it interacts directly with the underlying hardware. Kernel provides the required
abstraction to hide low level hardware details to system or application programs.
System Library − System libraries are special functions or programs using which application programs or system
utilities accesses Kernel's features. These libraries implement most of the functionalities of the operating
system and do not requires kernel module's code access rights.
SystemUtility−SystemUtilityprogramsareresponsibletodospecialized,individuallevel
tasks.
KernelModevsUserMo
de
Kernel component code executes in a special privileged mode called kernel mode with full access to all
resources of the computer. This code represents a single process, executes insingle address space and do not
require any context switch and hence is very efficient and fast. Kernel runs each processes and provides system
services to processes, provides protected access to hardware to processes.
Support code which is not required to run in kernel mode is in System Library. User programs and other system
programs works in User Mode which has no access to system hardware and kernel code. User programs/
utilities use System libraries to access Kernel functions to get system's low level tasks.
BasicFeatures
FollowingaresomeoftheimportantfeaturesofLinuxOperatingSystem.
Portable − Portability means software can works on different types of hardware in same way. Linux kernel and
application programs supports their installation on any kind of hardware platform.
Open Source − Linux source code is freely available and it is community based development project. Multiple
teams work in collaboration to enhance the capability of Linux operatingsystem and it is continuously evolving.
Multi-User − Linux is a multiuser system means multiple users can access system resources like memory/ ram/
application programs at same time.
Multiprogramming−Linuxisamultiprogrammingsystemmeansmultipleapplicationscanrun
at sametime.
Hierarchical File System − Linux provides a standard file structure in which system files/ user files are arranged.
Shell − Linux provides a special interpreter program which can be used to execute commandsof the operating
system. It can be used to do various types of operations, call application programs. etc.
Security − Linux provides user security using authentication features like password protection/ controlled
access to specific files/ encryption of data.
Architectu
re
ThefollowingillustrationshowsthearchitectureofaLinuxsystem−
ThearchitectureofaLinuxSystemconsistsofthefollowing layers
Hardwarelayer−Hardwareconsistsofallperipheraldevices(RAM/HDD/CPUetc).
Kernel−ItisthecorecomponentofOperatingSystem,interactsdirectlywithhardware,
provideslowlevelservicestoupperlayercomponents.
Shell−Aninterfacetokernel,hidingcomplexityofkernel'sfunctionsfromusers.Theshell takes
commandsfromtheuserandexecuteskernel'sfunctions.
Utilities−Utilityprogramsthatprovidetheusermostofthefunctionalitiesofanoperating systems.
KernelMode
In Kernel mode, the executing code has complete and unrestricted access to the underlying
hardware.ItcanexecuteanyCPUinstruction andreference any memoryaddress. Kernel modeis generally reserved
for the lowest-level, most trusted functions of the operating system. Crashes in kernel mode are catastrophic;
they will halt the entire PC.
User Mode
In User mode, the executing code has no ability to directly access hardware or reference memory. Code
running in user mode must delegate to system APIs to access hardware or memory. Due to the protection
afforded by this sort of isolation, crashes in user mode arealways recoverable. Most of the code running on
your computer will execute in user mode.
WindowsVs.LinuxFileSyst
em
In Microsoft Windows, files are stored in folders on different data drives like C: D: E:
But,inLinux,filesareorderedinatreestructurestartingwiththerootdirectory.
This root directory can be considered as the start of the file system, and it further branches out various other
subdirectories. The root is denoted with a forward slash '/'.
AgeneraltreefilesystemonyourUNIXmaylooklikethis.
TypesofFiles
In Linux and UNIX, everything is a file. Directories are files, files are files, and devices like Printer, mouse, keyboard
etc.are files.
Let'slookintotheFiletypesinmore detail.
GeneralFiles
GeneralFilesalsocalledasOrdinary files.Theycancontainimage,video, programor simplytext. They can be in ASCII
ora Binary format. These are the most commonly used files by Linux Users.
DirectoryFiles
These files are a warehouse for other file types. You can have a directory file within a directory(sub-
directory).You can take them as 'Folders' found in Windows operating system.
DeviceFiles:
In MS Windows, devices like Printers, CD-ROM, and hard drives are represented as drive letters
likeG:H:.InLinux,therearerepresentedasfiles.Forexample,ifthefirstSATAharddrivehad
threeprimarypartitions,theywouldbenamedandnumberedas/dev/sda1,/dev/sda2and
/dev/sda3.
Note:Alldevicefilesresideinthedirectory /dev/
All the above file types (including devices) have permissions, which allow a user to read, edit or execute (run)
them. This is a powerful Linux/Unix feature. Access restrictions can be applied for different kinds of users, by
changing permissions.
WindowsVs.Linux:Users
Thereare3typesofusersinLinux.
1. Regular
2. Administrative(root)
3. Service
RegularUser
A regular user account is created for you when you install Ubuntu on your system. All your files and folders are
stored in /home/which is your home directory. As a regular user, you do not have access to directories of other
users.
Root User
Other than your regular account another user account called root is created at the time of installation. The root
account is asuperuserwho can access restricted files, install software and has administrative privileges.
Whenever you want to install software, make changes to system files or perform any administrative task on
Linux; you need to log in as a root user.
Otherwise,forgeneraltaskslikeplayingmusicandbrowsingtheinternet,youcanuseyourregularaccount.
Serviceuser
Linux is widely used as a Server Operating System. Services such as Apache, Squid, email, etc. have their
ownindividualservice accounts.Having service accounts increases the security of your computer. Linux can allow
or deny access to various resources depending on the service.
InWindows,thereare4typesofuseraccounttypes.
1. Administrator
2. Standard
3. Child
4. Guest
WindowsVs.Linux:FileName Convention
In Windows, you cannot have 2 files with the same name in the same folder. While in Linux, you can
have 2 files with the same name in the same directory, provided they use different cases.
WindowsVs.Linux:HOMEDirectory
ForeveryuserinLinux,adirectoryiscreatedas/home/
Consider, a regular user account "Tom". He can store his personal files and directories in the
directory"/home/tom".Hecan'tsavefilesoutsidehisuserdirectoryanddoesnothaveaccessto directories of
otherusers. For instance,he cannotaccess directory "/home/jerry" of another user account"Jerry".
TheconceptissimilartoC:\DocumentsandSettingsinWindows.
WhenyouboottheLinuxoperatingsystem,youruserdirectory(fromtheaboveexample
/home/tom) is thedefault working directory. Hence the directory "/home/tom is also called the Home
directory which is a misnomer.
WindowsVs.Linux:KeyDifferences
Windows Linux
WindowsusesdifferentdatadriveslikeC:D:E Unix/Linuxusesatreelikeahierarchicalfilesystem.
to stored files and folders.
WindowshasdifferentdriveslikeC:D:E TherearenodrivesinLinux
Harddrives,CD- Peripheralslikeharddrives,CD-ROMs,printersarealso
ROMs,printersareconsidered as devices considered files in Linux/Unix
Administratoruserhasalladministrative Rootuseristhesuperuserandhasalladministrative
privileges of computers. privileges.
InWindows,youcannothave2fileswiththe Linuxfilenamingconventioniscasesensitive.Thus,sample
same name in the same folder and SAMPLE are 2 different files in Linux/Unix operating
system.
Inwindows,MyDocumentsisdefaulthome Foreveryuser/home/usernamedirectoryiscreatedwhichis
directory. called his home directory.
LinuxCommandLineTutorial:ManipulateTerminalwithCDCommands
The most frequent tasks that you perform on your PC is creating, moving or deleting Files. Let's look at various
options for File Management.
Tomanageyourfiles,youcaneitheruse
1. Terminal(CommandLineInterface-CLI)
2. Filemanager(GraphicalUserInterface-GUI)
WhylearnCommandLineInterface?
Eventhoughthe worldismovingtoGUIbased systems, CLIhasitsspecificuses andis widely used in scripting and
server administration. Let's look at it some compelling uses -
Comparatively, Commands offer more options &are flexible. Pipingand stdin/stdout are immensely powerful
are not available in GUI
Some configurations in GUI are up to 5 screens deep while in a CLIit's just a single command
Moving, renaming 1000's of the file in GUI will be time-consuming (Using Control /Shift to select multiple files),
while in CLI, using regular expressions so can do the same task with a single command.
CLI load fast and do not consume RAM compared to GUI. In crunch scenarios this matters.
Both GUI and CLI have their specific uses.For example,in GUI, performance monitoring graphsgiveinstant
visual feedbackonsystem health, while seeing hundreds of linesof logs in CLI is an eyesore.
Filesanddirectoryrelatedcommands
Command Meaning
cddirectory changetonameddirectory
cd changetohome-directory
cd~ changetohome-directory
cd.. changetoparentdirectory
pwd displaythepathofthecurrentdirector
Command Description
ls Listsallfilesanddirectoriesinthepresent working
directory
ls-R Listsfilesinsub-directoriesaswell
ls -a Listshiddenfilesaswell
catfilename Displaysthefilecontent
rm Deletesa file
man Giveshelpinformationonacommand
Givesalistofallpastcommandstypedinthe current
terminal session
history
clear Clearstheterminal
mv Renamesadirectory
pr-x Dividesthefileintoxcolumns
pr-n DenotesthefilewithLineNumbers
apt-get Commandusedtoinstallandupdatepackages
Command Meaning
lessfile displayafileapageatatime
headfile displaythefirstfewlinesofafile
tailfile displaythelastfewlinesofafile
grep'keyword'file searchafilefor keywords
wcfile countnumberoflines/words/charactersinfile
Command Meaning
command>file redirectstandardoutputtoa file
command>>file appendstandardoutputtoa file
command<file redirectstandardinputfroma file
command1|command2 pipetheoutputofcommand1totheinputof command2
sort sortdata
who listuserscurrentlyloggedin
FilePermissionsinLinux/UnixwithExample
Linux is a clone of UNIX, themulti-user operating systemwhich can be accessed by manyusers simultaneously.
Linux can also be used in mainframes and servers without any modifications. But this raises security concerns
as an unsolicited or malign usercancorrupt, change or remove crucial data. For effective security, Linux
divides authorization into 2 levels.
1. Ownership
2. Permission
Theconceptof permissions andownershipiscrucialinLinux.Here,wewilldiscussbothof them. Let us start with the
Ownership.
OwnershipofLinuxfiles
EveryfileanddirectoryonyourUnix/Linuxsystemisassigned3typesofowner,givenbelow.
User
Auseristheownerofthefile.Bydefault,thepersonwhocreatedafilebecomesitsowner. Hence, a user is also sometimes
called an owner.
Group
A user- group can contain multiple users. All users belonging to a group will have the same access permissions
to the file. Suppose you have a project where a number of people require access to a file. Instead of manually
assigning permissions to each user, you could add all users to a group, and assign group permission to file such
that only this group members and no one else can read or modify the files.
Other
Any other user who has access to a file. This person has neither created the file, nor he belongs toa
usergroupwhocouldownthefile.Practically, itmeans everybody else. Hence,whenyou set the permission for
others, it is also referred as set permissions for the world.
Now, the big question arises how does Linux distinguishbetween these three user types sothat a user 'A'
cannot affect a file which contains some other user 'B's' vital information/data. Itis like you do not wantyour
colleague, whoworks on your Linux computer, to view your images. This is where Permissions set in, and they
define user behavior.
LetusunderstandthePermissionsystemon Linux.
Permissions
EveryfileanddirectoryinyourUNIX/Linuxsystemhasfollowing 3permissionsdefinedforall the 3 owners discussed
above.
Read:Thispermissiongiveyoutheauthoritytoopenandreadafile.Readpermissionon a directory gives you the
ability to lists its content.
Write:The right permission gives you the authority to modify the contents of a file. The write permission on a
directory gives you the authority to add, remove and rename files stored in the directory. Consider a scenario
where you have to write permission on file but do not have write permission on the directory where the file is
stored. You will be able to modify the file contents. But you will not be able to rename, move or remove the file
from the directory.
Execute:In Windows, an executable program usually has an extension ".exe" and which you can easily run. In
Unix/Linux, you cannot run a program unless the execute permission is set. If the execute permission is not set,
you might still be able to see/modify the program code(provided read & write permissions are set), but not run
it.
Let'sseethisinaction ls - l on
terminal gives
ls- l
Here, we have highlighted'-rw-rw-r--'and this weird looking code is the one that tells us about the
permissions given to the owner, user group and the world.
Here,thefirst'-'impliesthatwehaveselectedafile.p>
Else,ifitwereadirectory,dwouldhavebeen shown.
Thecharactersareprettyeasyto remember.
r = readpermission
w=writepermission
x=executepermission
-= nopermission
Letuslookatitthisway.
Thefirstpartofthecodeis'rw-'.Thissuggeststhattheowner'Home'can:
Readthefile
Writeoreditthefile
Hecannotexecutethefilesincetheexecutebitissetto'-'.
By design, many Linux distributions like Fedora, CentOS, Ubuntu, etc. will add users to a group
ofthesamegroupnameastheusername.Thus,auser'tom'isaddedtoagroupnamed'tom'.
Thesecondpartis'rw-'.Itfortheusergroup'Home'andgroup-members can:
Readthefile
Writeoreditthefile
Thethirdpartisfortheworldwhichmeansanyuser.Itsays 'r--'.Thismeanstheusercan only:
Readthefile
Changingfile/directorypermissionswith'chmod'command
Sayyoudonotwantyourcolleaguetoseeyourpersonalimages.Thiscanbeachievedby changing file permissions.
Wecanusethe'chmod'commandwhichstandsfor'changemode'.Usingthecommand,we can set permissions
(read, write, execute) on a file/directory for the owner, group and the world. Syntax:
chmod permissions filename
There are 2 ways to use the command -
1. Absolutemode
2. Symbolicmode
Absolute(Numeric)Mode
Inthismode,filepermissionsarenotrepresentedascharactersbutathree-digitoctal number.
Thetablebelowgivesnumbersforallforpermissionstypes.
0 No Permission ---
1 Execute --x
2 Write -w-
4 Read r--
Let'sseethechmodcommandin action.
Intheabove-giventerminalwindow,wehavechangedthepermissionsofthefile'sampleto '764'.
'764'absolutecodesaysthefollowing:
Ownercanread,writeandexecute
Usergroupcan readandwrite
Worldcanonly read
Thisisshownas'-rwxrw-r-
Thisishowyoucanchangethepermissionsonfilebyassigninganabsolutenumber.
SymbolicMode
In the Absolute mode, you change permissions for all 3 owners. In the symbolic mode, you can modify
permissions of a specific owner. It makes use of mathematical symbols to modify the file permissions.
Operator Description
+ Addsapermissiontoafileor directory
- Removesthepermission
= Setsthepermissionandoverridesthepermissionssetearlier.
Thevariousownersarerepresentedas-
User Denotations
u user/owner
g group
o other
a all
Wewillnotbeusingpermissionsinnumberslike755butcharacterslikerwx.Let'slookintoan example
ViEditor:
Vi is a command line text editor. As you would be quite aware now, the command line is quite a different
environment to your GUI. It's a single window with text input and output only. Vi has
beendesignedtoworkwithintheselimitationsandmanywouldargue,isactuallyquitepowerful as a result. Vi is
intended as a plain text editor (similar to Notepad on Windows, or Textedit on Mac) as opposed to a word
processing suite such as Word or Pages. It does, however have a lot more power compared to Notepad or
Textedit.
Aprocess refers to aprogram in execution; it’s a running instance of a program. It is made up of the program
instruction, data read from files, other programs orinput from a system user.
Types of Processes
init
It is the first process executed by the kernel during the booting of a system. It is a
daemonprocesswhichrunstillthesystemisshutdown.Thatis why,itis theparent ofalltheprocesses.Firstofall,init
readsthescriptstoredinthefile/etc/inittab.Commandinit readstheinitialconfigurationscriptwhich basically take
care of everything that a system do at the time of systeminitialization like setting the clock, initializing the
serial port and so on.
Foreground processes (also referred to as interactive processes) – these are initialized and controlled through
a terminal session. In other words, there has tobe a user connected to the system to start such processes;
they haven’t started automatically as part of the system functions/services.
Backgroundprocesses(alsoreferredtoasnon-interactive/automaticprocesses)
–areprocessesnotconnectedtoaterminal;theydon’texpectanyuserinput.
Batchprocess
ProcesseswhichareinqueueorstackexecutesonebyoneinFIFOorderarecalled batch process.
Daemons
These are special types of background processes that start at system startup and keep running forever as a
service; they don’t die. They are started as system tasks (run as services), spontaneously. However, they can
be controlled by a user via the init process.
Whatisazombieprocess?
When a process finishes execution, it will have an exit status to report to its parent
process. Because of this last little bit of information, the process will remain in the
operating system’s process table as azombie process, indicating that it is not to be
scheduled for further execution, but that it cannot be completely removed (and its process
ID cannot be reused) until it has been determined that the exit status is no longer needed.
Whenachildexits,theparentprocesswillreceiveaSIGCHLDsignaltoindicate that
oneofitschildrenhasfinishedexecuting;theparentprocesswilltypicallycall thewait()system call
exitstatus,andwillcausethechildtobe reaped,orremovedfromtheprocess table.
ProcessStates:
As a process executes it changesstateaccording to its circumstances. Linux processes have thefollowing states:
Running
Theprocessiseitherrunning(itisthecurrentprocessinthesystem)oritisreadytorun(itis waiting to be assigned to one
of the system's CPUs).
Waiting
The process is waiting for an event or for a resource. Linux differentiates between two types of waiting
process;interruptibleanduninterruptible. Interruptible waiting processes can be interrupted by signals whereas
uninterruptible waitingprocesses are waiting directly on hardware conditions and cannot be interrupted under
any circumstances.
Stopped
Theprocesshasbeenstopped,usuallybyreceivingasignal.Aprocess thatisbeingdebuggedcan be in a stopped state.
Zombie
Thisisahaltedprocesswhich,forsomereason,stillhasa task_structdatastructurein the task vector. It is what it sounds
like, a dead process.
RunningaForegroundProcess
Tostartaforegroundprocess,youcaneitherrunitfromthedashboard,oryoucanrunitfrom the terminal.
WhenusingtheTerminal,youwillhavetowait,untiltheforegroundprocess runs.
RunningaBackgroundprocess
Ifyoustartaforegroundprogram/processfromtheterminal,thenyoucannotworkonthe terminal, till the program is up
and running.
Particular,data-intensivetaskstakelotsofprocessingpowerandmayeventakehoursto complete. You do not want your
terminal to be held up for such a long time.
Toavoidsuchasituation,youcanruntheprogramandsendittothebackgroundsothat terminal remains available to you.
Let's learn how to do this -
Fg
Youcanusethecommand"fg"tocontinueaprogramwhichwasstoppedandbringittothe foreground.
Thesimplesyntaxforthisutilityis:
fg jobname
Example
1. Launch'banshee'musicplayer
2. Stopitwiththe'ctrl+z' command
3. Continueitwiththe'fg'utility.
Let'slookatotherimportantcommandstomanageprocesses-
Top
ThisutilitytellstheuseraboutalltherunningprocessesontheLinuxmachine.
NI Thenicevalueofa task 0 0
S Status S R
PS
This command stands for 'Process Status'. It is similar to the "Task Manager" that pop-ups in a Windows
Machine when we use Cntrl+Alt+Del. This command is similar to 'top' command but the information displayed
is different.
Tocheckalltheprocessesrunningunderauser,usethecommand-
psux
Youcanalsochecktheprocessstatusofasingleprocess,usethesyntax -
psPID
Kill
ThiscommandterminatesrunningprocessesonaLinuxmachine.
TousetheseutilitiesyouneedtoknowthePID(processid)oftheprocessyouwanttokill
Syntax –
kill PID
To find the PID of a process simply type
pidofProcessname
Letustry itwithanexample.
CreationofProcess:
Thebelowgivenprogramshowshow tocreateaprocess:
#include<errno.h>
#include<stdio.h>
#include<unistd.h> Main()
{
pid_tpid
pid=fork();//this will create a new process if(pid==-1) //if the
process is not created
perror(“fork”);//printingtheerrormessage else if(pid==0)
printf(“thisisachildprocesswithpid:%d\n”,pid);
else
printf(“thisisaparentprocesswithpid:%d\n”,pid);
}
Compilingfileprocess.c
$gccprocess.c
Executingthecompiled file
$./a.out
Theoutputgeneratedbythisprogramis:
Thisisachildprocesswithpid:0
Thisisaparentprocesswithpid:5335
Fork()function:
System callfork()is used to create processes. It takes no arguments and returns a process ID. The purpose
offork()is to create anewprocess, which becomes thechildprocess of the caller. After a new child process is
created, bothprocesses will execute the next instruction following thefork()system call. Therefore, we have to
distinguish the parent from the child. This can be done by testing the returned value of fork():
Iffork()returnsanegativevalue,thecreationofachildprocesswas unsuccessful.
fork()returnsazerotothenewlycreatedchildprocess.
fork()returns a positive value, theprocess IDof the child process, to the parent. The returned process ID is of
typepid_tdefined insys/types.h. Normally, the process ID isan integer. Moreover, a process can use function
getpid()to retrieve the process ID assigned to this process.
the child process will have a parent process ID (PPID) which is same as the PID of the process that created it.
Thechildprocesswillhaveauniqueprocessidentifier(PID),likeallotherprocessesin the system.
Thechildprocessdoesnothavethepossibilitytoinheritanytimersfromits parent.
ResourceutilizationandCPUtimerswillberesettozerointhechildprocess.
After the fork function completes its work, there exist two processes. Each process continues its execution from the
position, where fork() returns.
Syntaxoffork()function:
#include<sys/types.h>
#include<unistd.h>Pid_t
fork(void);
Vfork()function:
It is used for creating new processes without copying the page tables belonging to the
parentprocess.Vfork()isfasterthanafork().Itdoesnotmakeanycopyoftheparentprocessaddress
space, rather it will borrow the parents memory and thread of the control, until a call to exit or execve() is
occurred. The execve() executes the program pointed by specified filename. Whenever a new process is
created by using vfork(), the parent process is suspendedtemporarily and the child process will proceed by
borrowing the parent proess address space. This will be continued, until the child process calls execve() or exits
at which the parent process will be continued.
Vfork()functionsyntax:
#include<sys/types.h>
#include<vunistd.h>Pid_t vfork();
getpid()andgetppid()functions:
getpid(): the child process can acquire its own process ID by using getpid() function.
getppid():thechildprocesscanacquireitsparentprocessIDbyusinggetppid()function.
Wait()function:
Aprocessissaidtobeinwaitingstate,ifitneedssomeresourceforitsexecutionorsomeevent to occur.
Syntaxofwait()function:
#include<sys/wait.h>Int
wait(status)
Example:
#include<sys/wait.h>
#include<stdio.h>
#include<sys/wait.h> Main()
{
Int statuspr,pid,cpid;
Pid=fork();
If(pid!=0)
{
Printf(“processid%d”,pid);
Cpid=wait(&statuspr);
Printf(“childprocessisinwaitstate”);
}
Else
{
Printf(“processid%d”,getpid());
Exit()
}
Printf(“pid%disgoingtobe terminated:”,pid);
}
Reasonsforfailureofcreatinganewprocess
Thefollowingarethepossiblereasonsforthefailureofcreationofnew processes:
TheresourcelimitonthenumberofprocessespermittedtoaparticularuserIDhasbeen exceeded.
Thesystemwidelimitonthenumberofprocessesthatcanbecreatedhasbeen reached.
Scheduling
The scheduler is the component of the kernel that selects which process to run next. The scheduler (or process
scheduler, as it is sometimes called) can be viewed as the code thatdivides the finite resource of processor time
between the runnable processes on a system. The scheduler is the basis of a multitasking operating system
such as Linux. By deciding whatprocess can run, the scheduler is responsible for best utilizing the system and
giving the impression that multiple processes are simultaneously executing.
The idea behind the scheduler is simple. To best utilize processor time, assuming there are runnable processes,
a process should always be running. If there are more processes than processors in a system, some processes
will not always be running. These processes are waiting to run. Deciding what process runs next, given a set of
runnable processes, is a fundamental decision the scheduler must make.
Objectivesofscheduling:
CategoriesofProcesses:
I/Oboundprocesses:theprocesseswhichuseI/Odevices,andwaitforalongtimeforI/O operations to
complete, can be treated as I/O bound processes.
SCHED_RR:roundrobinscheduling
SchedulinginMultiprocessorSystems
In a multiprocessor system hopefully all of the processors are busily running processes.Eachwill run the
scheduler separately as its current process exhausts its time-slice or has to wait for a system resource. The first
thing to notice about an SMP system is that there is not just one idle
processinthesystem.Inasingleprocessorsystemtheidleprocessisthefirsttaskin
thetaskvector, in an SMP system there is one idle process per CPU as you could have more
thanoneidleCPU.Additionally thereisonecurrentprocessperCPU,soSMP systemsmustkeep track of the current
and idle processes for each processor.
Personalities
Name
personality-settheprocessexecutiondomain
Synopsis
#include<sys/personality.h>
intpersonality(unsignedlongpersona);
Description
Linux supports different execution domains, or personalities, for each process. Among other things, execution
domains tell Linux how to map signal numbers into signal actions. The execution domain system allows Linux to
provide limited support for binaries compiled under other UNIX-like operating systems.
Thisfunctionwillreturnthecurrent personality()whenpersonaequals0xffffffff.Otherwise,it will make the
execution domain referenced by personathe new execution domain of the calling process.
Return Value
Onsuccess,thepreviouspersonaisreturned.Onerror,-1isreturned,and errnoisset appropriately.
Clonning
NAME
clone- createa childprocess
SYNOPSIS
#define_GNU_SOURCE #include
<sched.h>
intclone(int(*fn)(void*),void*child_stack, int flags, void *arg, ...
/*pid_t*ptid,void*newtls,pid_t*ctid*/);
/*Fortheprototypeoftherawsystemcall,seeNOTES*/
DESCRIPTION
clone()createsanewprocess,inamannersimilartofork(2).
Note that, we cannot ignore SIGSTOP and SIGKILL signals, because these signals provide a way for the kernel
or root user to stop or kill process in any circumstances. The default action for these two signals is to
terminate the process. These two signals can neither be caught nor be ignored.
SendingandReceivingSignals:
The termraise is used to indicate the generation of a signal, and the term catchis used to indicate the receipt
of a signal.
Signals are raisedby error conditions, andthey are generatedby the shelland terminal handlers to cause
interrupts and can also be sent from one process to another to pass information or to modify the behavior.
Signalscanbe:
1. Raised
2. Caught
3. Actedupon
4. Ignored
If a process receives signals such as SIGFPE,SIGKILL, etc., the process will be terminated immediately, and
acore dumpfile is created. Thecorefile is an image of the process, and we can use it to debug.
Here is an example of the common situation when we use a signal: when we type the interrupt character
(Ctrl+C), theISGINTsignal will be sent to theforeground process(the program currently running). This will cause
the program to terminate unless it has some arrangement for catching the signal.
The commandkillcan be used to send a signal to a process other than the current foreground process.
DevelopmentwithLINUX:
Everyone has a favorite development platform. With Windows becoming less popular, is Linux getting the
breakthrough it needs? The answer, on the surface, is “Yes.”If you haven’t yet experimented with the
operating system yourself, now’s the time to give it a try.
According to theStack Overflow 2016 survey, desktop developers now constitute just 6.9 percent of all
developers (and that also includes Mac Desktops). Over half of programming jobs are now for Web
developers, and that has opened up development on alternative platformssuch as Linux and Mac. That’s why,
after 20 years of focusing on building Windows applications,I decided to learn Linux, specifically Ubuntu.
SoWhy Switch?
Here’swhyyoumightpreferLinuxtoMacorWindows.
Itoffersgreaterbreadthanddepthofopen-sourcesoftware.
It’slessfiddlyrunningopen-sourcesoftwareonLinux.
Itrunsonanything,especiallyanoldWindowsPC.
Onceyou’velearnedtheterminalcommands,youcanbeextremelyefficientand productive.
RebuildingaWindowsDevelopmentPCafteramajorcrashcantakehoursordaysto reinstall everything. Linux, by
contrast, is a lot quicker and also easier to containerize.
Thedevelopmentenvironmentissimilartoproduction.It’salsolikelytostaystable
longer,asthere’slesscommercialpressuretoreleasenewOS versions.
Intheworldofservers,it reignssupreme.It’salsoamajorplayerinmobile,thanksto
Android.
Let’slookatacoupleofthesepoints:
TerminalCommands
Linux at heart is a command-line operating system with many commands for controlling and configuring the
system, running applications, and so on. You can control everything from the command line.
This might be a bit of a controversial statement, but I’ve not seen any Linux GUIs that are quite as good as
Windows. To be fair to those GUis, they are far more user-friendly than a terminal, and let you run multiple
terminals at once; you can also browse around the file system far more easily than issuing commands in a
terminal. (To be a Linux developer, you should feel at home with terminal commands, particularly for installing
and updating software.)
Irrespective of the GUI used, if you are familiar with the terminal commands and one of the shells, you can
find your way around any Linux deployment. There are many popular shells, though everyone knows Bash
(a.k.a. Bourne-again Shell); they differ in features such as history, saving commands as scripts, command line
completion and the like.
Shell scripts are not unlike Windows batch files for controlling the system, but a lot more powerful. They
constitute a “mini-programming language,” albeit with many more commands, and include constants, string
and integer variables; if, case, for, while and until loops; as well as job control, shell functions and aliases (and
lots more).
RebuildingAfteraMajorCrash
If you’ve been developing on Windows for a couple of years, you’ve likely experienced a catastrophic failure at
some point, or had to move all your dev tools to a new version of Windows.
Windows 10 is perhaps better at handling crashes than previous versions, but I’ve had to reload my entire
development package on Windows 95, 2000, XP and 7. None of these were total disk crashes, and I had
backups, but each wasted a day or two before I could get back to my pre- crash state.
There are probably less than 20 pieces of development software that I need on a PC, but thatstill means a lot of
time spent reacquiring and reinstalling.
You can have the same issues with Linux, except most Linux upgrades don’t require application reinstalls (and
I’ve neverexperienced a catastrophic crashwhile working with Linux). In the event of a total implosion, much of
the software can be reinstalled with batch files made up ofmultiple sudo apt install statements. It’s just far
quicker.
Gcc:
The GNU compiler collection(gcc) is a command line compiler on linux systems. It is also c and c++ compiler
developed by the GNU project. For example, if one had written a program whose filename is ‘add’, the
compilation procedure is as follows:
The primaryway ofcompiling that file ‘add.c’ into an executable filecalled ‘add’ is gcc -o add add.c.
Iftheprogramcompileswithoutanyerrors,itcanbeexecutedbytyping‘./add’
Gdb:
Thegdbwillstepthroughthesourcecodelinebylineorinstructionbyinstruction.Itisalso possible to know the value of any
variable at run time.
Gnat:
It is an acronym for GNU NYU ada translator (GNAT). It is a free software compiler for the programming
language ‘ada’ which is part of the GNU compiler collection. The gcc compiler is
havingthecapabilityofcompilingprogramswritteninseverallanguagesincludingada95, andc. if the file extension is
either ‘.ads’ or ‘.adb’, it assumes that the given program has been writtenin ada and, then it will call the GNAT
compiler to compile the particular specified file.
WhatisMySQL
MySQL is a fast, easy to use relational database. It is currently the most popular open- source database.
It is very commonly used in conjunction with PHP scripts to create powerful and dynamic server-side
applications.
MySQL is used for many small and big businesses. It is developed, marketed and supported by MySQL
AB, a Swedish company. It is written in C and C++.
Reasonsofpopularity
MySQLisbecomingsopopularbecauseofthesefollowingreasons:
o MySQLisanopen-sourcedatabasesoyoudon'thavetopayasinglepennytouseit.
o MySQL is a very powerful program so it can handle a large set of functionality of the most expensive
and powerful database packages.
o MySQLiscustomizablebecauseitisanopensourcedatabaseandtheopen-sourceGPL license facilitates
programmers to modify the SQL software according to their own specific environment.
o MySQL is quicker than other databases so it can work well even with the large data set.
o MySQL supports many operating systems with many languages like PHP, PERL, C, C++, JAVA, etc.
o MySQLusesastandardformofthewell-knownSQLdatalanguage.
o MySQLisveryfriendlywithPHP,themostpopularlanguageforwebdevelopment.
o MySQLsupportslargedatabases,upto50millionrowsormoreinatable.Thedefault
filesizelimitforatableis4GB,butyoucanincreasethis(ifyouroperatingsystemcan handle it) to a theoretical
limit of 8 million terabytes (TB).
HistoryofMySQL
MySQL is an open source database product that was created by MySQL AB, a company founded in 1995
in Sweden. In 2008, MySQL AB announced that it had agreed to be acquired by Sun Microsystems for
approximately $1 billion.
InitialEfforts
The project of MySQL was started in 1979, when MySQL's inventor, Michael Widenius developed an in-
house database tool called UNIREG for managing databases. After that UNIREG has been rewritten in
several different languages and extended to handle big databases. After some time Michael Widenius
contacted David Hughes, the author of mSQL, to see if Hughes would be interested in connecting mSQL
to UNIREG's B+ ISAM handler to provide indexing to mSQL. That's the way MySQL came in existence.
MySQLisnamedafterthedaughterofMichaelWideniuswhosenameis"My".
MySQLFeature
s
o Relational Database Management System (RDBMS): MySQL is a relational database management
system.
o Easytouse:MySQLiseasytouse.YouhavetogetonlythebasicknowledgeofSQL. You can build and interact
with MySQL with only a few simple SQL statements.
o It issecure: MySQL consistofa soliddatasecuritylayerthatprotects sensitivedata from intruders.
Passwords are encrypted in MySQL.
o Client/ServerArchitecture: MySQLfollowsaclient/serverarchitecture.Thereisa database server (MySQL)
and arbitrarily many clients (application programs), which communicate with the server; that is, they
query data, save changes, etc.
o Freetodownload:MySQLisfreetouseandyoucandownloaditfromMySQLofficial website.
o It is scalable: MySQL can handle almost any amount of data, up to as much as 50 million rows or more.
The default file size limit is about 4 GB. However, you can increase this number to a theoretical limit of
8 TB of data.
o Compatibale on many operating systems: MySQL is compatible to run on many operating systems, like
Novell NetWare, Windows* Linux*, many varieties of UNIX* (such as Sun* Solaris*, AIX, and DEC*
UNIX), OS/2, FreeBSD*, and others. MySQL
alsoprovidesafacilitythattheclientscanrunonthesamecomputerastheserveror on another computer
(communication via a local network or the Internet).
o Allows roll-back: MySQL allows transactions to be rolled back, commit and crash recovery.
o HighPerformance:MySQLisfaster,morereliableandcheaperbecauseofitsunique storage engine
architecture.
o High Flexibility: MySQL supports a large number of embedded applications which makes MySQL very
flexible.
o High Productivity: MySQL uses Triggers, Stored proceduresand viewswhich allows the developer to give
a higher productivity.
Disadvantages/DrawbackofMySQL:
FollowingarethefewdisadvantagesofMySQL:
o MySQLversionlessthan5.0doesn'tsupportROLE,COMMITandstoredprocedure.
o MySQLdoesnotsupportaverylargedatabasesizeasefficiently.
o MySQLdoesn'thandletransactionsvery efficientlyanditispronetodatacorruption.
o MySQLisaccusedthatitdoesn'thaveagooddevelopinganddebuggingtoolcompared to paid databases.
o MySQLdoesn'tsupportSQLcheckconstraints.
MySQLData Types
A Data Type specifies a particular type of data, like integer, floating points, Boolean etc. It also identifies
the possible values for that type, the operations that can be performed on that type and the way the
values of that type are stored.
MySQL supports a lot number of SQL standard data types in various categories. It uses many different
data types broken into mainly three categories: numeric, date and time, and string types.
NumericDataTyp
e
DataType Description
Syntax
INT Anormal-sizedintegerthatcanbesignedorunsigned.Ifsigned,theallowablerangeisfrom
-
2147483648to2147483647.Ifunsigned,theallowablerangeisfrom0to4294967295.You
can specify a width of up to 11 digits.
TINYINT A very small integer that can be signed or unsigned. If signed, the allowable range
is from - 128 to 127. If unsigned, the allowable range is from 0 to 255. You can
specify a width of up to 4 digits.
SMALLINT Asmallintegerthatcanbesignedorunsigned.Ifsigned,theallowablerangeisfrom -32768
to32767.If unsigned,theallowablerange isfrom0to65535.Youcanspecifyawidthofup
to 5 digits.
MEDIUMINT Amedium-sizedintegerthatcanbesignedorunsigned.Ifsigned,theallowablerange isfrom
-8388608to8388607.Ifunsigned,theallowablerangeisfrom0to16777215.Youcan
specify a width of up to 9 digits.
BIGINT A large integer that can be signed or unsigned. If signed, the allowable range is
from -
9223372036854775808to9223372036854775807.Ifunsigned,theallowablerangeisfr
om 0 to 18446744073709551615. You can specify a width of up to 20 digits.
FLOAT(m,d) Afloating-
pointnumberthatcannotbeunsigned.Youcandefinethedisplaylength(m)and the
number of decimals (d). This is not required and will default to 10,2, where 2 is the
number of decimals and 10 is the total number of digits (including decimals).
Decimal precision can go to 24 places for a float.
DOUBLE(m,d) A double precision floating-point number that cannot be unsigned. You can define
the display length (m) and the number of decimals (d). This is not required and will
default to
16,4,where4isthenumberofdecimals.Decimalprecisioncangoto53placesforadouble.
Real is a synonym for double.
DECIMAL(m,d) An unpacked floating-point number that cannot be unsigned. In unpacked decimals,
each
decimalcorrespondstoonebyte.Definingthedisplaylength(m)andthenumberofdecimals
(d)isrequired.Numericisasynonymfordecimal.
DateandTimeDataType
:
DataType MaximumSize Explanation
Syntax
StringDataTypes:
TINYBLOB Maximumsizeof255bytes.
BLOB(size) Maximumsizeof65,535bytes.
MEDIUMBLOB Maximumsizeof16,777,215bytes.
LONGTEXT Maximumsizeof4gbor4,294,967,295characters.
HowtoinstallMySQL
Download MySQL Follow
these steps:
o GotoMySQLofficialwebsitehttp://www.mysql.com/downloads/
o ChoosetheversionnumberforMySQLcommunityserverwhichyouwant.
InstallingMySQLonWindows
Your downloaded MySQL is neatly packaged with an installer. Download the installer package, unzip it
anywhere and run setup.exe.
Bydefault,thisprocesswillinstalleverythingunderC:\mysql.
VerifyMySQLinstallation
Open yourMySQL Command LineClient, it should beappeared with a mysql> prompt. If you have set
any password, write your password here. Now, you are connected to the MySQL server and you can
execute all the SQL command at mysql> prompt as follows:
Forexample:Checkthealreadycreateddatabaseswithshowdatabasescommand:
MysqlUserCreation:SettingUpaNewMySQLUserAccount
When you try to access MySQL database server from client such as mysql or even programming
language such asphporperl youneedauseraccount.MySQLhassophisticatedusermanagement system that
controls who can access server and from which client system. It uses special tables in mysql database.
In order to create a new user account you need a MySQL root account password. You need to use the
GRANT SQL command to set up the MySQL user account. Finally, use the account’s name and password
to make connections to the MySQL server.
$mysqladmin-urootpassword NEWPASSWO
Ifyouwanttochangeorupdatearootuserpassword,thenyouneedtousethefollowing command:
$mysqladmin-uroot-p'oldpassword'passwordnewpass
ProcedureforsettingupaMySQLuser account
Logininasmysqlrootuser.Atshellprompttypethefollowing command:
$mysql-uroot-p OR
$mysql-uroot-hyour-mysql-server-host-name-p
Createanewmysqldatabasecalleddemo.Typethefollowingcommandat mysql>prompt: mysql>CREATE
DATABASE demo;
Createanewusercalleduser1fordatabasecalleddemo:
mysql>GRANTALLONdemo.*TOuser1@localhostIDENTIFIEDBY'mypassword';
HowdoIconnecttoMySQLdatabasedemousinguser1 account?
Useruser1canconnecttodemodatabaseusingthefollowingshellcommand:
$mysql-uuser1-pdemo OR
$mysql-uuser1-hyour-mysql-server-host-name-here-pdemo
Where,
-uuser1:MySQL Username
-h:MySQL servername(defaultislocalhost)
-p:Promptforpassword
demo:demoisnameofmysqldatabase(optional)
StartingandTerminatingmysql
Problem
Youwanttostartandstopthemysqlprogram.
Solution
Invoke mysql from your command prompt to start it, specifying any connection parameters
that may be necessary. To leave mysql, use a QUIT statement.
Discussion
Tostartthemysqlprogram,tryjusttypingitsnameatyourcommand-lineprompt.Ifmysqlstarts
upcorrectly,you’llseeashortmessage,followedbyamysql>promptthatindicatestheprogramis
ready to accept queries. To illustrate, here’s what the welcome message looks like
%mysql
Welcome to the MySQL monitor.Commands end with ; or \g. YourMySQLconnectionidis18427toserverversion:3.23.51-log
Type'help;'or'\h'forhelp.Type'\c'toclearthebuffer. mysql>
Ifmysqltriestostartbutexitsimmediatelywithan“accessdenied”message,you’llneedtospecify
connectionparameters.Themostcommonlyneededparametersarethehosttoconnectto(thehost
where
%mysql the MySQL cbuser
-hlocalhost-p-u server runs), your MySQL username, and a password. For example:
Enterpassword:cbpass
Ingeneral,I’llshowmysqlcommandsinexampleswithnoconnectionparameteroptions.Iassume
that you’ll supply any parameters that you need, either on the command line, or in an option
file (Recipe 1.5) so that you don’t have to type them each time you invoke mysql.
Ifyoudon’thaveaMySQLusernameandpassword,youneedtoobtainpermissiontousetheMySQL
server,
Thesyntaxanddefaultvaluesfortheconnectionparameteroptionsareshowninthefollowingtable.
These options have both a single-dash short form and a double-dash long form.
As the
table indicates,there is nodefault password.To supplyone,use --password or-p,then enter your password when
mysql prompts you for it:
%mysql-pEnterpassword:←enteryourpassword here
If you like, you can specify the password directly on the command line by using either -p password (note
that there is no space after the -p) or --password=password. I don’t recommend doing this on a
multiple-user machine, because the password may be visible momentarily to other users who are
running tools such as ps that report process information.
MySQLCreate Database
YoucancreateaMySQLdatabasebyusingMySQL CommandLineClient.
OpentheMySQLconsoleandwritedownpassword,ifyousetonewhileinstallation.Youwillgetthe following:
Syntax:
CREATEDATABASEdatabase_name;
Example:
Let'stakeanexampletocreateadatabasename"employees"
CREATEDATABASEemployees;
Itwilllooklikethis:
Youcancheckthecreateddatabasebythefollowingquery:
SHOWDATABASES;
Output
MySQLSELECTDatabase
Syntax:
USEdatabase_name;
Example:
Let'stakeanexampletouseadatabasename"customers". USE
customers;
Itwilllooklikethis:
Note:Allthedatabasenames,tablenamesandtablefieldsnamearecasesensitive.Youmusthave to use
proper names while giving any SQL command.
MySQLDropDatabase
You can drop/delete/remove a MySQL database easily with the MySQL command. You should
becarefulwhiledeletinganydatabasebecauseyouwillloseyourallthedataavailableinyourdatabase.
Syntax:
DROPDATABASEdatabase_name;
Example:
Let'stakeanexampleto dropadatabasename"employees"
DROPDATABASEemployees; It will
look like this:
Nowyoucancheckthateitheryourdatabaseisremovedbyexecutingthefollowing query:
SHOWDATABASES;
Output:
Here,youcanseethatthedatabase"employees"isremoved.
MySQLCREATETABLE
o Nameofthe table
o Namesof fields
o Definitionsforeachfield
Syntax:
FollowingisagenericsyntaxforcreatingaMySQLtableinthe database.
Example:
CREATETABLEcus_tbl(
cus_idINT NOTNULLAUTO_INCREMENT,
cus_firstnameVARCHAR(100)NOTNULL,
cus_surname VARCHAR(100) NOT NULL,
PRIMARY KEY ( cus_id )
);
Note:
Seethecreatedtable:
Usethefollowingcommandtoseethetablealreadycreated:
1. SHOWtables;
Seethetablestructure:
Usethefollowingcommandtoseethetablealreadycreated:
1. DESCRIBEcus_tbl;
MySQLALTERTable
MySQL ALTER statement is used when you want to change the name of your table or any table
field. It is also used to add or delete an existing column in a table.
TheALTERstatementisalwaysusedwith"ADD","DROP"and"MODIFY"commandsaccordingto the
situation.
1) ADDacolumninthetable
Syntax:
ALTERTABLEtable_name
ADDnew_column_namecolumn_definitio
n [ FIRST | AFTER column_name ];
Parameters
table_name:Itspecifiesthenameofthetablethatyouwanttomodify.
new_column_name:Itspecifiesthenameofthenewcolumnthatyouwanttoaddtothetable.
column_definition:Itspecifiesthedatatypeanddefinitionofthecolumn(NULLorNOTNULL, etc).
FIRST | AFTER column_name: It is optional. It tells MySQL where in the table to create the
column.Ifthisparameterisnotspecified,thenewcolumnwillbeaddedtotheendofthetable.
Example:
Output:
Seetherecentlyaddedcolumn:
1.SELECT*FROM cus_tbl;
Output:
2) Addmultiplecolumnsinthetable
Syntax:
ALTERTABLEtable_name
ADDnew_column_namecolumn_definition [
FIRST | AFTER column_name ],
ADDnew_column_namecolumn_definition [
FIRST | AFTER column_name ],
...;
Example:
In this example, we add two new columns "cus_address", and cus_salary in the existing table
"cus_tbl".cus_addressisaddedaftercus_surnamecolumnandcus_salaryisaddedaftercus_age column.
Usethefollowingquerytodothis: ALTER TABLE
cus_tbl
ADDcus_addressvarchar(100)NOTNULL
AFTERcus_surname,
ADDcus_salaryint(100)NOTNULL
AFTERcus_age;
Seetherecentlyaddedcolumns:
SELECT*FROMcus_tbl;
3) MODIFYcolumninthetable
TheMODIFYcommandisusedtochangethecolumndefinitionofthe table.
Syntax:
ALTERTABLEtable_name
MODIFYcolumn_namecolumn_definition [ FIRST
| AFTER column_name ];
Example:
Usethefollowingquerytodothis:
ALTERTABLEcus_tbl
MODIFYcus_surnamevarchar(50)NULL;
Seethetablestructure:
4) DROPcolumn in table
Syntax:
ALTERTABLEtable_name
DROP COLUMNcolumn_name;
Let'stakeanexampletodropthecolumnname"cus_address"fromthetable"cus_tbl".
Usethefollowingquerytodothis:
ALTERTABLEcus_tbl
DROP COLUMNcus_address;
Output:
Seethetablestructure:
5) RENAMEcolumnintable
Syntax:
ALTERTABLEtable_name
CHANGECOLUMNold_namenew_name column_definition
[FIRST|AFTERcolumn_name ]
Example:
Inthisexample,wewillchangethecolumnname"cus_surname"to"cus_title".
Usethefollowingquerytodothis: ALTER
TABLEcus_tbl
CHANGECOLUMNcus_surname cus_title
varchar(20)NOTNULL;
Output:
6) RENAMEtable
Syntax:
1. ALTERTABLEtable_name
2. RENAMETOnew_table_name;
Example:
Output:
Seetherenamed table:
MySQLTRUNCATETable
MYSQLTRUNCATEstatementremovesthecompletedatawithoutremovingitsstructure.
The TRUNCATE TABLE statement is used when you want to delete the complete data from a table
without removing the table structure.
Syntax:
TRUNCATETABLEtable_name;
Example:
Thisexamplespecifieshowtotruncateatable.Inthisexample,wetruncatethetable"cus_tbl".
TRUNCATETABLEcus_tbl;
Output:
Seethetable:
1. SELECT*FROMcus_tbl;
Output:
MySQLDROPTable
MYSQLDROPtablestatementremovesthecompletedatawithstructure.
Syntax:
DROPTABLEtable_name;
Example:
Thisexamplespecifieshowtodropatable.Inthisexample,wearedroppingthetable"cus_tbl".
DROPTABLEcus_tbl;
MySQLTRUNCATETablevsDROPTable
You can also use DROPTABLEcommand to delete complete table but it will remove complete table data
and structure both. You need to re-create the table again if you have to store some data. But in the
case of TRUNCATE TABLE, it removes only table data not structure. You don't need to re- create the
table again because the table structure already exists.
MySQLView
InMySQL,Viewisavirtualtablecreatedbyaquerybyjoiningoneormoretables. MySQL Create
VIEW
AVIEWiscreatedbySELECTstatements.SELECTstatementsareusedtotakedatafromthesource table to make
a VIEW.
Syntax:
CREATE[ORREPLACE]VIEW view_nameAS
SELECT columns FROM
tables
[WHEREconditions];
Parameters:
view_name:ItspecifiesthenameoftheVIEWthatyouwanttocreateinMySQL.
WHEREconditions:Itisalsooptional.Itspecifiestheconditionsthatmustbemetfortherecords to be included
in the VIEW.
CREATEVIEW trainerAS
SELECT course_name,course_trainer
FROMcourses;
Tosee thecreatedVIEW:
Syntax:
SELECT*FROM view_name;
Let'sseehowitlooksthecreated VIEW:
SELECT*FROM trainer;
MySQLUpdateVIEW
Syntax:
ALTERVIEWview_nameAS SELECT
columns
FROM table
WHEREconditions;
Example:
ThefollowingexamplewillalterthealreadycreatedVIEWname"trainer"byaddinganewcolumn.
ALTERVIEWtrainerAS
SELECTcourse_name,course_trainer, course_id
FROMcourses;
ToseethealteredVIEW:
SELECT*FROMtrainer;
MySQLDropVIEW
YoucandroptheVIEWbyusingtheDROPVIEW statement.
Syntax:
DROPVIEW[IFEXISTS]view_name; Parameters:
view_name:ItspecifiesthenameoftheVIEWthatyouwanttodrop.
IF EXISTS: It is optional. If you do not specifythis clause and the VIEW doesn't exist, the DROPVIEW
statement will return an error.
Example:
DROPVIEWtrainer;
MySQLQueries
A list of commonly used MySQL queries to create database, use database, create table, insert record,
update record, delete record, select record, truncate table and drop table are given below.
1) MySQLCreate Database
MySQLcreatedatabaseisused tocreatedatabase.Forexample
createdatabasedb1;
2) MySQLSelect/UseDatabase
MySQLcreatequeryisusedtocreateatable,view,procedureandfunction.Forexample:
CREATETABLEcustomers (id
int(10),
name varchar(50), city
varchar(50),
PRIMARYKEY(id));
4) MySQLAlterQuery
MySQLalterqueryisusedtoadd,modify,deleteordropcolumsofatable.Let'sseeaquerytoadd column in
customers table:
ALTERTABLEcustomers
ADDagevarchar(50);
5) MySQLInsertQuery
MySQLinsertqueryisusedtoinsertrecordsintotable.For example:
insertintocustomersvalues(101,'rahul','delhi');
6) MySQLUpdateQuery
MySQLupdatequeryisusedtoupdaterecordsofatable.Forexample:
updatecustomerssetname='bob',city='london'whereid=101;
7) MySQLDeleteQuery
MySQLupdatequeryisusedtodeleterecordsofatablefromdatabase.Forexample:
deletefromcustomerswhereid=101;
8) MySQLSelectQuery
Oracleselectqueryisusedtofetchrecordsfromdatabase.For example:
SELECT*fromcustomers;
9) MySQLTruncateTable Query
MySQL update queryis used to truncate or remove records ofa table.It doesn't remove structure. For
example:
truncatetablecustomers;
MySQL drop query is used to drop a table, view ordatabase.It removes structure and data of atable if
you drop table. For example:
droptablecustomers;
MySQLINSERTStatement
Syntax:
TheSQLINSERTINTOcommandisusedtoinsertdatainMySQLtable.Followingisagenericsyntax:
INSERTINTOtable_name(field1,field2,...fieldN)
VALUES
Fieldnameisoptional.Ifyouwanttospecifypartialvalues,fieldnameis mandatory.
(value1,value2,...valueN);
Syntaxforallfields:
INSERTINTOtable_nameVALUES(value1,value2,...valueN);
MySQLINSERTExample1:forallfields
Ifyouhavetostoreallthefieldvalues,eitherspecifyallfieldnameordon'tspecifyanyfield.
Example:
Or,
INSERTINTOcus_tbl
(cus_id,cus_firstname,cus_surname)
VALUES
(5,'Ajeet','Maurya'),
(6,'Deepika','Chopra'),
(7,'Vimal','Jaiswal');
Visual Representation:
SeethedatawithinthetablebyusingtheSELECT command:
MySQLUPDATE Query
Syntax:
FollowingisagenericsyntaxofUPDATEcommandtomodifydataintotheMySQLtable:
UPDATEtable_nameSETfield1=new-value1,field2=new-value2 [WHERE
Clause]
Note:
o Oneormorefieldcanbeupdatedaltogether.
o AnyconditioncanbespecifiedbyusingWHEREclause.
o Youcanupdatevaluesinasingletableatatime.
o WHEREclauseisusedtoupdateselectedrowsinatable.
Example:
Here, we have a table "cus_tbl" within the database"customers". We are goingto updatethe data within
the table "cus_tbl".
Thisquerywillupdatecus_surnamefieldforarecordhavingcus_idas5.
UPDATEcus_tbl
SET cus_surname ='Ambani'
WHEREcus_id=5;
Visual Representation:
OutputbySELECT query:
1. SELECT*FROM cus_tbl;
Syntax:
DELETEFROMtable_name
WHERE
(Conditionspecified);
Example:
DELETEFROMcus_tbl
WHEREcus_id=6;
Output:
MySQLSELECTStatement
Syntaxforspecified fields:
SELECT expressions
FROM tables
[WHEREconditions];
Syntaxforallfields:
SELECT*FROMtables[WHEREconditions];
MySQLSELECTExample1:forspecifiedfields
Insuchcase,itismandatorytospecifyfield names.
Example:
SELECTofficer_name,address
FROMofficers
MySQLSELECTExample2:forall fields
Insuchcase,wecanspecifyeitherallfieldsor*(asterisk)symbol.
SELECT*FROM officers
MySQLSELECTExample3:frommultipletables
MySQLWHEREClause
MySQLWHEREClauseisusedwithSELECT,INSERT,UPDATEandDELETEclausetofiltertheresults. It specifies a
specific position where you have to do the operation.
Syntax:
WHEREconditions;
Parameter:
conditions:Itspecifiestheconditionsthatmustbefulfilledforrecordstobeselected. MySQL
WHERE Clause with single condition
Let'stakeanexampletoretrievedatafromatable"officers".
Table structure:
Executethisquery:
SELECT*
FROMofficers
WHEREaddress='Mau';
Output:
MySQLWHEREClausewithAND condition
Inthisexample,weareretrievingdatafromthetable"officers"withAND condition.
Executethefollowingquery:
SELECT*
FROMofficers
WHEREaddress='Lucknow'AND
officer_id < 5;
Output:
WHEREClausewithORcondition
Executethefollowingquery:
SELECT*
FROMofficers
WHEREaddress='Lucknow'OR
address = 'Mau';
Output:
MySQLWHEREClausewithcombinationofAND&ORconditions
YoucanalsousetheAND&ORconditionsaltogetherwiththeWHEREclause.
Seethisexample:
Executethefollowingquery:
SELECT*
FROMofficers
WHERE(address='Mau'ANDofficer_name='Ajeet') OR
(officer_id < 5);
Output:
MySQLDistinct Clause
MySQLDISTINCTclauseisusedtoremoveduplicaterecordsfromthetableandfetchonlytheunique records.
The DISTINCT clause is only used with the SELECT statement.
Syntax:
SELECTDISTINCTexpressions
FROM tables
[WHEREconditions];
Parameters
expressions:specifythecolumnsorcalculationsthatyouwanttoretrieve.
WHERE conditions: It is optional. It specifies the conditions that must be met for the records to be
selected.
Note:
MySQLDISTINCTClausewithsingleexpression
Seethetable:
Usethefollowingquery:
SELECTDISTINCTaddress
FROMofficers;
MySQLDISTINCTClausewithmultipleexpressions
If you use multiple expressions with DISTINCT Clause then MySQL DISTINCT clause will
removeduplicates from more than one field in your SELECT statement.
Usethefollowingquery:
SELECT DISTINCTofficer_name,address
FROMofficers;
MySQLFROMClause
Syntax:
FROMtable1
[{INNER JOIN|LEFT[OUTER]JOIN| RIGHT[OUTER]JOIN} table2
ONtable1.column1=table2.column1]
Parameters
table1 and table2: specify tables used in the MySQL statement. The two tables are joined based on
table1.column1 = table2.column1.
Note:
o If you are using the FROM clause in a MySQL statement then at least one table must have been
selected.
o If you are using two or more tables in the MySQL FROM clause, these tables are generally joined using
INNER or OUTER joins.
MySQLFROMClause:Retrievedatafromone table
Thefollowingqueryspecifieshowtoretrievedatafromasingle table.
UsethefollowingQuery:
SELECT*
FROMofficers
WHEREofficer_id<=3;
MySQL FROM Clause: Retrieve data from two tables with inner join
MySQLFROMClause:Retrievedatafromtwotablesusingouter join MySQL
ORDER BY Clause
TheMYSQLORDERBYClauseisusedtosorttherecordsinascendingordescending order.
Syntax:
SELECTexpressions
FROM tables
[WHEREconditions]
ORDERBYexpression[ASC|DESC];
Parameters
expressions:Itspecifiesthecolumnsthatyouwanttoretrieve.
tables:Itspecifiesthetables,fromwhereyouwanttoretrieverecords.Theremustbeatleastone table listed in
the FROM clause.
WHEREconditions:Itisoptional.Itspecifiesconditionsthatmustbefulfilledfortherecordstobe selected.
ASC:Itisoptional.Itsortstheresultsetinascendingorderbyexpression(default,ifnomodifieris provider).
DESC:Itisalsooptional.Itsortstheresultsetindescendingorderbyexpression.
Note: You can use MySQL ORDER BY clause in a SELECT statement, SELECT LIMIT statement, and DELETE LIMIT statement.
MySQLORDERBY:withoutusingASC/DESCattribute
If you use MySQL ORDER BY clause without specifying the ASC and DESC modifier then by default you
will get the result in ascending order.
Executethefollowingquery:
SELECT*
FROMofficers
WHEREaddress='Lucknow'
ORDERBYofficer_name;
Output:
MySQLORDERBY:withASCattribute
Let'stakeanexampletoretrievethedatainascending order.
Executethefollowingquery:
SELECT*
FROMofficers
WHEREaddress='Lucknow'
ORDER BYofficer_nameASC;
Output:
MySQLORDERBY:withDESCattribute
SELECT*
FROMofficers
WHEREaddress='Lucknow'
ORDERBYofficer_nameDESC;
MySQLORDERBY:usingbothASCandDESCattributes
Executethefollowingquery: SELECT
officer_name, address
FROMofficers
WHEREofficer_id<5
ORDERBYofficer_nameDESC,addressASC; Output:
MySQLGROUPBY Clause
The MYSQL GROUP BY Clause is used to collect data from multiple records and group the result by one
or more column. It is generally used in a SELECT statement.
You can also use some aggregate functions like COUNT, SUM, MIN, MAX, AVG etc. on the grouped
column.
Syntax:
SELECTexpression1,expression2,...expression_n,
aggregate_function (expression)
FROM tables
[WHEREconditions]
GROUPBYexpression1,expression2,...expression_n;
Parameters
aggregate_function: It specifies a function such as SUM, COUNT, MIN, MAX, or AVG etc. tables:
Itspecifiesthetables,fromwhereyouwanttoretrievetherecords.Theremustbeatleastonetable listed in the
FROM clause.
WHERE conditions: It is optional. It specifies the conditions that must be fulfilled for the records to be
selected.
(i) MySQLGROUPBYClausewithCOUNTfunction
Consideratablenamed"officers"table,havingthefollowingrecords.
Now,let'scountrepetitivenumberofcitiesinthecolumnaddress.
Executethefollowingquery: SELECT
address, COUNT(*)
FROMofficers
GROUP BY address;
Output:
(ii) MySQLGROUPBYClausewithSUMfunction
Let'stakeatable"employees"table,havingthefollowingdata.
Executethefollowingquery:
SELECTemp_name,SUM(working_hours)AS"Totalworking hours"
FROMemployees
GROUP BY emp_name;
Output:
(iii) MySQLGROUPBYClausewithMIN function
Thefollowingexamplespecifiestheminimumworkinghoursoftheemployeesformthetable "employees".
Executethefollowingquery:
SELECTemp_name,MIN(working_hours)AS"Minimumworking hour"
FROMemployees
GROUP BY emp_name;
Output:
(iv) MySQLGROUPBYClausewithMAXfunction
Thefollowingexamplespecifiesthemaximumworkinghoursoftheemployeesformthetable "employees".
Executethefollowingquery:
SELECTemp_name,MAX(working_hours)AS"Minimumworking hour"
FROMemployees
GROUP BY emp_name;
Output:
(v) MySQLGROUPBYClausewithAVGfunction
Thefollowingexamplespecifiestheaverageworkinghoursoftheemployeesformthetable "employees".
Executethefollowingquery:
SELECTemp_name,AVG(working_hours)AS"Averageworking hour"
FROMemployees
GROUP BY emp_name;
Output:
MySQLHAVING Clause
MySQLHAVINGClauseisusedwithGROUPBYclause.Italwaysreturnstherowswhereconditionis TRUE.
Syntax:
SELECTexpression1,expression2,...expression_n,
aggregate_function (expression)
FROM tables
[WHEREconditions]
GROUPBYexpression1,expression2,...expression_n
HAVINGcondition;
Parameters
aggregate_function: It specifies any one of the aggregate function such as SUM, COUNT, MIN, MAX, or
AVG.
WHEREconditions:Itisoptional.Itspecifiestheconditionsfortherecordstobe selected.
HAVING condition: It is used to restrict the groups of returned rows. It shows only those groups in
result set whose conditions are TRUE.
HAVINGClausewithSUMfunction
Consideratable"employees"tablehavingthefollowingdata.
Here, we use the SUM function with the HAVING Clause to return the emp_name and sum of their
working hours.
Executethefollowingquery:
SELECTemp_name,SUM(working_hours)AS"Totalworking hours"
FROMemployees
GROUP BY emp_name
HAVINGSUM(working_hours)> 5;
Simply,itcanalsobeusedwithCOUNT,MIN,MAXandAVGfunctions.
MySQLConditions
MySQLANDMySQLORMySQLANDORMySQLLIKEMySQLINMySQLNOTMySQLISNULLMySQLIS NOT
NULLMySQL BETWEEN
MySQL Join
MySQLJOINS
MySQLJOINSareusedwithSELECTstatement.Itisusedtoretrievedatafrommultipletables.Itis performed whenever
you need to fetch records from two or more tables.
TherearethreetypesofMySQLjoins:
o MySQLINNERJOIN(orsometimescalledsimplejoin)
o MySQLLEFTOUTERJOIN(orsometimescalledLEFTJOIN)
o MySQLRIGHTOUTERJOIN(orsometimescalledRIGHTJOIN)
MySQL Inner JOIN (Simple Join)
The MySQL INNER JOIN is used to return all rows from multiple tables where the join condition is
satisfied. It is the most common type of join.
Syntax:
Imagerepresentation:
Let'stakean example:
Considertwotables"officers"and"students",havingthefollowing data.
Executethefollowingquery:
SELECTofficers.officer_name,officers.address,students.course_name
FROMofficers
INNERJOINstudents
ONofficers.officer_id=students.student_id;
Output:
MySQLLeftOuterJoin
The LEFT OUTER JOIN returns all rows from the left hand table specified in the ON condition and only
those rows from the other table where the join condition is fulfilled.
Syntax:
SELECTcolumns
FROMtable1
LEFT[OUTER]JOINtable2
ONtable1.column=table2.column;
Imagerepresentation:
Let'stakean example:
Considertwotables"officers"and"students",havingthefollowing data.
Executethefollowingquery:
SELECTofficers.officer_name,officers.address,students.course_name
FROM
officersLEFTJOINstude
nts
ONofficers.officer_id=students.student_id;
Output:
MySQLRightOuterJoin
Syntax:
SELECTcolumns
FROMtable1
RIGHT[OUTER]JOINtable2
ONtable1.column=table2.column;
Imagerepresentation:
Let'stakean example:
Considertwotables"officers"and"students",havingthefollowing data.
Executethefollowingquery:
SELECTofficers.officer_name,officers.address,students.course_name,students.student_name
FROM
officersRIGHTJOINstude
nts
ONofficers.officer_id=students.student_id;
Output:
AggregateFunctions
MySQLcount()MySQLsum()MySQLavg()MySQLmin()MySQLmax()MySQLfirst()MySQL last()
StringFunctions:
1 CHAR_LENGTH(str) mysql>SELECTCHAR_LENGTH("text");
+ +
Returns the length of the string str, | CHAR_LENGTH("text") |
measured in characters.Amulti- +------------------------------------------------+
bytecharactercountsasasingle character. | 4 |
This means that for a string containing five +------------------------------------------------+
two-byte characters, LENGTH() returns 10, 1rowinset (0.00 sec)
whereas CHAR_LENGTH() returns 5.
2 CONCAT(str1,str2,...)
Returns the string that results from mysql>SELECTCONCAT('My','S','QL');
concatenating the arguments. May have +------------------------------------------------+
one or more arguments. Ifall | CONCAT('My','S','QL') |
argumentsarenon- +------------------------------------------------+
binarystrings,theresultis a non-binary | MySQL |
string. If the arguments include any binary +------------------------------------------------+
strings,theresultis abinary string.Anumeric 1rowinset (0.00 sec)
argument is converted to its equivalent
binary
stringform;ifyouwanttoavoidthat,youcanu
se
anexplicittypecast,asinthis example:
3 BIT_LENGTH(str) mysql>SELECTBIT_LENGTH('text');
Returnsthelengthofthestringstrinbits. +------------------------------------------------+
| BIT_LENGTH('text') |
+ +
| 32 |
+ +
1rowinset (0.00 sec)
4 CONCAT_WS(separator,str1,str2,...) mysql>SELECTCONCAT_WS(',','Firstname','Last
CONCAT_WS() stands for Concatenate Name'
With Separator and is a special form of );
CONCAT(). The first argument is the +------------------------------------------------+
separator for the rest of the arguments. | CONCAT_WS(',','Firstname','LastName')
The separator is added between the |
strings to be concatenated. The separator +------------------------------------------------+
can be a string, as can the rest of the | Firstname,LastName |
arguments. If the separator is NULL, the +------------------------------------------------+
result is NULL. 1rowinset (0.00 sec)
5 FORMAT(X,D) mysql>SELECTFORMAT(12332.123456,4);
Formats the number X to a format like +------------------------------------------------+
'#,###,###.##', rounded to D decimal | FORMAT(12332.123456, 4) |
places, and +------------------------------------------------+
returnstheresultasastring.IfDis0,theresulth | 12,332.1235 |
as no decimal point or fractional part. + +
1rowinset (0.00 sec)
6 INSERT(str,pos,len,newstr) mysql>SELECTINSERT('Quadratic',3,4,'What');
Returns the string str, with the substring +------------------------------------------------+
beginning at | INSERT('Quadratic',3,4,'What') |
positionposandlencharacterslongreplaced +------------------------------------------------+
by the string newstr. Returns the original | QuWhattic |
string if pos is not within the length of the +------------------------------------------------+
string. Replaces the rest of the string from 1rowinset (0.00 sec)
position pos if len is not within the length
of the rest of the string. Returns NULL if
any argument is NULL.
7 INSTR(str,substr) mysql>SELECTINSTR('foobarbar','bar');
Returns the position of the first +------------------------------------------------+
occurrence |of INSTR('foobarbar','bar') |
substringsubstrinstringstr.Thisisthesameas+------------------------------------------------+
the two-argument form of LOCATE(), | 4 |
except that the order of the arguments is + +
reversed. 1rowinset (0.00 sec)
8 LOWER(str) mysql>SELECTLOWER('QUADRATICALLY');
Returnsthestringstrwithallcharacterschang +------------------------------------------------+
edto lowercase according to the current | LOWER('QUADRATICALLY') |
character set mapping. +------------------------------------------------+
| quadratically |
+ +
1rowinset (0.00 sec)
9 LTRIM(str) mysql>SELECTLTRIM('barbar');
Returnsthestringstrwithleadingspacecharac +------------------------------------------------+
ters removed. | LTRIM('barbar') |
+ +
| barbar |
+ +
1rowinset (0.00 sec)
DateandTimeFunctions
1 CURDATE() mysql>SELECTCURDATE();
+ +
Returns the current date as a value in | CURDATE() |
'YYYY-MM- DD' or YYYYMMDD format, +----------------------------------------------------------+
depending on whether the function is | 1997-12-15 |
used in a string or numeric context. + +
1rowinset(0.00sec)
mysql>SELECTCURDATE()+ 0;
+ +
| CURDATE()+0 |
+ +
| 19971215 |
+ +
1rowinset(0.00sec)
2 CURTIME() mysql>SELECTCURTIME();
+ +
Returns the current time as a value in | CURTIME() |
'HH:MM:SS' or HHMMSS format, +----------------------------------------------------------+
depending on whether the function is | 23:50:26 |
usedin a string or numeric context.The + +
value is expressed in the current time 1rowinset(0.00sec)
zone.
mysql>SELECTCURTIME()+0;
+ +
| CURTIME()+0 |
+ +
| 235026 |
+ +
1rowinset(0.00sec)
3 DATE(expr) mysql>SELECTDATE('2003-12-3101:02:03');
+ +
Extractsthedatepartofthedateordatetime | DATE('2003-12-3101:02:03')
expression expr. |
+ +
| 2003-12-31 |
+ +
1rowinset(0.00sec)
4 DATEDIFF(expr1,expr2) mysql>SELECTDATEDIFF('1997-12-31
23:59:59','1997-12-30');
DATEDIFF() returns expr1 . expr2 +----------------------------------------------------------+
expressed as a | DATEDIFF('199712-3123:59:59','1997-
valueindaysfromonedatetotheother.expr1 12-30') |
and expr2 are date or date-and-time +----------------------------------------------------------+
expressions. Only the date parts of the | 1 |
values are used in the calculation. + +
1rowinset(0.00sec)
METADATA
ObtainingDatabaseMetadata
MySQLprovidesseveralwaystoobtaindatabasemetadata—thatis,informationaboutdatabasesandthe
objects in them:
SHOWstatementssuchasSHOWDATABASESorSHOWTABLES
TablesintheINFORMATION_SCHEMAdatabase
Command-lineprogramssuchasmysqlshowormysqldump
Thefollowingsectionsdescribehowtouseeachoftheseinformationsourcestoaccessmetadata.
ObtainingMetadatawithSHOW
MySQL provides a SHOWstatement that displays many types of database metadata. SHOWis helpful
for
keepingtrackofthecontentsofyourdatabasesandremindingyourselfaboutthestructureofyourtables.The
following examples demonstrate a few uses for SHOWstatements.
Listthedatabasesyoucanaccess:
SHOW DATABASES;
DisplaytheCREATEDATABASEstatementforadatabase:
SHOWCREATEDATABASEdb_name;
Listthetablesinthedefaultdatabaseoragivendatabase:
SHOWTABLES;
SHOWTABLESFROMdb_name;
SHOWTABLESdoesn’tshowTEMPORARYtables. Display the
CREATE TABLEstatement for a table:
SHOWCREATETABLEtbl_name;
Displayinformationaboutcolumnsorindexesinatable:
SHOWCOLUMNSFROMtbl_name; SHOW
INDEX FROM tbl_name;
TheDESCRIBEtbl_nameandEXPLAINtbl_namestatementsaresynonymouswithSHOW COLUMNS
FROMtbl_name.
Displaydescriptiveinformationabouttablesinthedefaultdatabaseorinagivendatabase:
SHOWTABLESTATUS;
SHOWTABLESTATUSFROMdb_name;
SeveralformsoftheSHOWstatementtakea LIKE'pattern'clausepermittingapatterntobegiventhat limits
the scope of the output. MySQL interprets 'pattern' as an SQL pattern that may include the ‘%’ and ‘_’
wildcard characters. For example, this statement displays the names of columns in the studenttable
that begin with ‘s’:
mysql>SHOWCOLUMNSFROMstudentLIKE's%';
+------------+------------------+------+-----+---------+-----------------+
+------------+------------------+------+-----+---------+-----------------+
|student_id|int(10)unsigned|NO|PRI|NULL |auto_increment|
+------------+------------------+------+-----+---------+---------------------------------------+
MySQL:Sequences(AUTO_INCREMENT)
This MySQL tutorial explains how to create sequences using the AUTO_INCREMENT attribute in MySQL
with syntax and examples.
Description
In MySQL, you can create a column that contains a sequence of numbers (1, 2, 3, and so on) by using
the AUTO_INCREMENT attribute. The AUTO_INCREMENT attribute is used when you need to create a
unique number to act as a primary key in a table.
Syntax
Thesyntaxtocreateasequence(orusetheAUTO_INCREMENTattribute)inMySQLis:
CREATETABLEtable_name (
AUTO_INCREMENT
TheattributetousewhenyouwantMySQLtoassignasequenceofnumbersautomaticallytoafield (in essence,
column1datatypeNOTNULLAUTO_INCREMENT,
creating an autonumber field). column2 datatype
NULL orNOT NULL
[ NULL | NOT NULL ],
Each column should be defined as NULL or NOT NULL. If this parameter is omitted, the database
assumes ...NULL as the default.
);
Note
You can use the LAST_INSERT_IDfunction to find last value assigned by the AUTO_INCREMENTfield.
Example
Let'slookatanexampleofhowto useasequenceortheAUTO_INCREMENTattributeinMySQL. For example:
CREATETABLE contacts
(contact_idINT(11)NOTNULL AUTO_INCREMENT,
last_nameVARCHAR(30)NOTNULL, first_name
VARCHAR(25),
This MySQL AUTO_INCREMENT example creates a table called contacts which has 4 columns and one
primary key:
The first column is called contact_id which is created as an INT datatype (maximum 11 digits in length)
and can not contain NULL values. It is set as an AUTO_INCREMENT field which means that it is an
autonumber field (starting at 1, and incrementing by 1, unless otherwise specified.)
The second column is called last_name which is a VARCHAR datatype (maximum 30 characters in
length) and can not contain NULL values.
The third column is called first_name which is a VARCHAR datatype (maximum 25 characters in length)
and can contain NULL values.
ThefourthcolumniscalledbirthdaywhichisaDATEdatatypeandcancontainNULL values.
Theprimarykeyiscalled contacts_pkandissettothe contact_idcolumn.
SetAUTO_INCREMENTstartingvalue
Now that you've created a table using the AUTO_INCREMENT attribute, how can you change the
starting value for the AUTO_INCREMENT field if you don't want to start at 1?
You can use the ALTER TABLE statement to change or set the next value assigned by the
AUTO_INCREMENT.
Syntax
InMySQL,thesyntaxtochangethestartingvalueforanAUTO_INCREMENTcolumnusingtheALTER TABLE
statement is:
ALTERTABLEtable_nameAUTO_INCREMENT= start_value;
table_name
ThenameofthetablewhoseAUTO_INCREMENT valueyou wish to change.Sinceatablein MySQL can only
contain one AUTO_INCREMENT column, you are only required to specify the table name that contains
the sequence. You do not need to specify the name of the column that contains the AUTO_INCREMENT
value.
start_value
ThenextvalueinthesequencetoassignintheAUTO_INCREMENT column.
Example
Let'slookatanexampleofhowtochangethestartingvaluefortheAUTO_INCREMENTcolumnina table in
MySQL.
Forexample:
ALTERTABLEcontactsAUTO_INCREMENT=50;
ThisMySQLAUTO_INCREMENTexamplewouldchangethenextvalueintheAUTO_INCREMENTfield (ie: next
value in the sequence) to 50 for the contact_id field in the contacts table.
PYTHON
FactsAboutPython
Featuresof Python
PYTHONInstallation:
PythonsourcecodeisavailableundertheGNUGeneralPublicLicense(GPL).
Python GUI
TherearevariousGUIbasedPythonIDEthatpythonprogrammerscanuse for better coding experience.
NamesofsomePythoninterpreters are:
PyCharm
PythonIDLE
ThePythonBundle
pyGUI
SublimeTextetc.
Interpreter:
BasicsofPYTHON
Pythonisaninterpretedprogramminglanguage.Pythonsourcecodeis
compiledtobytecodeasa .py file,andthisbytecodecanbeinterpreted.
TherearetwomodesforusingthePythoninterpreter:
1. InteractiveMode
2. ScriptMode
InteractiveMode
Without passing python script file to the interpreter, directly execute code to Python prompt.
Example:
>>>2+6
Output:
8
Thechevronatthebeginningofthe1stline,i.e.,thesymbol>>>isaprompt the python interpreter uses to
indicate that it is ready. If the programmer types 2+6, the interpreter replies 8.
ScriptMode
Alternatively, programmers can store Python script source code in a file with the .py extension, and use
the interpreter to execute the contents of the file. To execute the script by the interpreter, you have to
tell the interpreter the name ofthefile. Forexample, ifyouhaveascriptname MyFile.pyandyou're working
on Unix, to run the script you have to type:
pythonMyFile.py
Working with the interactive mode is better when Python programmers deal
withsmallpiecesofcodeasyoucantypeandexecutethemimmediately, but when the code is more than 2-4
lines, using the script for coding can help to modify and use the code in future.
Python-VariableTypes
Variables are nothing but reserved memory locations to store values. This means that when you create
a variable you reserve some space in memory.
Based on the data type of a variable, the interpreter allocates memory and decideswhatcanbestored
inthereserved memory.Therefore,byassigning different data types to variables, you can store integers,
decimals or characters in these variables.
AssigningValuestoVariables
Python variables do not need explicit declaration to reserve memory space.
Thedeclarationhappensautomaticallywhenyouassignavaluetoavariable. The equal sign (=) is used to
assign values to variables.
The operandtotheleft ofthe= operatoristhename ofthevariableandthe operandtotherightofthe=
operatoristhevaluestoredinthevariable.For example −
Here,100,1000.0and"John"arethevaluesassignedtocounter,
#!/usr/bin/python miles,and
namevariables,respectively.Thisproducesthefollowingresult−
counter = 100 #Anintegerassignment miles
100
MultipleAssignment
= 1000.0 # A floating point
1000.0
Pythonallowsyoutoassignasinglevaluetoseveralvariablessimultaneously.
Forexample−
name = "John" #Astring
Here,anintegerobjectiscreatedwiththevalue1,andallthreevariablesare
a =b=c=1 assigned to the same memory
print counter
location. You can also assign multiple objects to multiple variables. For example −
a,b,c=1,2,"john"
printmiles
Here,twointegerobjectswithvalues1and2areassignedtovariablesaand b respectively, and one string
object with the value "john" isassigned to the variable c.
StandardDataTypes
The data stored in memory can be of many types. For example, a person's age is stored as a numeric
value and his or her address is stored as alphanumeric characters. Python has various standard data
types that are used to define the operations possible on them and the storage method for each of
them.
Pythonhasfivestandarddatatypes−
o Numbers
o String
o List
o Tuple
o Dictionary
PythonNumbers
Number data types store numeric values. Number objects are created when you assign a value to
them. For example –
var1=1
Youcanalsodeletethereferencetoanumberobjectbyusingthedel statement. The syntax of the del statement is –
var2=10
delvar1[,var2[,var3[................,varN]]]]
Youcandeleteasingleobjectormultipleobjectsbyusingthedelstatement.
Forexample−
Pythonsupportsfourdifferentnumericaltypes−
delvar
delvar_a,var_b
int(signedintegers)
long(longintegers,theycanalsoberepresentedinoctaland hexadecimal)
float(floatingpointrealvalues)
complex(complexnumbers)
Examples
Herearesomeexamplesofnumbers−
PythonStrings
Strings in Python are identified as a contiguous set of characters represented in the quotation marks. Python
allows for either pairs of single or double quotes.
Subsetsofstringscanbetakenusingthesliceoperator([]and[:])withindexes starting at 0 in the beginning of the
string and working their way from -1 at the end.
Theplus(+)signisthestringconcatenationoperatorandtheasterisk(*)isthe
repetitionoperator.Forexample−
#!/usr/bin/python str='HelloWorld!'
print str#Printscompletestring
print str[0]#Prints firstcharacterofthe string
print str[2:5]#Printscharactersstartingfrom3rdto5th
print str[2:] #Printsstringstartingfrom3rd character
print str*2 #Printsstringtwo times
printstr+"TEST"#Printsconcatenatedstring
Thiswillproducethefollowingresult−
HelloWorld!
PythonLists
ListsarethemostversatileofPython'scompounddatatypes.Alistcontainsitems
H separated by commasand
enclosed within squarebrackets([]). To someextent, lists are similar to arrays in C. One difference between
them isllo
that all the items belonging to a list can be of different data type.
The values stored in a list can be accessed using the slice operator ([ ] and [:])
withindexesstartingat0inthebeginningofthelistandworkingtheirwaytoend
lloWorld!
-1.Theplus(+)signisthelistconcatenationoperator,andtheasterisk(*)isthe
repetitionoperator.Forexample−
Thisproducethefollowingresult−
#!/usr/bin/python
['abcd',786,2.23,'john',70.200000000000003]
list=['abcd',786,2.23,'john',70.2] tinylist = [123,
abcd
[786, 2.23]
'john']
print list[1:3] # Prints elements starting from 2nd till 3rd print
PythonTuples
Thisproducethefollowingresult−
#!/usr/bin/python
Thefollowingcodeisinvalidwithtuple,becauseweattemptedtoupdateatuple,
('abcd',786,2.23,'john',70.200000000000003) which is not allowed. Similar case is
possible with lists –
abcd
tuple=('abcd',786,2.23,'john',70.2)
(786, 2.23) tinytuple = (123,
'john')
(2.23,'john', 70.200000000000003)
print tuple[1:3] # Prints elements starting from 2nd till 3rd print
list[2]=1000 #Validsyntaxwithlist
PythonDictionary
Python'sdictionariesarekindofhashtabletype.Theyworklikeassociativearrays or hashes found in Perl and
consist of key-value pairs. A dictionary key can be
almostanyPythontype,butareusuallynumbersorstrings.Values,ontheother hand, can be any arbitrary Python
object.
Dictionariesareenclosedbycurlybraces({})andvaluescanbeassignedand
accessedusingsquarebraces([]).Forexample−
#!/usr/bin/python dict = {}
dict['one']= "Thisis one"
tinydict={'name':'john','code':6734,'dept':'sales'}
print tinydict.keys()#Printsallthekeys
printtinydict.values()#Printsallthevalues
Thisproducethefollowingresult−
Thisisone
This is two
Python-BasicOperators
Operatorsaretheconstructswhichcanmanipulatethevalueof operands.
Consider the expression 4 + 5 = 9. Here, 4 and 5 are called operandsand+ is called operator.
TypesofOperator
Pythonlanguagesupportsthefollowingtypesofoperators.
o ArithmeticOperators
o Comparison(Relational)Operators
o AssignmentOperators
o LogicalOperators
o BitwiseOperators
o MembershipOperators
o IdentityOperators
Python-DecisionMaking
Itisthepredictionofconditionsthatoccurwhileexecutingaprogramtospecifyactions.
MultipleexpressionsgetevaluatedwithanoutcomeofeitherTRUEorFALSE.Theseare logicaldecisions, and Python
alsoprovides decision-making statements that tomake
decisionswithinaprogramforanapplicationbasedontheuserrequirement.
Pythonprovidesvarioustypesofconditionalstatements:
Statement Description
if Statements ItconsistsofaBooleanexpressionwhichresultsiseitherTRUEorFALSE
followed by one or more statements.
if Statement
Thedecision-makingstructurescanberecognizedandunderstoodusing flowcharts.
Figure-IfconditionFlowchart:
Syntax:
ifexpression:
Example:
a=15
ifa>10:
Output:
aisgreater
ifelseStatements
Figure-IfelseconditionFlowchart:
Syntax:
if expression:
#executeyourcode
else:
Example:
a=15
b=20
ifa>b:
print("aisgreater") else:
Output:
bis greater
elifStatements
if expression:
#executeyourcode
elif expression:
#executeyourcode
else:
#executeyourcode
Example:
a=15
b=15
ifa>b:
print("aisgreater")
elif a == b:
print("bothareequal") else:
print("bisgreater")
Output:
bothareequal
SingleStatementCondition
If the block of an executable statement of if -clause contains only a single line, programmers can write it on the
same line as a header statement.
Example:
a=15
if(a==15):print("Thevalueofais15")
PHYTHONLOOPS:
In programming, loops are a sequence of instructions that does a specific set of instructions or tasks
based on some conditions and continue the tasks until it reaches certain conditions.
It is seen that in programming, sometimes we need to write a set of instructions repeatedly -which is a
tedious task and the processing also takes time. So in programming, we use iteration technique to
repeat the same or similar type of tasks based on the specified condition.
Statements are executed sequentially, but there sometimes occur such cases where programmers need
to execute a block of code several times. The control
structuresofprogramminglanguagesallowustoexecuteastatementorblockof statement repeatedly.
TypesofLoopsinPython
Pythonprovidesthreetypesofloopingtechniques:
Loop Description
whileLoop The loop gets repeated until the specific Boolean condition is met.
NestedLoops Programmers can use one loop inside another; i.e., they can use for loop
inside while or vice -versa or for loop inside for loop or while inside while.
forLoop
Thegraphicalrepresentationofthelogicbehindforloopingisshown below:
Figure-forloop Flowchart:
Syntax:
foriterating_varinsequence:
Example01:
forxinrange(0,3):
Output:
Loopexecution0
Loopexecution1
Example02:
forletterin'TutorialsCloud':
Output:
Currentletteris:T Currentletteris:u Current letter is : t Currentletteris:o Current letteris: r Current letter is : i Currentletteris:a Current lett
Currentletteris:s Currentletteris:C Current letter is : l Currentletteris:o Currentletteris:u
Currentletteris:d
whileLoop
Thegraphicalrepresentationofthelogicbehindwhileloopingisshown below:
Figure-whileloopFlowchart:
Syntax:
whileexpression:
Example:
#initializecountvariableto1 count =1
whilecount<6: print
(count) count+=1
#theabovelinemeanscount=count+1
Output:
1
2
3
NestedLoops
Syntax:
foriterating_varinsequence:
#executeyourcode
Example:
forkinrange(1,3):
print("%d*%d=%d"%(g,k,g*k))
Output:
1*1=1
1*2=2
2*1=2
2*2=4
3*1=3
3*2=6
LoopControlStatements
ControlStatements Description
Breakstatement
Syntax:
break
Example:
count=0
whilecount<=100:print(count)count+=1ifcount>=3: break
Output:
Continuestatement
Syntax:
continue
Example:
forxin range(10):
#checkwhetherxiseven if x % 2
== 0:
continue
Output:
PassStatement
Syntax:
pass
Example:
forletterin'TutorialsCloud': if
letter == 'C':
pass
Output:
Currentletteris:T
Currentletteris:u Current
letter is : t
Currentletteris:o Current
letteris: r Current letter
is : i Currentletteris:a
Current letter is : l
Currentletteris:s Pass
block
Currentletteris:C Current
letter is : l
Currentletteris:o
Currentletteris:u
PYTHONFUCNTIONS:
Afunctionisablockoforganized,reusablecodethat isusedtoperform asingle,related
action.Functionsprovidebettermodularityforyourapplicationandahighdegreeofcode reusing.
As you already know, Python gives you many built-in functions like print(), etc. but you can also create your
own functions. These functions are called user-defined functions.
DefiningaFunction
You can define functions to provide the required functionality. Here are simple rules to define a function in
Python.
Function blocks begin with the keyword def followed by the function name and parentheses ( ( ) ).
Any input parameters or arguments should be placed within these parentheses. You can also define
parameters inside these parentheses.
The first statement of a function can be an optional statement - the documentationstring of the function or
docstring.
Thecodeblockwithineveryfunctionstartswithacolon(:)andisindented.
The statement return [expression] exits a function, optionally passing back an expression to the caller. A
return statement with no arguments is the same as return None.
Syntax
By default, parameters have a positional behavior and you need to inform them in thesame order
deffunctionname(parameters):
that they were defined.
Example "function_docstring"
function_suite
Thefollowingfunctiontakesastringasinputparameterandprintsitonstandardscreen.
CallingaFunction
defprintme(str ):
Definingafunctiononlygivesitaname,specifiestheparametersthataretobeincluded in the function and
"Thisprintsapassedstringintothisfunction"
structures the blocks of code.
print str structure of a function is finalized, you can execute it by calling it from another
Once the basic
function or directly from the Python prompt. Following is the example to call printme() function −
Whentheabovecodeisexecuted,itproducesthefollowingresult−
I'mfirstcalltouserdefinedfunction!
Again second call to the same
#!/usr/bin/python
#Functiondefinitionishere
def printme( str ):
"Thisprintsapassedstringintothisfunction"
print str
return;
Passbyreferencevsvalue
All parameters (arguments) in thePython languagearepassed by reference. It means if
youchangewhataparameterreferstowithinafunction,thechangealsoreflectsbackin the calling function. For
example −
Here,wearemaintainingreferenceofthepassedobjectandappendingvaluesinthe
sameobject.So,thiswouldproducethefollowingresult−
#!/usr/bin/python
Thereisonemoreexamplewhereargumentisbeingpassedbyreferenceandthe reference is being overwritten inside
Valuesinsidethefunction:[10, 20,30,[1,2,3,4]]
the called function.
#Functiondefinitionishere
def changeme( mylist ):
Valuesoutsidethefunction:[10,20,30,[1,2,3,4]]
#!/usr/bin/python
"Thischangesapassedlistintothisfunction"mylist.append(
#Functiondefinitionishere
[1,2,3,4]);
def changeme( mylist ):
print"Valuesinsidethefunction:",mylist
"Thischangesapassedlistintothisfunction"
return
mylist=[1,2,3,4];#Thiswouldassignewreferenceinmylist
print "Values inside the function: ", mylist
return
#Nowyoucancallchangemefunction
The parameter mylist is local to the function changeme. Changing mylist within the function does not affect
mylist. The function accomplishes nothing and finally this would produce the following result −
FunctionArguments
Valuesinsidethefunction:[1,2,3,4]
Youcancallafunctionbyusingthefollowingtypesofformalarguments−
Valuesoutsidethefunction:[10,20,30]
o Requiredarguments
o Keywordarguments
o Defaultarguments
o Variable-lengtharguments
Requiredarguments
Required arguments are the arguments passed to a function in correct positional order.
Here,thenumberofargumentsinthefunctioncallshouldmatchexactlywiththefunction definition.
Tocallthefunctionprintme(),youdefinitelyneedtopassoneargument,otherwiseit
givesasyntaxerrorasfollows−
Whentheabovecodeisexecuted,itproducesthefollowingresult−
#!/usr/bin/python
#Functiondefinitionishere
Keywordarguments
Traceback(mostrecentcalllast):
def printme(
Keyword str are
arguments ): related to the function calls. When you use keyword arguments in a
File"test.py",line11,in<module>printme
function call, the caller identifies the arguments by the parameter name.
"Thisprintsapassedstringintothisfunction"
();
This allows you to skip arguments or place them out of order because the Python interpreter is able
print str
to use the keywords provided to match the values with parameters. You can also make keyword
calls to the printme() function in the following ways −
Whentheabovecodeisexecuted,itproducesthefollowingresult−
The following example gives more clear picture. Note that the order of parameters does not matter.
#!/usr/bin/python
My string
#!/usr/bin/python
#Functiondefinitionishere
#Functiondefinitionishere def printinfo( name, age ):
"Thisprintsapassedinfointothisfunction"
def printme( str ): print "Name: ", name
print"Age",age return;
"Thisprintsapassedstringintothisfunction"
print str
#Nowyoucancallprintinfofunction printinfo( age=50, name="miki" )
Whentheabovecodeisexecuted,itproducesthefollowingresult−
Name:mik
i Age50
Defaultarguments
Adefaultargumentisanargumentthatassumesadefaultvalueifavalueisnotprovided in the function call for that
argument. The following example gives an idea on default arguments, it prints default age if it is not passed
−
Whentheabovecodeisexecuted,itproducesthefollowingresult−
Name:mik
#!/usr/bin/python
i Age50
Name:mik
Variable-lengtharguments
i
# Age35
Function definition is
Youmayneedtoprocessafunctionformoreargumentsthanyouspecifiedwhiledefining the function. These
hereare called variable-length arguments and are not named in the function definition, unlike
arguments
requireddefprintinfo(name,age=35):
and default arguments.
Syntaxforafunctionwithnon-keywordvariableargumentsisthis−
"Thisprintsapassedinfointothisfunction"
print "Name: ", name
Anasterisk(*)isplacedbeforethevariablenamethatholdsthevaluesofallnonkeyword variable arguments. This
deffunctionname([formal_args,]*var_args_tuple):
tuple remains empty if no additional arguments are specified during the function call. Following is a simple
print"Age",age
"function_docstring"
example − return;
#!/usr/bin/python
function_suite
#Functiondefinitionishere
defprintinfo(arg1,*vartuple):
"Thisprintsavariablepassedarguments" print "Output is: "
printarg1
forvarinvartuple: print var
return;
#Nowyoucancallprintinfofunction printinfo( 10 )
printinfo(70,60,50)
Whentheabovecodeisexecuted,itproducesthefollowingresult−
Outputis
: 10
Outputis
: 70
TheAnonymousFunctions
These functions are called anonymous because they are not declared in the standard manner by using the
def keyword. You can use the lambda keyword to create small anonymous functions.
Lambdaformscantakeanynumberofargumentsbutreturnjustonevalueintheform of an expression. They
cannot contain commands or multiple expressions.
An anonymous function cannot be a direct call to print because lambda requires an expression
Lambda functions have their own local namespace and cannot access variables other than those in their
parameter list and those in the global namespace.
Although it appears that lambda's are a one-line version of a function, they are not equivalent to inline
statements in C or C++, whose purpose is by passing function stack allocation during invocation for
performance reasons.
Syntax
Thesyntaxoflambdafunctionscontainsonlyasinglestatement,whichisasfollows−
Followingistheexampletoshowhowlambdaformoffunctionworks−
lambda[arg1[,arg2,. . . .argn]]:expression
#!/usr/bin/python
Whentheabovecodeisexecuted,itproducesthefollowingresult–
#Functiondefinitionishere
Valueoftotal:30
sum=lambdaarg1,arg2:arg1+ arg2;
Valueoftotal:40
Whentheabovecodeisexecuted,itproducesthefollowingresult−
#!/usr/bin/python
ScopeofVariables
Insidethefunction:30
#Functiondefinitionishere
Allvariablesinaprogrammaynotbeaccessibleatalllocationsinthatprogram.This depends on where you have
declareddef
a sum( arg1, arg2 ):
variable.
Outsidethefunction:30
Thescopeofavariabledeterminestheportionoftheprogramwhereyoucanaccessa
#Addboththeparametersandreturnthem."
particularidentifier.TherearetwobasicscopesofvariablesinPython−
total = arg1 + arg2
o Globalvariables
print"Insidethefunction:",total
o Local variables
return total;
Globalvs.Localvariables
Variables that are defined inside a function body have a local scope, and those defined outside have a global
scope.
Thismeansthatlocalvariablescanbeaccessedonlyinsidethefunctioninwhichtheyare declared, whereas global
variables can be accessed throughout the program body by all
functions.Whenyoucallafunction,thevariablesdeclaredinsideitarebroughtintoscope. Following is a simple
example −
#!/usr/bin/python
total=0;#Thisisglobalvariable. # Function definition is here
defsum(arg1,arg2):
#Addboththeparametersandreturnthem."
total=arg1+arg2;#Heretotalislocalvariable. print "Inside the function local total : ", t
#Nowyoucancallsumfunction sum( 10, 20 );
print"Outsidethefunctionglobaltotal:",total
Whentheabovecodeisexecuted,itproducesthefollowingresult−
Inside the function local total :30
Outsidethefunctionglobaltotal:0
PYTHON–MODULES:
AmoduleallowsyoutologicallyorganizeyourPythoncode.Groupingrelated code into a module makes the
code easier to understand and use. A module is a Python object with arbitrarily named attributes that
you can bind and reference.
Simply, a module is a file consisting of Python code. A module can define functions, classes and
variables. A module can also include runnable code.
Example
ThePythoncodeforamodulenamedanamenormallyresidesinafilenamed
aname.py.Here'sanexampleofasimplemodule, support.py
#Importmodulesupport
import support
Whentheabovecodeisexecuted,itproducesthefollowingresult−
Hello:Zara
Amoduleisloadedonlyonce,regardlessofthenumberoftimesitisimported. This prevents the module
execution from happening over and over again if multiple imports occur.
Thefrom...importStatement
Python'sfromstatementletsyouimportspecificattributesfromamoduleinto the current namespace. The
from...import has the following syntax −
Forexample,toimportthefunctionfibonaccifromthemodulefib,usethe
followingstatement−
frommodnameimportname1[,name2[,... nameN]]
This fromfibimport
statement does not import the entire module fib into the current
fibonacci
namespace;itjustintroducestheitemfibonaccifromthemodulefibintothe global symbol table of the
importing module.
Thefrom...import*Statement
Itisalsopossibletoimportallnamesfromamoduleintothecurrent
namespacebyusingthefollowingimportstatement−
This provides an easy way to import all the items from a module into the current namespace; however,
frommodnameimport*
this statement should be used sparingly.
PYTHON–FILESI/O
PrintingtotheScreen
Thesimplestwaytoproduceoutputisusingtheprintstatementwhereyoucanpasszero or more expressions
separated by commas. This function converts the expressions you pass into a string and writes the result to
standard output as follows −
#!/usr/bin/python
Thisproducesthefollowingresultonyourstandardscreen−
Pythonisreallyagreatlanguage,isn'tit?
ReadingKeyboardInput
Pythonprovidestwobuilt-infunctionstoreadalineoftextfromstandardinput,whichby
defaultcomesfromthekeyboard.Thesefunctionsare−
o raw_input
o input
o
Theraw_inputFunction
The raw_input([prompt]) function reads oneline from standard input and returns it asa string (removing the
trailing newline).
Thispromptsyoutoenteranystringanditwoulddisplaysamestringonthescreen.When
Ityped"HelloPython!",itsoutputislikethis−
#!/usr/bin/python
TheinputFunction
Enteryourinput:Hello Python
Theinput([prompt])functionisequivalenttoraw_input,exceptthatitassumestheinput is a valid Python
str=raw_input("Enteryourinput:");
expression and returns the
Receivedinput evaluated
is:Hello result to you.
Python
Thiswouldproducethefollowingresultagainsttheenteredinput−
Enteryourinput:[x*5forxinrange(2,10,2)]
#!/usr/bin/python
Recieved input is :[10, 20, 30, 40]
OpeningandClosingFiles
str=input("Enteryourinput:");
Untilnow,youhavebeenreadingandwritingtothestandardinputandoutput.Now,we will see how to use actual
data files.
Python provides basic functions and methods necessary to manipulate files by default. You can do most of
the file manipulation using a file object.
TheopenFunction
Before you can read or write a file, you have to open it using Python's built-in open() function. This
functioncreatesa file object,which wouldbe utilized to call other support methods associated with it.
Syntax
fileobject=open(file_name[,access_mode][,buffering])
Hereareparameterdetails−
file_name − The file_name argument is a string valuethat contains thenameof the file that you want to
access.
access_mode − The access_mode determines the mode in which the file has to be
opened,i.e.,read,write,append,etc.Acompletelistofpossiblevaluesisgivenbelow in the table. This is optional
parameter and the default file access mode is read (r).
buffering−Ifthebufferingvalueissetto0,nobufferingtakesplace.Ifthebuffering
valueis1,linebufferingisperformedwhileaccessingafile.Ifyouspecifythebuffering
valueasanintegergreaterthan1,thenbufferingactionisperformedwiththeindicated buffer size. If negative, the
buffer size is the system default(default behavior).
Hereisalistofthedifferentmodesofopeningafile–
1 r
Opensafileforreadingonly.Thefilepointerisplacedatthebeginningofthefile.Thisis the default mode.
2 rb
Opens a file for reading only in binary format. The file pointer is placed at the beginning of the file. This is the
default mode.
3 r+
Opens a file for both reading and writing. The file pointer placed at the beginning of the file.
4 rb+
Opens a file for both reading and writing in binary format. The file pointer placed at the beginning of the file.
5 w
Opensafileforwritingonly.Overwritesthefileifthefileexists.Ifthefiledoesnotexist, creates a new file for writing.
6 wb
Opens a file for writing only in binary format. Overwrites the file if the file exists. If the file does not exist,
creates a new file for writing.
7 w+
Opens a file for both writing and reading. Overwrites the existing file if the file exists. If the file does not
exist, creates a new file for reading and writing.
8 wb+
Opens a file for both writing and reading in binary format. Overwrites the existing file if the file exists. If the
file does not exist, creates a new file for reading and writing.
9 a
Opens a file for appending. The file pointer is at the end of the file if the file exists. That
is,thefileisintheappendmode.Ifthefiledoesnotexist,itcreatesanewfileforwriting.
10 ab
Opens a fileforappending in binary format.Thefilepointer is attheend of thefileif the file exists. That is, the
file is in the append mode. If the file does not exist, it creates a new file for writing.
11 a+
Opensafileforbothappendingandreading.Thefilepointerisattheendofthefileifthe file exists. The file opens in
the append mode. If the file does not exist, it creates a new file for reading and writing.
12 ab+
Opens a file for both appending and reading in binary format. The file pointer is at the end of the file if the
file exists. The file opens in the append mode. If the file does not exist, it creates a new file for reading and
writing.
ThefileObjectAttributes
Onceafileisopenedandyouhaveonefileobject,youcangetvariousinformationrelated to that file.
Hereisalistofallattributesrelatedtofileobject−
file.closed
1 Returnstrueiffileisclosed,falseotherwise.
file.mode
2 Returnsaccessmodewith whichfilewas opened.
file.name
3 Returnsnameofthefile.
file.softspace
4 Returnsfalseifspaceexplicitlyrequiredwithprint,true otherwise.
Example
#!/usr/bin/
python # Open
a file
fo=open("foo.txt","wb")
print"Nameofthefile:",fo.name
print"Closedornot:",fo.closed
Theclose()Method
Nameofthefile:foo.txt
The close() method of a file object flushes any unwritten information and closes the file object, after which no
Closed or not :False
more writing can be done.
Pythonautomaticallyclosesafilewhenthereferenceobjectofafileisreassignedto
Opening mode :wb another file. It is a good practice to
use the close() method to close a file.
Syntax
fileObject.close();
Example
#!/usr/bin/python
#Opena file
fo=open("foo.txt","wb")
print"Nameofthefile:",fo.name
Thisproducesthefollowingresult−
Nameofthefile:foo.txt
ReadingandWritingFiles
The file object provides a set of access methods to make our lives easier. We would see how to use read()
and write() methods to read and write files.
Thewrite()Method
The write() method writes any string to an open file. It is important to note that Python strings can have binary
data and not just text.
Thewrite()methoddoesnotaddanewlinecharacter('\n')totheendofthestring−
Syntax
fileObject.write(string);
Here,passedparameteristhecontenttobewrittenintotheopenedfile.
Example
#!/usr/bin/python
#Opena file
fo=open("foo.txt","wb")
fo.write("Pythonisagreatlanguage.\nYeahitsgreat!!\n");
Theabovemethodwouldcreatefoo.txtfileandwouldwritegivencontentinthatfileand finally it would close that file.
If you would open this file, it would have following content.
Theread()Method
Pythonisagreatlanguage.
The read() method reads a string from an open file. It is important to note that Python strings can have binary
Yeahits
data. apart great!!
from text data.
Syntax
Here, passed parameter is the number of bytes to be read from the opened file. This method starts reading
fileObject.read([count]);
from the beginning of the file and if count is missing, then it tries to read as much as possible, maybe until
the end of file.
Example
Let'stakeafilefoo.txt,whichwecreatedabove.
#!/usr/bin/python
#Opena file
fo=open("foo.txt","r+")
str = fo.read(10);
print"ReadStringis:",str #
Thisproducesthefollowingresult−
ReadStringis :Python is
FilePositions
The tell() method tells you the current position within the file; in other words, the next read or write will
occur at that many bytes from the beginning of the file.
The seek(offset[, from]) method changes the current file position. The offset argument indicates the number
of bytes to be moved. The from argument specifies the reference position from where the bytes are to be
moved.
If from is set to 0, it means use the beginning of the file as the reference position and 1
meansusethecurrentpositionasthereferencepositionandifit isset to2thentheend of the file would be taken as
the reference position.
Example
Letustakeafilefoo.txt,whichwecreatedabove.
#!/usr/bin/python
#Opena file
fo=open("foo.txt","r+") str = fo.read(10);
print"ReadStringis:",str
#Checkcurrentposition position = fo.tell();
print"Currentfileposition:",position
#Repositionpointeratthebeginningonceagain position = fo.seek(0, 0);
str=fo.read(10);
print"AgainreadStringis:",str # Close opend file
fo.close()
Thisproducesthefollowingresult−
ReadStringis:Pythonis
Currentfileposition:10
RenamingandDeletingFiles
Python os module provides methods that help you perform file-processing operations,such as renaming and
deleting files.
Tousethismoduleyouneedtoimportitfirstandthenyoucancallanyrelatedfunctions.
Therename()Method
Therename()methodtakestwoarguments,thecurrentfilenameandthenewfilename.
Syntax
Exampleos.rename(current_file_name,new_file_name)
Followingistheexampletorenameanexistingfiletest1.txt−
Theremove()Method
#!/usr/bin/
You can use the remove() method to delete files by supplying the name of the file to be deleted as the
argument.
python import
Syntax
os
os.remove(file_name)
Example
Followingistheexampletodeleteanexistingfiletest2.txt−
#!/usr/bin/
python import
os
DirectoriesinPython
All files are contained within various directories, and Python has no problem handling
thesetoo.Theosmodulehasseveralmethodsthathelpyoucreate,remove,andchange directories.
Themkdir()Method
You can use the mkdir() method of the os module to create directories in the current
directory.Youneedtosupplyanargumenttothismethodwhichcontainsthenameofthe directory to be created.
Syntax
os.mkdir("newdir")
Example
Followingistheexampletocreateadirectorytestinthecurrentdirectory−
#!/usr/bin/
python import
os
#Createadirectory"test"os.mkd
Thechdir()Method
Youcanusethechdir()methodtochangethecurrentdirectory.Thechdir()methodtakes an argument, which is the
name of the directory that you want to make the current directory.
Syntax
os.chdir("newdir")
Example
Followingistheexampletogointo"/home/newdir"directory−
#!/usr/bin/
python import
os
Thegetcwd()Method
Thegetcwd()methoddisplaysthecurrentworkingdirectory.
Syntax
os.getcwd()
Example
Followingistheexampletogivecurrentdirectory−
#!/usr/bin/
python import
os
Thermdir()Method
Thermdir()methoddeletesthedirectory,whichispassedasanargumentinthemethod. Before removing a directory,
all the contents in it should be removed.
Syntax
Exampleos.rmdir('dirname')
Following is the example to remove "/tmp/test" directory. It is required to give fully
qualifiednameofthedirectory,otherwiseitwouldsearchforthatdirectoryinthecurrent directory.
File&DirectoryRelatedMethods
#!/usr/bin/
Therearethreeimportantsources,whichprovideawiderangeofutilitymethodstohandle and manipulate files &
python
directories importand Unix operating systems. They are as follows −
on Windows
o FileObjectMethods:Thefileobjectprovidesfunctionstomanipulatefiles.
os
o OSObjectMethods:Thisprovidesmethodstoprocessfilesaswellasdirectories.
PHYTHON–EXCEPTIONHANDLING
Sometimes we what to catch some or all errors that can possibly get generated; and as a programmer,
we want to be as specific as possible. So, python allows programmers to deal with errors smoothly.
Exceptions are events that are used to modify the flow of control through a program when the error
occurs. Exceptions get triggered automatically on finding errors in Python.
Theseexceptionsareprocessedusingfivestatements.Theseare:
ThelastwasanoptionalextensiontoPython2.6&Python3.0.
WhyareExceptionsUsed?
Exceptions allow us to jump outof random illogical large chunks of codes in case of errors. Let us take a
scenario that you have given a function to do a specific task. If you go there and found those things
missing that are required to do that specifictask,whatwillyoudo?
Eitheryoustopworkingorthinkaboutasolution
- where to find those items to perform the task. The same thinghappens here in case of
Exceptions also. Exceptions allow programmers jump to an exception
handlerinasinglestep,abandoningallfunctioncalls.Youcanthinkexceptionsto an optimized quality go-to
statement, in which the program error that occurs at runtime gets easily managed by the exception
block. When the interpreter encounters an error, it lets the execution go to the exception part to solve
and continue the execution instead of stopping.
While dealing with exceptions, the exception handler creates a mark & executes some code.
Somewhere further within that program the exception is raised that solves the problem & makes
Python jump back to the marked location; by not
throwingaway/skippinganyactivefunctionsthatwerecalledafterthemarkerwas left.
RolesofanExceptionHandlerinPython
Error handling: The exceptions get raised whenever Python detects an error in a program at runtime. As
a programmer, if you don't want the default behavior then code a 'try' statement to catch and recover
the program from anexception.Pythonwilljumptothe'try'handlerwhentheprogramdetects an error the
execution will be resumed.
Event Notification: Exceptions are also used to signal suitable conditions & then passing result flags
around a program & text them explicitly.
Terminate Execution: There may arise some problems or errors in programs
thatitneedsatermination.Sotry/finallyisusedthatguaranteesthatclosing- time operation will be
performed. The 'with' statement offers an alternative for objects that support it.
Exotic flow of Control: Programmers can also use exceptions as a basis for implementing unusual
control flow. Since there is no 'go to' statement in Python so exceptions can help in this respect.
ASimpleProgramtoDemonstratePythonExceptionHandling
Example01:
(a,b)= (6,0)
try:#simpleuseoftry-exceptblockforhandlingerrors g
= a/b
Output:
ThisisaDIVIDEDBYZEROerror
Theaboveprogramcanalsobewrittenlikethis: Example 02:
(a,b)= (6,0)
try:
g=a/b
exceptZeroDivisionErrorass: k =
s
print (k)
#Outputwillbe:integerdivisionormoduloby zero
Output:
divisionbyzero
The'try-Except'ClausewithNoException
Thestructureofsuchtypeof'except'clausehavingnoexceptionisshownwithan example.
try:
#alloperationsaredonewithinthisblock.
. . .. ..
except:
#thisblockwillgetexecutedifanyexception encounters.
. . .. ..
else:
#thisblockwillgetexecutedifnoexceptionis found.
Alltheexceptionsgetcaughtwherethereistry/exceptthestatementofthistype. Good programmers use this
technique of exception to make the program fully executable.
'except'ClausewithMultipleExceptions
try:
#alloperationsaredonewithinthisblock.
. . .. ..
except(Exception1[,Exception2[,….ExceptionN]]]):
#thisblockwillgetexecutedifanyexceptionencountersfromtheabovelistsof exceptions.
. . .. ..
else:
#thisblockwillgetexecutedifnoexceptionis found.
. . .. ..
The'try-Finally'Clause
try:
#alloperationsaredonewithinthisblock.
. . .. ..
#ifanyexceptionencounters,thisblockmaygetskipped. finally:
. . .. ..
#thisblockwilldefinitelybeexecuted.
PHYTHON–OOP
The python is an Object-oriented programming Language. This means there exists a concept called
'class' that let's programmer structure the codes of software in a fashioned way. Because of the use of
classes and objects, the programming became easy to understand and code.
DefiningClassandObject
A class is a technique to group functions and data members and put them in a
containersothattheycanbeaccessedlaterbyusingdot(.)operator.Objectsare
thebasicruntimeentitiesofobject-orientedprogramming.Itdefinestheinstance
ofaclass.Objectsgettheirvariablesandfunctionsfromclassesandtheclasswe will be creating are the
templates made to create the object.
Object-OrientedTerminologies
o class: Classes are auser-defined data type that is used toencapsulate data
andassociatedfunctionstogether.Italsohelpsinbindingdatatogetherinto a single unit.
o DataMember:Avariableormemorylocationnamethatholdsvaluetodoes a specific task within a program.
o MemberFunction:Theyarethefunctions;usuallyablockofacodesnippet that is written to re-use it.
o Instancevariable:Avariablethatisdefinedinsideamethodofaclass.
o Function Overloading: This technique isused to assignmultiple tasks to a single function & the tasks are
performed based on the numberof argument or the type of argument the function has.
o Operator Overloading: It is the technique of assigning multiple function/tasks to a particular operator.
o Inheritance: It is the process of acquiring the properties of one class to another, i.e., one class can
acquire the properties of another.
o Instantiation:Itisthetechniqueofcreatinganinstanceofaclass.
ProgramforClassinPython Example:
classkarl:
varabl =
Anotherprogramtoexplainfunctionsinsideaclass:
'Hello'deffunction(sel Example:
f):
classkarl(object):
print("ThisisamessageHello")
def init (self,x,y):
self.x = x
self.y = y
defsample(self):
print("Thisisjustasamplecode")
In the above code, we created a class name karl using 'class' keyword. And two functionsareused
namelyinit()(forsettingtheinstancevariable)& sample(). Classes are used instead of modules because
programmers can take this class 'karl'& use it or modify it as many times as we want & each one won't
interferewitheachother.Importingamodulebringstheentireprogramintouse.
CreatingObjects(InstanceofAClass)
Let'sseeanexampletoshowhowtocreateanobject:Example:
classstudent:
def init (self,roll,name):
self.r = roll
self.n = name
print((self.n))
#...
Output:
stud1 = student(1, "Alex")
stud2=student(2,"Karlos")
Alex
print("Datasuccessfullystoredinvariables")
Karlo
s
Datasuccessfullystoredinvariables
AccessingObjectVariables
myobject_name.variable_name
Example:
printobject.varabl
AccessingAttributes
Objectattributescanalsobeaccessedbyusingdotoperator. Example:
stud1.display()
stud2.display()
UseofPre-definedFunctions
Instead of using general statements to access attributes, programmers can use the following functions:
o getattr(obj,name[,default]):usedtoaccessobject'sattribute.
o hasattr(object,name):usedforcheckingwhethertheattributeexistsornot.
o setattr(obj,name,value):setorcreateanattribute,ifdoesn'texist.
o delattr(obj,name):usedtodeleteanattribute.
Built-inClassAttributes
All the Pythonbuilt-in class attributes can be accessed using dot(.) operator like other attributes.
Thebuilt-inclassattributesare:
The PHP Hypertext Pre-processor (PHP) is a programming language that allows web developers to create
dynamic content that interacts with databases.
PHPisbasicallyusedfordevelopingwebbasedsoftwareapplications.
PHPIntroduction
PHPisarecursiveacronymfor"PHP:HypertextPreprocessor".
PHP isaserversidescriptinglanguagethat isembeddedinHTML.It isusedtomanage dynamic content,
databases, session tracking, even build entire e-commerce sites.
It is integrated with a number of popular databases, including MySQL, PostgreSQL, Oracle, Sybase,
Informix, and Microsoft SQL Server.
PHP is pleasingly zippy in its execution, especially when compiled as an Apache module on the Unix side. The
MySQL server, once started, executes even very complex queries with huge result sets in record-setting
time.
PHP supports a large number of major protocols such as POP3, IMAP, and LDAP. PHP4 added support for
Java and distributed object architectures (COM and CORBA), making n-tier development a possibility for the
first time.
PHPisforgiving:PHPlanguagetriestobeasforgivingaspossible.
PHPSyntaxisC-Like.
Commonusesof PHP:
PHP performs system functions, i.e. from files on a system it can create, open, read,write, and close them.
PHPcanhandleforms,i.e.gatherdatafromfiles,savedatatoafile,thruemailyoucan send data, return data to the
user.
Youadd,delete,modifyelementswithinyourdatabasethruPHP.
Accesscookiesvariablesandsetcookies.
UsingPHP,youcanrestrictuserstoaccesssomepagesofyourwebsite.
Itcanencrypt data.
CharacteristicsofPHP
FiveimportantcharacteristicsmakePHP'spracticalnaturepossible:
Simplicity
Efficiency
Security
Flexibility
Familiarity
"HelloWorld"ScriptinPHP:
Toget afeel for PHP,first startwithsimple PHP scripts.Since "Hello,World!"is anessential example, first we will
create a friendly little "Hello, World!" script.
1|Page
Asmentionedearlier,PHPisembeddedinHTML.ThatmeansthatinamongstyournormalHTML (or
XHTML if you're cutting-edge) you'll have PHP statements like this:
2|Page
<html>
<head>
<title>HelloWorld</title>
<body>
Itwillproducefollowingresult:
Hello,World!
If you examine the HTML output of the above example, you'll notice that the PHP code is not present in the
file sent from the server to your Web browser. All of the PHP present in the Web page is processed and
stripped from the page; the onlything returned to the client from theWeb server is pure HTML output.
AllPHP code must be included inside one of the three special markup tags ate are recognised by the PHP
Parser.
<?phpPHPcodegoeshere?>
<scriptlanguage="php">PHPcodegoeshere</script>
Mostcommontagisthe<?php...?>andwewillalsousesametaginourtutorial.
From the next chapter we will start with PHP Environment Setup on your machine and then we willdig out
almost allconcepts related to PHP to make you comfortable with thePHP language.
PHPEnvironmentSetup
InordertodevelopandrunPHPWebpagesthreevitalcomponents needtobeinstalledonyour computer system.
Web Server - PHP will work with virtually all Web Server software, including Microsoft's Internet
Information Server (IIS) but then most often used is freely availble Apache Server. Download Apache for free
here: http://httpd.apache.org/download.cgi
Database - PHP will work with virtually all database software, including Oracle and Sybase but most
commonly used is freely available MySQL database. Download MySQL for free here:
http://www.mysql.com/downloads/index.html
PHP Parser - In order to process PHP script instructions a parser must be installed to generate HTML output
that can be sent to the Web Browser. This tutorial will guide you how to install PHP parser on your
computer.
PHPParserInstallation:
Before you proceeditisimportant tomake sure thatyouhave properenvironment setup on your machine to
develop your web programs using PHP.
Typethefollowingaddressintoyourbrowser'saddressbox.
http://127.0.0.1/info.php
3|Page
If this displays a page showing your PHP installation related information then it means you have PHP and
Webserver installed properly. Otherwise you have to follow given procedure to install PHP on your
computer.
ApacheConfiguration:
PHP.INIFileConfiguration:
ThePHPconfigurationfile,php.ini,isthefinalandmostimmediatewaytoaffectPHP'sfunctionality.
WindowsIIS Configuration:
To configure IISon your Windows machine you can refer your IISReference Manual shippedalong with IIS.
PHPSyntaxOverview
This chapter willgive you an idea of very basic syntax of PHP and very important to make your PHP
foundation strong.
EscapingtoPHP:
CanonicalPHPtags:
ThemostuniversallyeffectivePHPtagstyleis:
<?php...?>
Ifyouusethisstyle,youcanbepositivethatyourtagswillalwaysbecorrectlyinterpreted.
Short-open (SGML-style)tags:
Shortorshort-opentagslooklikethis:
<?...?>
Shorttagsare,asonemightexpect,theshortestoptionYoumustdooneoftwothingsto enable PHP to recognize the
tags:
Choosethe--enable-short-tagsconfigurationoptionwhenyou'rebuildingPHP.
Set the short_open_tag setting in your php.ini file to on. This option must be disabled to parse XML with
PHP because the same syntax is used for XML tags.
ASP-styletags:
ASP-style tags mimic the tags used by Active Server Pages to delineate code blocks. ASP-style tags look like
this:
TouseASP-styletags,youwillneedtosettheconfigurationoptioninyourphp.inifile.
<%...%>
4|Page
HTMLscripttags:
HTMLscripttagslooklikethis:
CommentingPHPCode:
<scriptlanguage="PHP">...</script>
Acommentis the portion of a program that exists only for the human reader and stripped out before
displaying the programs result. There are two commenting formats in PHP:
<?
Multi-linesprinting:Herearetheexamplestoprintmultiplelinesinasingleprintstatement:
#Thisisacomment,and
<?
#Thisisthesecondlineofthecomment
Multi-lines comments: They are generally used to provide pseudocode algorithms and more detailed
explanations#FirstExample
when necessary. The multiline style of commenting is the same as in C. Here are the example
of multi linesprint
comments.
<<<END
END;
?>
PHPiswhitespaceinsensitive:
Whitespace is the stuff you type that is typically invisible on the screen, including spaces, tabs, and carriage
returns (end-of-line characters).
5|Page
Forexample,eachofthefollowing PHPstatementsthatassignsthesumof2+2tothevariable
$fourisequivalent:
PHPiscasesensitive:
$four=2+2;//singlespaces
YeahitistruethatPHPisacasesensitivelanguage.Tryoutfollowingexample:
$four<tab>=<tab2<tab>+<tab>2;//spacesandtabs
<html> $four=
<body>
<?
$capital=67;
print("Variablecapitalis$capital<br>");
Thiswillproducefollowingresult:
Variablecapitalis67
Variable CaPiTaL is
Statementsareexpressionsterminatedbysemicolons:
A statement in PHP is any expression that is followed by a semicolon (;).Any sequence of valid PHP
statements that is enclosed by the PHP tags is a valid PHP program. Here is a typical
statementinPHP,whichinthiscaseassignsastringofcharacterstoavariablecalled
$greeting:
Expressionsarecombinationsoftokens:
$greeting="WelcometoPHP!";
The smallest building blocks of PHP are the indivisible tokens, such as numbers (3.14159), strings (.two.),
variables ($two), constants (TRUE), and the special words that make up the syntax of PHP itself like if, else,
while, for and so forth
Bracesmakeblocks:
Although statements cannot be combined like expressions, you can always put a sequence of statements
anywhere a statement can go by enclosing them in a set of curly braces.
Herebothstatementsareequivalent:
if(3==2+1)
print("Good-Ihaven'ttotallylostmymind.<br>");
if(3==2+1)
6|Page
RunningPHPScriptfromCommandPrompt:
<?php
Nowrunthisscriptascommandpromptasfollows:
echo"HelloPHP!!!!!";
$php test.php
Itwillproducefollowingresult:
HelloPHP!!!!!
HopenowyouhavebasicknowledgeofPHPSyntax.
PHPVariableTypes
ThemainwaytostoreinformationinthemiddleofaPHPprogramisbyusingavariable. Here are the most
important things to know about variables in PHP.
AllvariablesinPHParedenotedwithaleadingdollarsign($).
Thevalueofavariableisthevalueofitsmostrecentassignment.
Variables are assigned with the = operator, with the variable on the left-hand side and the expression to be
evaluated on the right.
Variablescan,butdonotneed,tobedeclaredbeforeassignment.
VariablesinPHPdonothaveintrinsictypes-avariabledoesnotknowinadvance whether it will be used to store a
number or a string of characters.
Variablesusedbeforetheyareassignedhavedefaultvalues.
PHPdoesagoodjobofautomaticallyconvertingtypesfromonetoanotherwhen necessary.
PHPvariablesarePerl-like.
PHPhasatotalofeightdatatypeswhichweusetoconstructourvariables:
Integers:arewholenumbers,withoutadecimalpoint,like4195.
Doubles:arefloating-pointnumbers,like3.14159or49.1.
Booleans:haveonlytwopossiblevalueseithertrueorfalse.
NULL:isaspecialtypethatonlyhasonevalue:NULL.
Strings:aresequencesofcharacters,like'PHPsupportsstringoperations.'
Arrays:arenamedandindexedcollectionsofothervalues.
Objects:areinstancesofprogrammer-definedclasses,whichcanpackageupboth other kinds of values and
functions that are specific to the class.
Resources:arespecialvariablesthatholdreferencestoresourcesexternaltoPHP (such as database
connections).
The first five are simple types, and the next two (arrays and objects) are compound - the compound types
can package up other arbitrary values of arbitrary type, whereas the simple types cannot.
We will explain only simile data type in this chapters. Array and Objects will be explained separately.
7|Page
Integers:
They are whole numbers, without a decimal point, like 4195. They are the simplest type .they correspond to
simple whole numbers, both positive and negative. Integers can be assigned to variables, or they can be
used in expressions, like so:
$int_var=12345;
Integer can be in decimal (base 10), octal (base 8), and hexadecimal (base 16) format. Decimal format is the
default, octal integers are specified with a leading 0, and hexadecimals have a leading 0x.
For most common platforms, the largest integer is (2**31 . 1) (or 2,147,483,647), and the smallest (most
negative) integer is . (2**31 . 1) (or .2,147,483,647).
Doubles:
Theylike 3.14159or 49.1.Bydefault,doublesprintwiththeminimumnumberofdecimal places needed. For
example, the code:
$many=2.2888800;
Itproducesthefollowingbrowseroutput:
$many_2=2.2111200;
2.28888+2.21112=4.5
Boolean:
Theyhaveonlytwopossiblevalueseithertrueorfalse.PHPprovidesacoupleofconstants especially for use as
Booleans: TRUE and FALSE, which can be used like so:
if (TRUE)
InterpretingothertypesasBooleans:
print("Thiswillalwaysprint<br>");
Herearetherulesfordeterminethe"truth"ofanyvaluenotalreadyoftheBooleantype:
print("Thiswillneverprint<br>");
Ifthevalueisanumber,itisfalseifexactlyequaltozeroandtrueotherwise.
If the value is a string, it is false if the string is empty (has zero characters) or is the string "0", and is true
otherwise.
ValuesoftypeNULLarealwaysfalse.
If the value is an array, it is false if it contains no other values, and it is true otherwise. For an object,
containing a value means having a member variable that has been assigned a value.
Valid resources are true (although some functions thatreturn resources when they are successful will
return FALSE when unsuccessful).
Don'tusedoubleasBooleans.
Each of the following variables has the truth value embedded in its name when it is used in a Boolean
context.
8|Page
$true_num=3+0.14159;
$true_str="Triedandtrue"
$true_array[49]="Anarrayelement";
$false_array=array();
NULL:
NULL isa specialtypethatonly has onevalue: NULL. Togive a variablethe NULL value, simply assign it like this:
The special constant NULL is capitalized by convention, but actually it is case insensitive; you could just as
$my_var=NULL;
well have typed:
$my_var=null;
AvariablethathasbeenassignedNULLhasthefollowingproperties:
ItevaluatestoFALSEinaBooleancontext.
ItreturnsFALSEwhentestedwithIsSet()function.
Strings:
Theyaresequencesofcharacters,like"PHPsupportsstringoperations".Followingarevalid examples of string
$string_1="Thisisastringindoublequotes";
Singlyquotedstringsaretreatedalmostliterally,whereasdoublyquotedstringsreplace variables with their values as
well as specially interpreting certain character sequences.
$string_2="Thisisasomewhatlonger,singlyquotedstring";
<?
Thiswillproducefollowingresult:
$variable="name";
My$variablewillnotprint!\n My
name will print
$literally='My$variablewillnotprint!\\n';
print($literally);
Therearenoartificiallimitsonstringlength-withintheboundsofavailablememory,youought to be able to make
arbitrarily long strings.
Certaincharactersequencesbeginningwithbackslash(\)arereplacedwithspecial characters
Variablenames(startingwith$)arereplacedwithstringrepresentationsoftheir values.
9|Page
Theescape-sequencereplacementsare:
\nisreplacedbythenewlinecharacter
\risreplacedbythecarriage-returncharacter
\tisreplacedbythetabcharacter
\$isreplacedbythedollarsignitself ($)
\"isreplacedbyasingledouble-quote(")
\\isreplacedbyasinglebackslash (\)
HereDocument:
Youcanassignmultiplelinestoasinglestringvariableusingheredocument:
<?php
$channel=<<<_XML_
<channel>
<title>What'sForDinner<title>
<description>Choosewhattoeattonight.</description>
</channel>
_XML_;
echo<<<END
This uses the "here document" syntax to output multiple lines with variable interpolation. Not
<br/>
END;
print$channel;
?>
Thiswillproducefollowingresult:
VariableScope:
This uses the "here document" syntax to output
multiple lines with variable interpolation. Note
Scope can bethattheheredocumentterminatormustappearona
defined asthe rangeof availability avariable hastothe programinwhichitis declared. PHP variables
line
can be one ofwith
four just
scopeatypes:
semicolon. no extra whitespace!
Localvariables
<channel>
Functionparameters
Globalvariables
Staticvariables
10|Page
VariableNaming:
Rulesfornamingavariableis:
Variablenamesmustbeginwithaletterorunderscorecharacter.
Avariablenamecanconsistofnumbers,letters,underscoresbutyoucannotuse characters like + , - , % , ( , ) . & ,
etc
Thereisnosizelimitforvariables.
PHPConstants
A constant is a name or an identifier for a simple value. A constant value cannot change during the
execution of the script. By default a constant is case-sensitive. By convention, constant identifiers
are always uppercase. A constant name starts with a letter or underscore, followed by any number
of letters, numbers, or underscores. If you have defined a constant, it can never be changed or
undefined.
To define a constant you have to use define() function and to retrieve the value of a constant, you
have to simply specifying its name. Unlike with variables, you do not need to have
aconstantwitha$.Youcan alsousethefunctionconstant()toreadaconstant'svalueifyou wish to obtain
the constant's name dynamically.
constant()function:
Asindicatedbythename,thisfunctionwillreturnthevalueoftheconstant.
This is useful when you want to retrieve value of a constant, but you do not know its name, i.e.It is
stored in a variable or returned by a function.
constant()example:
<?php
Onlyscalardata(boolean,integer,floatandstring)canbecontainedinconstants.
define("MINSIZE",50)
Differencesbetweenconstantsandvariablesare:
;
There is no need to write a dollar sign ($) before a constant, where as in Variable one has to write a dollar
echoMINSIZE;
sign.
Constantscannotbedefinedbysimpleassignment, theymayonlybedefinedusing the define() function.
Constants may be defined and accessed anywhere without regard to variable scoping rules.
OncetheConstantshavebeenset,maynotberedefinedorundefined.
11|Page
Validandinvalidconstantnames:
PHPprovidesalargenumberofpredefinedconstantstoanyscriptwhichitruns.
There are five magical constants that change depending on where they are used. For example,
thevalueofLINEdependsonthelinethatit'susedoninyourscript.Thesespecial constants are case-
insensitive and are as follows:
Afew"magical"PHPconstantsategivenbelow:
Name Description
LINE Thecurrentlinenumberofthefile.
FUNCTION Thefunctionname.(AddedinPHP4.3.0)AsofPHP5thisconstantreturns
thefunctionnameasitwasdeclared(case-sensitive).InPHP4itsvalueis
alwayslowercased.
CLASS Theclassname.(AddedinPHP4.3.0)AsofPHP5thisconstantreturnsthe
classnameas it was declared(case-sensitive). In PHP4its valueisalways
lowercased.
PHPOperatorTypes
What is Operator? Simple answer can be given using expression 4 + 5 is equal to 9. Here 4and 5 are
called operands and + is called operator. PHP language supports following type of operators.
ArithmeticOperators
ComparisionOperators
Logical(orRelational)Operators
AssignmentOperators
Conditional (or ternary) Operators
Letshavealookonalloperatorsonebyone.
12|Page
ArithmaticOperators:
TherearefollowingarithmaticoperatorssupportedbyPHPlanguage: Assume
variable A holds 10 and variable B holds 20 then:
Operator Description Example
- Subtractssecondoperandfromthefirst A-Bwillgive-10
* Multiplybothoperands A*Bwillgive200
/ Dividenumeratorbydenumerator B/Awillgive2
% ModulusOperatorandremainderof B%Awillgive0
after an integer division
++ Incrementoperator,increasesinteger A++willgive11
value by one
-- Decrementoperator,decreasesinteger A--willgive9
value by one
ComparisonOperators:
TherearefollowingcomparisonoperatorssupportedbyPHPlanguage Assume
variable A holds 10 and variable B holds 20 then:
Operator Description Example
== Checksifthevalueoftwooperandsare (A==B)isnottrue.
equalornot,ifyesthencondition
becomes true.
!= Checksifthevalueoftwooperandsare (A !=B)istrue.
equal or not, if values are not equal
then condition becomes true.
13|Page
>= Checksifthevalueofleftoperandis (A>=B)isnottrue.
greaterthanorequaltothevalueof right
operand, if yes then condition
becomes true.
LogicalOperators:
14|Page
AssignmentOperators:
TherearefollowingassignmentoperatorssupportedbyPHPlanguage:
= Simpleassignmentoperator, C=A+BwillassignevalueofA+BintoC
Assigns values from right
side operands to left side
operand
+= Add AND assignment operator, C+=AisequivalenttoC=C+A
It addsright operandto theleft
operandandassigntheresultto
left operand
/= DivideANDassignmentoperator, C/=AisequivalenttoC=C/A
It divides left operand with the
right operand and assign the
result to left operand
ConditionalOperator
There is one more operator called conditional operator. This first evaluates an expression for a true or false
value and then execute one of the two given statements depending upon the result of the evaluation. The
conditional operator has this syntax:
?: ConditionalExpression IfConditionistrue?ThenvalueX:
Otherwise value Y
15|Page
OperatorsCategories:
Alltheoperatorswehavediscussedabovecanbecategorisedintofollowingcategories:
Unaryprefixoperators,whichprecedeasingleoperand.
Binaryoperators,whichtaketwooperandsandperformavarietyofarithmeticand logical operations.
Theconditionaloperator(aternaryoperator),whichtakesthreeoperandsandevaluates
eitherthesecondorthirdexpression,dependingontheevaluationofthefirstexpression.
Assignmentoperators,whichassignavaluetoavariable.
PrecedenceofPHPOperators:
Operator precedence determines the grouping of terms in an expression. This affects how an
expressionisevaluated.Certainoperatorshave higherprecedencethanothers;forexample, the multiplication
operator has higher precedence than the addition operator:
For example x = 7 + 3 * 2; Here x is assigned 13, not 20 because operator * has higher precedenace than +
so it first get multiplied with 3*2 and then adds into 7.
Here operators with the highest precedenceappear at the top of the table, those with the lowest appear at
the bottom. Within an expression, higher precedence operators will be evaluated first.
Conditional ?: Righttoleft
Assignment =+=-=*=/=
PHPDecisionMaking
Theif,elseif...elseandswitchstatementsareusedtotakedecisionbasedonthedifferent condition.
16|Page
if...elsestatement-usethisstatementifyouwanttoexecuteasetofcodewhena condition is true and another
if the condition is not true
elseifstatement-isusedwiththeif...elsestatementtoexecuteasetofcode if one of several condition
are true
switch statement - is used if you want to select one of many blocks of code to be
executed,usetheSwitchstatement.Theswitchstatementisusedtoavoidlongblocks of if..elseif..else code.
TheIf...ElseStatement
If you want to execute some code if a condition is true and another code if a condition is false, use the if
.........................else statement.
Syntax
if (condition)
Example
codetobeexecutedifconditionistrue;
Thefollowingexamplewilloutput"Haveaniceweekend!" ifthecurrentdayisFriday,otherwise it will output
"Have a nice day!":
<html>
Ifmorethanonelineshouldbeexecutedifaconditionistrue/false,thelinesshouldbe enclosed within curly braces:
<body>
TheElseIfStatement
<html>
<?php
Ifyouwanttoexecutesomecodeifoneofseveralconditionsaretrueusetheelseifstatement
$d=date("D");
<body>
if ($d=="Fri")
<?php
echo"Haveaniceweekend!";
$d=date("D");
if ($d=="Fri")
17|Page
Syntax
if (condition)
Example
codetobeexecutedifconditionistrue;
Thefollowingexamplewilloutput"Haveaniceweekend!"ifthecurrentdayisFriday,and"Have a nice Sunday!" if
elseif(condition)
the current day is Sunday. Otherwise it willoutput "Have a nice day!":
TheSwitchStatement
<html>
Ifyouwanttoselectoneofmanyblocksofcodetobeexecuted,usetheSwitchstatement.
<body> The switch
statement is <?php
used to avoid long blocks of if..elseif..else code.
Syntax
$d=date("D");
switch(expression)
Example if ($d=="Fri")
{
The switch statement works in an unusual way. First it evaluates given expression then seeks a lable
echo"Haveaniceweekend!";
caselabel1:
elseif ($d=="Sun")
tomatch theresultingvalue. Ifa matchingvalue isfoundthenthe code associatedwith the matching label will be
executed or if none of the lables match then statement will execute any specified default code.
codetobeexecutedifexpression=label1;
break;
caselabel2
:
codetobeexecutedifexpression=label2;
18|Page
<html>
<body>
<?php
$d=date("D"); switch ($d)
{
case"Mon":
echo"TodayisMonday"; break;
case"Tue":
echo"TodayisTuesday"; break;
case"Wed":
echo"TodayisWednesday"; break;
case"Thu":
echo"TodayisThursday"; break;
case"Fri":
echo"TodayisFriday"; break;
case"Sat":
echo"TodayisSaturday"; break;
case"Sun":
echo"TodayisSunday"; break;
default:
echo"Wonderwhichdayisthis?";
}
?>
</body>
</html>
PHPLoopTypes
LoopsinPHPareusedtoexecutethesameblockofcodeaspecifiednumberoftimes.PHP supports following four loop
types.
for-loopsthroughablockofcodeaspecifiednumberoftimes.
while-loopsthroughablockofcodeifandaslongasaspecifiedconditionistrue.
do...while-loopsthroughablockofcodeonce,andthenrepeatstheloopaslongasa special condition is trur.
foreach-loopsthroughablockofcodeforeachelementinanarray.
Wewilldiscussaboutcontinueandbreakkeywordsusedtocontroltheloopsexecution.
Theforloopstatement
The for statement is used when you knowhow many times you want to executea statement or a block of
statements.
Syntax
for(initialization;condition;increment)
The initializer is used to set the start value for the counter of the number of loop iterations. A variable may
{
be declared here for this purpose and it is traditional to name it $i.
19|Page
Example
<html>
Thiswillproducefollowingresult:
<body>
Attheendoftheloopa=50and b=25
<?php
Thewhileloopstatement
Thewhilestatementwillexecuteablockofcodeifandaslongasatestexpressionistrue.
$a=0;
Syntax {
Example while(condition)
Thisexampledecrementsavariablevalueoneachiterationoftheloopandthecounter
{
increments until it reaches 10
when the evaluation is false and the loop ends.
<html>
<body>
<?php
$i=0;
$num=50;
while($i<10)
20|Page
Thiswillproducefollowingresult:
Loopstoppedati=1andnum= 40
Thedo...whileloopstatement
The do...while statement will execute a block of code at least once - it then will repeat the loop as long as a
condition is true.
Syntax
Example do
{
Thefollowingexamplewillincrementthevalueofiatleastonce,anditwillcontinue incrementing the variable i as long
as it has a value of less than 10:
<html>
Thiswillproducefollowingresult:
<body>
Loopstoppedati=10
<?php
Theforeachloopstatement
The foreach $i=0;
statement is used to loop through arrays. For each pass the value of the current array element
is assigned to $value and the array pointer is moved by one and in the next pass next element will be
processed. $num=0;
do
Syntax
{
Example foreach(arrayasvalue)
{
Tryoutfollowingexampletolistoutthevaluesofanarray.
codetobe executed;
21|Page
<html>
<body>
<?php
$array=array(1,2,3,4,5);
foreach( $array as $value )
Thiswillproducefollowingresult:
Valueis1
Thebreakstatement
ThePHPbreakkeywordisusedtoterminatetheexecutionofaloopprematurely.
Valueis2
Valueis3is situated inside the statement block. If gives you full control and whenever you want
The break statement
to exit from the loop you can come out. After coming out of a loopimmediate statement to the loop will be
executed.
Example
<html>
Thiswillproducefollowingresult:
<body>
Loopstoppedati=3
Thecontinuestatement
<?php
ThePHP continue keywordisusedtohaltthecurrentiterationofaloopbutitdoesnot terminate the loop.
$i=0;
while($i<10)
22|Page
Just like the break statement the continue statement is situated inside the statement block containing the
code that the loop executes, preceded by a conditional test. For the pass encountering continue statement,
rest of the loop code is skipped and next pass starts.
Example
In the following example loop prints the value of array but for which condition becomes true it just skip the
code and next value is printed.
<html>
<body>
<?php
$array=array(1,2,3,4,5); foreach( $array as $value )
{
if( $value == 3 )continue; echo"Valueis$value<br/>";
}
?>
</body>
</html>
Thiswillproducefollowingresult
Valueis1
PHPArrays
An array is aValueis2
data structure that stores one or more similar type of values in a single value. For example if
you want to store 100 numbers then instead of defining 100 variables its easy to define an array of 100
length.
There are three different kind of arrays and each array value is accessed using an ID c which is called array
index.
NumericArray
Thesearrays can store numbers, strings and any object but their index will be prepresented by numbers. By
default array index starts from zero.
Example
Followingistheexampleshowinghowtocreateandaccessnumericarrays.
23|Page
Herewehaveusedarray()functiontocreatearray.Thisfunctionisexplainedinfunction reference.
<html>
<body>
<?php
/*Firstmethodtocreatearray.*/
$numbers=array(1,2,3,4,5); foreach( $numbers as $value )
{
echo"Valueis$value<br/>";
}
/*Secondmethodtocreatearray.*/
$numbers[0]="one";
$numbers[1]="two";
$numbers[2]="three";
$numbers[3]="four";
$numbers[4]="five";
foreach($numbersas$value)
{
echo"Valueis$value<br/>";
}
?>
</body>
</html>
Thiswillproducefollowingresult:
AssociativeArrays
Valueis1
The associative arrays are very similar to numeric arrays in term of functionality but they are different in
Valueis2
terms of their index. Associative array will have their index as string so that you can establish a strong
Valueis3
association between key and values.
Valueis4
To store the salaries of employeesin an array, a numericallyindexed array would not be the best choice.
Instead, we could
Value use
is the
5 employees names as the keys in our associative array, and the value would be
their respective salary.
Value is one
NOTE: Don't keep associative array inside double quote while printing otheriwse it would not return any
value.
Example
24|Page
<html>
<body>
<?php
/*Firstmethodtoassociatecreatearray.*/
$salaries=array(
"mohammad"=>2000,
"qadir"=>1000,
"zara"=>500
);
/*Secondmethodtocreatearray.*/
$salaries['mohammad']="high";
$salaries['qadir']="medium";
$salaries['zara']="low";
echo"Salaryofmohammadis".$salaries['mohammad']."<br/>"; echo "Salary of qadir is ".$salaries['qadir']
echo"Salaryofzarais".$salaries['zara']."<br/>";
?>
</body>
</html>
Thiswillproducefollowingresult:
MultidimensionalArrays
Salaryofmohammadis2000
Salary of qadir is 1000
A multi-dimensional array
Salary of eachiselement
zara 500 in the main array can also be an array. And each element in the
sub-array can be an array, and so
Salaryofmohammadishigh on. Values in the multi-dimensional array are accessed using multiple
index. Salary of qadir is medium
Salary of zara is low
Example
In this example we create a two dimensional array to store marks of three students in three subjects:
Thisexampleisanassociativearray,youcancreatenumericarrayinthesamefashion.
25|Page
<html>
<body>
<?php
$marks=array(
"mohammad"=>array
(
"physics"=>35,
"maths"=>30,
"chemistry"=>39
),
"qadir"=>array
(
"physics"=>30,
"maths"=>32,
"chemistry"=>29
),
"zara"=>array
Thiswillproducefollowingresult:
Marksformohammadinphysics:35
PHPStrings Marks for qadir in maths : 32
Theyaresequencesofcharacters,like"PHPsupportsstringoperations".
Marks for zara in chemistry : 39 Following are valid
examples of string
$string_1="Thisisastringindoublequotes";
Singlyquotedstringsaretreatedalmostliterally,whereasdoublyquotedstringsreplace variables with their values as
well as specially interpreting certain character sequences.
$string_2="Thisisasomewhatlonger,singlyquotedstring";
<?
$variable="name";
$literally='My$variablewillnotprint!\\n';
print($literally);
26|Page
Thiswillproducefollowingresult:
My$variablewillnotprint!\n My
Therearenoartificiallimitsonstringlength-withintheboundsofavailablememory,youought
name will print to be able to make
arbitrarily long strings.
Certaincharactersequencesbeginningwithbackslash(\)arereplacedwithspecial characters
Variablenames(startingwith$)arereplacedwithstringrepresentationsoftheir values.
Theescape-sequencereplacementsare:
\nisreplacedbythenewlinecharacter
\risreplacedbythecarriage-returncharacter
\tisreplacedbythetabcharacter
\$isreplacedbythedollarsignitself ($)
\"isreplacedbyasingledouble-quote(")
\\isreplacedbyasinglebackslash (\)
StringConcatenationOperator
Toconcatenatetwostringvariablestogether,usethedot(.)operator:
<?php
Thiswillproducefollowingresult:
$string1="HelloWorld";
HelloWorld1234
$string2="1234";
Ifwelookatthecodeaboveyouseethatweusedtheconcatenationoperatortwotimes.Thisis because we had to insert
a third string.
Between thetwostring variables weadded a string with a single character, an empty space, to separate the two
variables.
Usingthestrlen()function
<?php
echostrlen("Helloworld!");
27|Page
Thiswillproducefollowingresult:
12
The length of a string is often used in loops or other functions, when it is important to knowwhen the string
ends. (i.e. in a loop, we would want to stop the loop after the last character in the string)
Usingthestrpos()function
Thestrpos()functionisusedtosearchforastringorcharacterwithinastring.
If a match is found in the string, this function will return the position of the first match. If no match is found,
it will return FALSE.
Let'sseeifwecanfindthestring"world"inourstring:
Thiswillproducefollowingresult:
<?php
echostrpos("Helloworld!","world");
6
As you see the positionof the string "world"in our string is position6.The reason that it is 6, and not 7, is
that the first position in the string is 0, and not 1.
PHPFileInclusion
You can include the content of a PHP file into another PHP file before the server executes it. Thereare two
PHP functionswhich can be used to included one PHP file into another PHP file.
Theinclude()Function
Therequire()Function
Theinclude()Function
The include() function takes all the text in a specified file and copies it into the file that uses the include
function.Ifthere is anyprobleminloading afilethenthe include() functiongenerates a warning but the script
will continue execution.
Assume you want to create a common menu for your website. Then create a file menu.php with the
following content.
<ahref="http://www.tutorialspoint.com/index.htm">Home</a>-
Now create as many pages as you like and include this file to create header. For example now your test.php
file can have <ahref="http://www.tutorialspoint.com/ebxml">ebXML</a>-
following content.
28|Page
<html>
<body>
<?phpinclude("menu.php");?>
Thiswillproducefollowingresult
Therequire()Function
Home-ebXML-AJAX-PERL
Therequire()functiontakesallthetext inaspecifiedfileandcopiesitintothefilethatusesthe
include function.Ifthere is anyprobleminloading afilethenthe require() functiongenerates a fatal error and
halt the excution of the script.
So there is no difference in require() and include() except they handle error conditions. It is recommended
to use the require() function instead of include(), because scripts should not continue executing if files are
missing or misnamed.
You can try using above example with require() function and it will generate same result. But if you willtry
following two examples where file does not exist then you will get different results.
<html>
Thiswillproducefollowingresult
<body>
ThisisanexampletoshowhowtoincludewrongPHPfile!
<?phpinclude("xxmenu.php");?>
Nowletstrysameexamplewithrequire()function.
<html>
Thistimefileexecutionhaltsandnothingisdisplayed.
<body>
NOTE:Youmaygetplainwarningmessagesorfatalerrormessagesornothingatall.This depends on your PHP Server
<?phprequire("xxmenu.php");?>
configuration.
29|Page
PHPFiles&I/O
Thischapterwillexplainfollowingfunctionsrelatedtofiles:
Openinga file
Readinga file
Writingafile
Closinga file
OpeningandClosingFiles
The PHPfopen()function is used to open a file. It requires two arguments stating first the file name and
then mode in which to operate.
Filesmodescanbespecifiedasoneofthesixoptionsinthistable.
Mode Purpose
r Opensthefileforreadingonly.
Placesthefilepointeratthebeginningofthefile.
r+ Opensthefileforreadingandwriting.
Placesthefilepointeratthebeginningofthefile.
w Opensthefileforwritingonly.
Places the file pointer at the beginning of the file.
andtruncatesthefiletozerolength.Iffilesdoesnot
exist then it attemts to create a file.
a Opensthefileforwritingonly.
Placesthefilepointerattheendofthefile.
Iffilesdoesnotexistthenitattemtstocreateafile.
a+ Opensthefileforreadingandwritingonly.
Placesthefilepointerattheendofthefile.
Iffilesdoesnotexistthenitattemtstocreateafile.
If an attempt to open a file fails then fopen returns a value of false otherwise it returns a file pointer which is
used for further reading or writing to that file.
After making a changes to the opened file it is important to close it with the fclose() function. Thefclose()
function requires a file pointer as its argument and then returns true when the closure succeeds or false if it
fails.
Readingafile
Once a file is opened usingfopen() function it can be read with a function calledfread(). This function
requires two arguments. These must be the file pointer and the length of the file expressed in bytes.
30|Page
The files's length can be found using the filesize() function which takes the file name as its argument and
returns the size of the file expressed in bytes.
Soherearethestepsrequiredtoreadafilewith PHP.
Openafileusingfopen()function.
Getthefile'slengthusingfilesize()function.
Readthefile'scontentusingfread()function.
Closethefilewithfclose()function.
The following example assigns the content of a text file to a variable then displays thosecontents on the web
page.
<html>
<head>
<title>ReadingafileusingPHP</title>
</head>
<body>
<?php
$filename="/home/user/guest/tmp.txt";
$file=fopen($filename,"r"); if( $file == false )
{
echo("Errorinopeningfile"); exit();
}
$filesize=filesize($filename);
$filetext=fread($file,$filesize); fclose( $file );
echo("Filesize:$filesizebytes"); echo ( "<pre>$text</pre>" );
?>
</body>
</html>
Writingafile
A new file can be written or text can be appended to an existing file using the PHP fwrite()function. This
function requires two arguments specifying afile pointerand thestring of data that is to be written.
Optionally a third intger argument can be included to specifythe length of the data to write. If the third
argument is included, writing would will stop after the specified length has been reached.
The following example creates a new text file then writes a short text heading insite it. After
closingthisfileitsexistenceisconfirmedusing file_exist() functionwhichtakesfilenameas an argument
31|Page
<?php
$filename="/home/user/guest/newfile.txt";
$file=fopen($filename,"w"); if( $file == false )
{
echo("Errorinopeningnewfile"); exit();
}
fwrite($file,"Thisisasimpletest\n"); fclose( $file );
?>
<html>
<head>
<title>WritingafileusingPHP</title>
</head>
<body>
<?php
if(file_exist($filename))
{
$filesize=filesize($filename);
$msg="Filecreatedwithname$filename";
$msg.="containing$filesizebytes"; echo ($msg );
}
else
{
echo("File$filenamedoesnotexit");
}
?>
</body>
</html>
PHPFunctions
PHP functions are similar to other programming languages. A function is a piece of code whichtakes one
more input in the form of parameter and does some processing and returns a value.
You already have seen many functions like fopen() and fread() etc. They are built-in functions but PHP gives
you option to create your own functions as well.
Therearetwopartswhichshouldbecleartoyou:
CreatingaPHPFunction
CallingaPHPFunction
In fact you hardly need to create your own PHP function because there are already more than 1000 of built-
in library functions created for different area and you just need to call them according to your requirement.
CreatingPHPFunction:
Its very easy to create yourown PHP function. Suppose you want to create aPHP function which will simply
write a simple message on your browser when you will call it. Following example creates a function called
writeMessage() and then calls it just after creating it.
Note that while creating a function its name should start with keyword function and all the PHP code should
be put inside { and } braces as shown in the following example below:
32|Page
<html>
<head>
<title>WritingPHPFunction</title>
</head>
<body>
<?php
/*DefiningaPHPFunction*/
functionwriteMessage()
Thiswilldisplayfollowingresult:
PHPFunctionswithParameters:
Youarereallyaniceperson,Haveanicetime!
PHP gives you option to pass your parameters inside a function. You can pass as many as parameters your
like. These parameters work like variables inside your function. Following example takes two integer
parameters and add them together and then print them.
<html>
Thiswilldisplayfollowingresult:
<head>
PassingArgumentsbyReference:
Sumofthetwonumbersis:30
<title>WritingPHPFunctionwithParameters</title>
It is possible</head>
to pass arguments to functions by reference. This means that a reference to the variable is
manipulated by the function rather than a copy of thevariable's value.
<body>
Any changes made to an argument in these cases will change the value of the original variable. You can pass
an argument<?php
by reference by adding an ampersand to the variable name in either the function call or the
function definition.
functionaddFunction($num1,$num2)
Followingexampledepictsboththecases.
{
33|Page
<html>
<head>
<title>PassingArgumentbyReference</title>
</head>
<body>
<?php
functionaddFive($num)
{
$num+= 5;
}
functionaddSix(&$num)
{
$num+= 6;
}
$orignum = 10; addFive(&$orignum);
echo"OriginalValueis$orignum<br/>"; addSix( $orignum );
echo"OriginalValueis$orignum<br/>";
?>
</body>
</html>
Thiswilldisplayfollowingresult:
PHPFunctionsreturningvalue:
OriginalValueis15
OriginalValueis21
A function can return a valueusing thereturnstatement in conjunction with a value or object. return stops
the execution of the function and sends thevalue back to the calling code.
Youcanreturnmorethanonevaluefromafunctionusingreturnarray(1,2,3,4).
Following example takes two integer parameters and add them together and then returns their
sumtothecallingprogram.Notethatreturnkeywordisusedtoreturnavaluefromafunction.
<html>
Thiswilldisplayfollowingresult:
<head>
Returnedvaluefromthefunction:30
<title>WritingPHPFunctionwhichreturnsvalue</title>
</head>
<body>
<?php
functionaddFunction($num1,$num2)
34|Page
SettingDefaultValuesforFunctionParameters:
<html>
<head>
<title>WritingPHPFunctionwhichreturnsvalue</title>
</head>
<body>
<?php
functionprintMe($param=NULL)
{
print$param;
}
printMe("Thisistest"); printMe();
?>
</body>
</html>
Thiswillproducefollowingresult:
DynamicFunctionCalls:
Thisis test
<html>
Thiswilldisplayfollowingresult:
<head>
Hello
<title>DynamicFunctionCalls</title>
</head>
<body>
<?php
functionsayHello()
35|Page
Web Servers
Chapter2.SettingupaWebServer
TableofContents
WampserverandApacheHTTPonWindows.............................................................................................................1
Requirements 1
InstallingWampserver 2
SettingupServerPasswords 3
TestingApplications 5
ApacheTomcatonWindows....................................................................................................................................6
Requirements 6
TomcatSetup 7
TestingApplications 10
LampserverandApacheHTTPonUbuntu15.04.......................................................................................................10
Requirements 10
InstallingApache,PHPandMySQL..............................................................................................................................11
TestingApplications 13
ApacheTomcatonUbuntu15.04............................................................................................................................13
TestingApplications 14
Objectives
Attheendofthisunityouwillbeableto:
installandsetupWampserverandApacheserveronWindows;
installandsetupTomcatServeronWindows;
installandsetupLampserverandApacheserveronUbuntu15.04;and
installandsetupTomcatonUbuntu15.04.
WampserverandApacheHTTPon Windows
In this section, you will learn how to set up a Web Server on a Windows PC. The steps in this section
will illustrate how to use Apache HTTP. The next section will illustrate the setup for Apache Tomcat.
Apache is a popular Web Server that allows users to easily set up their own Web Servers. It has the
advantage of being open-source and hence is free to download. Apache is the basic software needed
to support running of HTML and related content. Additional software, such as Tomcat, can be installed
to complement the Web Server. Tomcat is a server that is meant to run applications written in Java
and JSP (Java Server Pages).
Some popular options for deploying Apache, and optionally PHP and MySQL on Windows are Apache
Lounge, XAMPP and Wampserver. Wampserver was used for this example. WAMP is an acronymthat
stands for “Windows, Apache, MySQL, and PHP”.
Requirements
To illustrate the steps below a Windows 7 64-bit computer was used. The Windows computer was
connectedtoalocalareanetwork(LAN)thathasInternetaccess.YoualsoneedtoknowtheIPaddressofyour
1
Web Servers
computer.YoucanfindyourIPaddressbytyping‘ipconfig’atacommandprompt.Findtheentrylabeled
‘Ethernet adapter Local Area Network’ and take note of the IPv4 address.
It is recommended to disable the Windows Firewall before starting the Web Server setup. The steps
below are for a fresh installation of Wampserver (assumes that Wampserver had not been installed
before).
InstallingWampserver
Download WAMP from http://www.wampserver.com/en/. You will have the option of choosing 64-bit
or 32- bit installation depending on your PC. This example uses the 64-bit installation. Locate the
downloaded Wampserver file and click on it. This will open an installation wizard as shown in Figure 1.
Follow the instruction wizard and leave the default settings as they are. After successful installation you
will get thewindowshown inFigure 2. Leave the ‘launchWampServer 2 now’boxchecked and click
on‘Finish’button(in future you can start WampServer by clicking on your Start menu and clicking on its
menu). On your toolbar,you should now see a ‘W’ shaped icon. On left-clicking this icon, you get the
pop-up management console in Figure 3. Click on ‘Start all services’ and then check the ‘W’ icon on your
toolbar. If the ‘W’ icon is green it means that all services are running. If it is red it means that no
services are running. If it is orange it means that some services are running. If everything was installed
correctly you should see a window such as the one in Figure 4.
Figure1:WelcomewindowtoWAMPsetup Figure2:Finishedinstallation
Figure3:WampServerManagementConsole
2
Web Servers
Figure4:Localhosthomescreen
SettingupServerPasswords
In this step, you will learn how to create passwords for your server and also passwords to protect the
files that you may want to share from your Web Server.
i. SettingupMySQLandPHPAdminPassword
On your local host home screen (Figure 4) click on ‘phpmyadmin’ under ‘Tools. The interface window
opens showing the WAMP configurationpage. Atthe bottomofthispage, a message indicates
thatMySQLisrunning without a password (Figure 5).
Click on users and then check the box next to ‘root localhost’ and then click on ‘Edit Privileges’ (Figure
6). Scroll down to change the password and then click on ‘Go’ to save the changes. If you try to click on
any other menu on the interface, for example on the SQL menu, you will get an error. Let us fix this by
also changing the PHP admin password to match the MySQL password.
OpenWindowsExplorer.NavigatetotheC:\wamp\apps\phpmyadminx.x.x\folder(Figure7).Insidethat
folder open config.inc.php – ideally using Notepad or any other html editor.
Searchfortheline$cfg['blowfish_secret']='';–ifyou’re usingnotepadit mightbeeasiertojustsearchforthe
word ‘blowfish’. Change the line $cfg['blowfish_secret'] = 'abcdef'; to $cfg['blowfish_secret'] =
'mypassphrase'; where mypassphrase is your own password – not the same one that you specified for
root in MySQL.
Nowsearchforthephrase['auth_type']='config'.Change‘config’to‘cookie’.Nowsearchfor
$cfg['Servers'][$i]['password'] = ''; Replace the '' with 'mysql-password'; where mysql-password is
theMySQL password you created earlier.
Figure5:NoPasswordsetforWampServer
3
Web Servers
Figure6:Changingrootuserprivilege
Figure7:Accessconfig.incfolder
Save the changes you have made and exit out of the editor. Go back to your toolbar where wampserver
islocated and click on ‘restart all services’. Refresh localhost on your browser. Click on ‘phpmyadmin’.
You shouldnowbepromptedfora usernameandpassword.Yourusernameis‘root’(since we
didnotchangeitfrom the default one) and your password is the MySQL password that you created.
ii. SettingupapasswordtoaccessfilesstoredintheWebServer
Nowin case you want to share files from your server, you do not want just anyone to be able to access
the files. Soletuspassword-
protectyourfiles.Forexample,assumethatyouwouldliketostoremusicfilesonyour
serverandsharethemwithothers.First,decidewhereyouwouldliketostoreyourmusicfiles.Inthis illustration,
the folder ‘www’ found in C:\wamp is used to store the music directory. We are assuming that you
want your users to be able to access any folders that are stored inside the ‘www’ folder.
Next,usingacommandprompt,accessthebindirectoryintheApachefolder(Figure8).Thentype:
4
Web Servers
htpsswd–c“C:\wamp\my-password-file.txt”username
my-password-file.txt is the file where you create the password to access the ‘www’ folder. username is
the username that you create to access the ‘www’ folder. Pick a username of your choice. Note that
the password file is not created inside the music folder. After you press enter, you will be prompted to
enter and re-enter a password. Create a password of your choice. This is the password that will be
associated with the username you have just created, and the combination will be used to access the
‘www’ folder.
Figure8:AccessingApachefolderviacommandprompt
Now we want to apply the login to your music folder. Open a new file in a plain text editor like Notepad.
Copy and paste the following into it:
AuthTypeBasic
AuthName"Thisisaprivatearea,pleaselogin"
AuthUserFile "c:\wamp\my_password_file.txt"
AuthGroupFile/dev/null require
valid-user
You can replace the message ‘this is a private area, please log in’ with your own security message that
you would like users to see. Save this file in the ‘www’ folder which is our server root folder. There are
two important things to note when saving this file:
1. Savethisfileas.htaccess(includingthedot).
2. If using an html editor such as Notepad put quotation marks around the name, thus “.htaccess”. This
way, it will not be saved as a text file.
Nowwhenyourefreshyourlocalhostonthebrowser,youshouldbepromptedforlogindetails.
Testing Applications
SharingfilesstoredonyourWampServer
If you would like to share the music files that you have stored on your Web Server, simply create a
directory called ‘music’ insider the ‘www’ folder (Figure 9). Put the music files that you would like to
share in the‘music’ folder. Type http://localhost/music/on your address bar and you should be able to
see a listing of your music.
5
Web Servers
Figure9:Musicfoldertobeshared
Take note of your computer’s IP address. Go to your wampserver toolbar, left-clickand click‘Putonline’.
Your server can now be accessed on the Web.
Share the address http://xxx.xxx.xx.xxx/music/with someone else to try on another computer. ‘xxx’
represents the numbers in your IP address. The user will be prompted for the logindetails that yousetup
inthe last section. Note that this may not work if you are sharing the address with someone outside of
your network who is behind a firewall.
<html>
<head>
<title>HTMLTest</title>
</head>
<body>Hellow
orld
</body>
</html>
ApacheTomcatonWindows
ApacheTomcatisaJava-capableHTTPserver,whichisabletoexecutespecialJavaprogramsknownasJava
Servlet and Java Server Pages (JSP). It is also able to execute HTML files just like Apache HTTP.
Requirements
To illustrate the steps below a Windows 7 64-bit computer was used. The Windows computer had
Internet access. Tomcat 8 was used for this installation. You need to have the latest Java JDK version
installed. The recommended JDK version for Tomcat 8 is jdk1.8. Make sure that your computer is
updated with this version. Go to java.com to download the latest version of Java.
The steps below are for a fresh installation of Apache Tomcat (assumes that Tomcat had not been
installed before).
6
Web Servers
TomcatSetup
i. DownloadandInstallTomcat
Go to http://tomcat.apache.org/and download the latest version of Tomcat (Tomcat 9 at the time of
writingthis). Under Download on the left click on ‘Tomcat 9’ and under Binary Distribution click on
‘Core’ and you willsee variouspackages. Clickonthe package applicable to you. For a Windows64-
bitcomputer clickon‘64- bit Windows zip’. Download this file.
Unzip the downloaded file to a directoryof your choice. It is recommended not to unzip to the desktop
as it is a difficult directory to locate from a command prompt. For this illustration a folder named
‘tomcat’ was created under drive D, hence D:/tomcat. The zipped file was extracted to this location. It is
recommended to rename the unzipped file from the default name, for example, rename to
‘apachetomcat’ (Figure 10).
Figure10:Creatingfolderstostoretomcatfiles
ii. CreateanEnvironmentVariable
First, take note of the directory into which JDK was installed. The default is "C:\Program Files\Java\
jdk1.8.0_{xx}", where {xx} is the latest upgrade number. It is important to verify your JDKinstalled
directory before you proceed further. Start the command prompt and type ‘set JAVA_HOMEtocheck if
the environmental variable had been set. If not, you will get the message ‘Environment Variable
JAVA_HOME not set’. IfJAVA_HOMEis set, check if it is set to your JDK installed directorycorrectly (For
example in Figure 11). If not, proceed to set the environmental variable as below.
To set the environment variableJAVA_HOMEin Windows 7: Press "Start" button > Control Panel >
System & Security > System > Advanced system settings > Switch to "Advanced" tab > Environment
Variables > System Variables >"New" (or "Edit" for modification) > In "Variable Name", enter
"JAVA_HOME"> In "Variable Value", enter your JDKinstalled directory(e.g., "c:\ProgramFiles\Java\
jdk1.8.0_51").ClickOK. To verify, restart the command promptand type ‘setJAVA_HOME’. Youshould
nowsee the outputasinFigure 11.
Figure11:CheckingenvironmentalvariablesforJAVA_HOME
iii. ConfigureTomcatServer
The Tomcat configuration files are located in the "conf" sub-directory of your Tomcat installed
directory, e.g. "D:\tomcat\apachetomcat\conf". There are 4 configuration XML files: server.xml,
web.xml, context.xml and tomcat-users.xml.Make a BACKUP of the configuration files before you
proceed.
7
Web Servers
The default TCP port number configured in Tomcat is 8080, you may choose any number between 1024
and 65535,whichisnotusedbyanexistingapplication.Weshallchoose8888inthisarticle. Locatethe following
lines that define the HTTP connector, and change port="8080" to port="8888". Save the file and exit.
<Connectorport="8888"protocol="HTTP/
1.1"connectionTimeout="20000"redirectPort="8443" /
>
EnablingDirectoryListing
Again,useanHTMLtexteditortoopentheconfigurationfile"web.xml",underthe"conf" sub-directoryof
Tomcat installed directory.
<servlet>
<servlet-name>default</servlet-name>
<servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>0</param-value>
</init-param>
<init-param>
<param-name>listings</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
EnablingAutomaticReload
We shall add the attribute reloadable="true" to the <Context> element to enable automatic reload
after code changes. Again, this is handy for test system but not for production, due to the overhead of
detecting changes. Open context.xml and locate the <Context> start element, and change it to
<Context reloadable="true">. Save and exit.
<Contextreloadable="true">
……</Context>
8
Web Servers
Figure12:Startingtomcatfromcommandprompt
A new Tomcat console window appears (look out for the Tomcat's port number (double check that
Tomcat is running on port 8888). Future error messages will be sent to this console. Output messages
from related Java
programsarealsosenttothisconsole.Ifyouwanttoshutdowntheservertype‘shutdown’inplaceof‘startup’.
Start a browser. Issue URL "http://localhost:8888" to access the Tomcat server's welcome page. For
users on the other machines over the net, they have to use the server's IP address or DNS domain
name or hostname in the format of "http://serverHostnameOrIPAddress:8888". Note that this may
not work if you are sharing the address with someone outside of your network who is behind a
firewall. If everything is setup correctly, you should see the screen in Figure 13.
Figure13:Tomcathomescreenonlocalhost
InstallTomcat'sSampleWeb Application
Goto:http://localhost:8888/docs/
Clickthelink:"3.Firstwebapplication"
Click"ExampleApp"undercontentsontheleftsideofthescreen.
Click on the link "here" to download their "Sample Application". (Download
link: http://localhost:8888/docs/appdev/sample/sample.war )
Savetothislocation:C:\Tomcat\apachetomcat\webapps
GivetheTomcatcontaineraminuteanditwillautomaticallyextracttheWARfileandcreateaWebApplication
called "Sample".
Testyourinstall:http://localhost:8888/sample
If you wishto create your owndirectoryunder webapps, choose anamefor your webapp.
Let'scallit"myapps". Go to Tomcat's "webapps" sub-directory. Create the following directory structure
for you webapp "myapps":
1. Under Tomcat's "webapps", create your webapp root directory "myapps"
(i.e., "tomcat\apachetomcat\webapps\myapps").
2. Under "myapps", create a sub-directory "WEB-INF" (case sensitive, a "dash" not an underscore)
9
Web Servers
(i.e.,"tomcat\apachetomcat\webapps\myapps\WEB-INF").
10
Web Servers
Restartyourtomcatservertopickupthechanges.Thenonyourbrowsertypehttp://localhost:8888/myapps/
LampserverandApacheHTTPonUbuntu 15.04
In this section, you will learn how to set up a Web Server on Ubuntu 15.04. The steps in this
section will illustrate howto use Apache HTTP. The next section willillustrate the setup for
Apache Tomcat. LAMP stack is a group of open source software used to get web servers up
and running. The acronym stands for Linux, Apache, MySQL or MariaDB, and PHP. Since I
assume that your computer is already running Ubuntu, the Linux part is taken care of.
Requirements
To illustrate the steps below a 64-bit computer running Ubuntu 15.04 was used. The computer
was connected to a local area network (LAN) with Internet access. You also need to know your
server IP address. You can find out your IP address by right clicking the network icon in the
notification area and clicking Connection Information, or by running ifconfig –a on the
terminal. To log into your server, you will need to know the password for the "root" user's
account.First, run the command belowto update your server, before which you will be
prompted to enter your root password.
11
Web Servers
sudoapt-getupdate
stallingApache,PHPandMySQL
RunthecommandsbelowtoinstallApache2WebServerandwaitforcompletionofinstallation.
sudoapt-getinstallapache2
RunthecommandsbelowtostarttheApache2WebServer.
sudoserviceapache2start
Atthistime,ifyoubrowsetotheserver usingitsIPaddress,you’llsee Apache2defaultpageforUbuntuasin
Figure 14. This is how you also tell the server is up and functioning.
Thenextstep isto installPHPas wellasits moduleto enablePHP appsor web servicesto function.
Thereare
hundredsofPHPmodules,butthesefewwillgetmostwebservicesstarted.ToinstallPHPandothermodules,
run the commands below.
sudoapt-getinstallphp5php5-mysqlphp5-curlphp5-gdphp5-snmpphp5-mcryptphp5-tidy php5-xmlrpc
libapache2-mod-php5
Figure14:SuccessfulApacheInstallation
Afterinstallingtheabove modules, gotoApacherootdirectory.Itcanbefoundat/var/www/htmlinUbuntu.
There create a file called phpinfo.php. Then in that file, add the lines below.
<?phpphpinfo();
?>
Save the file, restart Apache and browser to the server IP address followed by phpinfo.php. (ex.
http://Your IP address/phpinfo.php). There you’ll find PHP information page such as Figure 15. This is
how you also know PHP is functioning.
12
Web Servers
Figure15:SuccessfulPHPInstallation
ThenextstepistoinstallMySQL.Runthecommandsbelow.Waitforcompletion.
sudoapt-getinstallmysql-servermysql-client
Afterinstallingthedatabaseserver,youcanstarteditusingthecommandsbelow.
sudosystemctlstartmysql
Whenit’sstarted,runthecommandsbelowtoconfigurethedatabaseserver.
sudomysql_secure_installation
Whenprompted,followtheoptionsbelowandtype yourrootpassword.
Next, choose Yes for the rest of the prompts until you’re done.
Entercurrentpasswordforroot(enterfornone):Typerootpassword
Changetherootpassword?N
Removeanonymoususers?Y
Disallowrootloginremotely?Y
Removetestdatabaseandaccesstoit?Y
Reloadprivilegetablesnow?Y
RestartApache2andyou’redone.
Atthistime,Apache2,PHP5andothermodulesandMySQLdatabaseservershouldbeinstalledand functioning.
You Ubuntu server is ready for any open source application that supports the LAMP stack.
You may wish to install PhpMyAdmin,which is a web interface through which you can easily
manage/administer your MySQL/MariaDB databases. The installation can be completed with the
following command:
sudoapt-getinstallphpmyadmin
Upon installation you will be asked to select the web server you are using. Select “Apache” and
continue. Next you will be asked if you wish to configure phpmyadmin with dbconfig-common. Select
“Yes”. You need to perform one more step so that you can be able to access phpmyadmin from your
Apache server. Run the following command.
sudoln-s/usr/share/phpmyadmin/var/www/html
13
Web Servers
Ifyounowaccessphpmyadminonyourbrowserthroughhttp://YourIPaddress/phpmyadmin
youshouldsee thesame windowinFigure16 and youcanbeableto login using your MySQL username
and password that you created.
Figure16:Phpadminhomepage
TestingApplications
SharingfilesstoredonyourTomcatServer
IfyouwouldliketosharethemusicfilesthatyouhavestoredonyourWebServer,createadirectorycalled ‘music’
insider the ‘var/www/html’ folder.
Putthemusicfilesthatyouwouldliketoshareinthe‘music’folder.Typehttp://localhost/music/onyour
address bar and you should be able to see a listing of your music.
Testinga‘HelloWorld’ApplicationonTomcatServer
Letustryasimpleexampletotestthatthewebsiteswewillcreatewillworkonthiswebserver.Typethe following
command on the terminal to open the gedit editor.
sudogedit
Inthegediteditor,typethefollowingcodetoprint‘hello world’andsaveisathello.htmlundervar/www/html
Then access this file from your browser using your ip address or localhost/hello.html. You should get
‘hello world’ as output in your browser.
<html>
<head>
<title>HTMLTest</title>
</head>
<body>Helloworld
</body>
</html>
ApacheTomcatonUbuntu15.04
Make sure that Java JDK in installed on your machine. For this example, JDK-7 was installed.First, head-
on- over to the Apache Tomcat 8 Download site. Then, under the heading 8.0.28 (the current version as
ofNovember 2015), or whichever is the newest version at the time you read this chapter, you’ll see
Binary Distributions. Under Binary Distributions you’ll see Core and then under Core, you will see tar.gz.
Right click on tar.gz and copy the URL link location.
14
Web Servers
IntheterminalusetheURLyoucopiedthus:
wgethttp://apache.is.co.za/tomcat/tomcat-8/v8.0.28/bin/apache-tomcat-8.0.28.tar.gz
Afterthedownloadcompletes,decompressthefileusingthefollowingcommand:
tarxvzfapache-tomcat-8.0.28.tar.gz
Now,movethefileintoaproperlocationusingthefollowingcommand.Iammovingittoafoldercalledopt.
mvapache-tomcat-8.0.28/opt
Nowlet’ssettheenvironmentvariablesin.bashrcbytypingthefollowingcommand:
vim~/.bashrc
Once in the vim editor, type ‘i’ in order to enter the insert mode. Add this information to the end of the
file.After thatpressesc onthe keyboardto gobackto normal mode. Nextpress‘:’(the colon) and this
willdrop the cursor to the bottom of the terminal. Here you save your changed by typing ‘w’ and press
enter, and then youcan type ‘q’ to quit vim.
exportJAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64export
CATALINA_HOME=/opt/ apache-tomcat-8.0.28Make the changes
effective by running the following command:
. ~/.bashrc
Tomcat should nowbe installed and configured for your server. To activate Tomcat, run the following
script on the terminal. Note that the commands are case sensitive.
$CATALINA_HOME/bin/startup.sh
You can verify that Tomcat is installed correctly by typing http://localhost:8080 in your browser. You
shouldsee the window indicating successful installation.
TestingApplications
SharingfilesstoredonyourTomcatServer
If you would like to share the music files that you have stored on your Web Server, simply create a
directory called ‘music’ insider the ‘webapps’ folder. Put the music files that you would like to share in
the ‘music’folder. Type http://localhost/music/on your address bar and you should be able to see a
listing of your music.
Testinga‘HelloWorld’ApplicationonTomcatServer
Let us try a simple example to test that the websites we will create will work on this web server. Type
the following command on the terminal to open the gedit editor.
sudogedit
In the gedit editor, type the following code to print ‘hello world’ and save is at hello.html under the
webapps folder.Thenaccessthisfilefromyourbrowserusingyouripaddressorlocalhost/
hello.html.Youshouldgetthe output in Figure 17.
<html>
<head>
<title>HTMLTest</title>
</head>
15
Web Servers
<body>Helloworld
</body>
</html>
Figure17:HelloWorldOutput
16
EclipseIDETutorial
Onceyouselecttheworkspace,theapplicationwilllooksimilartothefollowing screenshot.
Eclipse projects
A perspective is a visual container for a set of parts. Perspectives can be used to store
differentarrangementsofparts.Forexample,theEclipseIDEusesthemtolayouttheviews appropriate to the task
(development, debugging, review, ...) the developer wants to perform.
Open editors are typically shared between perspectives, i.e., if you have an editor open in
theJavaperspectiveforacertainclassandswitchtothe Debugperspective,thiseditorstays open.
YoucanswitchPerspectives viatheWindow→OpenPerspective →Other...menuentry.
Select File→New→Javaprojectfromthemenu.
Tocreatethec343.firstpackage,selectthesrcfolder,right-clickon itand
selectNew→Package.Enterthenameofyournewpackageinthedialogandpress the Finish button.
CreateJavaclass
publicclassMyFirstClass{
public static void main(String[] args) { System.out.println("HelloEclipse!");
}
}
Youcouldalsodirectlycreatenewpackagesviathisdialog.Ifyouenteranewpackageinthis dialog, it is created
automatically.
Runyourprojectin Eclipse
Classification by device
Desktop OS:
The most common type for personal computers, such as Windows, macOS, and Linux.
Server OS:
Optimized to handle multiple user requests and run applications for many users, such as Windows Server and Linux
Server.
Mobile OS:
Specifically designed for mobile devices like smartphones and tablets, such as Android and iOS.
Embedded OS:
Designed to run on a dedicated device, such as the operating system in a car's navigation system or a smart TV,
such as QNX or embedded Linux.
Windows 10?
Windows 10 is the most recent version of the Microsoft Windows operating system. There have been many
different versions of Windows over the years, including Windows 8 (released in 2012), Windows 7 (2009),
Windows Vista (2006), and Windows XP (2001). While older versions of Windows mainly ran on desktop and
laptop computers, Windows 10 is also designed to run equally well on tablets.
An operating system manages all of the hardware and software on a computer. Without it, the computer would be
useless. To learn more about how operating systems work, check out Understanding Operating Systems and
Getting to Know the OS in our Computer Basics tutorial.
How is Windows 10 different from other versions?
Many users complained that Windows 8, the previous version of Windows, was confusing and difficult to use. As a
result, Windows 10 looks and feels similar to older versions. Still, it includes a lot of new features and
improvements. Watch the video below to learn more:
<span id="selection-marker-1" class="redactor-selection-marker"></span>
Start menu
Whereas Windows 8 uses the Start screen to launch applications, Windows 10 has reintroduced a more traditional
Start menu. It's also been expanded to make it easier to find important apps.
Microsoft Edge
This new browser is designed to give Windows users a better experience on the Web. It's faster, more secure, and
includes a lot of new features. Microsoft Edge is meant to replace Internet Explorer as your default web browser,
but you'll still be able to use another browser if you prefer.
Cortana
Similar to Siri and Google Now, you can talk to this virtual assistant with your computer's microphone. Cortana can
answer questions like What's the weather like today?, perform simple tasks like remind you to take out the trash,
and much more.
Multiple desktops and Task view
Instead of keeping everything open on the same desktop, you can move some of your windows to a virtual desktop
to get them out of the way. And the new Task view feature makes it easy to manage all of your open windows.
Action Center
The new Action Center is pretty different from previous versions of Windows. For example, it's been expanded to
let you access frequently used settings, such as Wi-Fi connectivity and tablet mode. It's also where you'll see
important notifications, so if your computer receives an update you'll get a notification about it here.
Tablet mode
Unlike Windows 8, Windows 10 makes a clear distinction between desktops and tablets. If you're using a keyboard
and mouse with Windows 10, you'll be in desktop mode by default. If your computer also has a touchscreen, you
can go into tablet mode at any time. Tablet users can also switch back to desktop mode if they prefer.
Computer Science
Exploring Windows 10
Exploring Windows 10
Reviewed by:
Nafeez Khan
Download PDF
Study Materials
Sample Papers
Syllabus
Textbook Solutions
Introduction to Windows 10
Windows 10 is the most significant release of the Windows operating system since Windows 8. It came out first in
July 2015. This update brought back the much-loved home screen of Windows 8. The Start button has been added
back, and the vertical toolbars have been removed. It was later succeeded by Windows 11 in 2021.
Start menu.
The Start menu is the place from which you can open any programs. There are two ways to open the Start menu.
A. Click one time with the mouse on the start button located on the down left side of the homepage.
B. Press the Windows key located on the keyboard.
Action Center
Action Center
The action center is a window that allows you to access useful shortcuts and tools quickly. Various services like Wi-
Fi, Bluetooth and location are available in it.
Task Bar
Taskbar
The task view bar is located at the very end of the home page. It displays the frequently used apps so they can be
easily accessed. On the left side, it shows information like Internet connection, battery percentage, etc. In the
centre, the apps you have pinned down will appear. Microsoft Edge is the default app pinned down in the Task Bar.
Task View
Task View
The Task view can be opened by clicking on the Task view from the Taskbar. It allows the user to change the tab
that is open. You can also use it to see all the tabs at one time and remove any tab you do not want.
Tablet Mode
Tablet Mode
The tablet mode is Windows 10 allows you to open apps in a tablet-size window. This feature is very useful for
people using computers and smartphones.
File Explorer
File Explorer
The file explorer in Windows 10 allows the user to access his/her files. You can open, close, delete, share and copy
your files easily with the help of file explorer.
Cortana
Cortana
Cortana is a virtual assistant developed by Microsoft. It has the ability to write and send emails or messages by
itself. You can search for various information just by speaking to it. You do not need to press any key on the
keyboard to use Cortana. It allows you to truly do multitasking efficiently.
Microsoft Edge
Microsoft Edge
Microsoft Edge is the new browser developed by Microsoft. It has replaced the older Internet Explorer web
browser. Microsoft Edge uses the Microsoft Bing search engine as its search engine. Microsoft Edge is faster and
more secure than Internet Explorer. It is the only pinned-down app in the task view bar by default.
Firewall
Firewall
It is like a virtual wall that protects your computer. It checks the information entering the computer. If any threats
are found, it blocks the information from entering the computer.
Screensaver
Screensaver
When you do not use the computer for some time, it goes dormant. When it is in a dormant state, it displays a set
image on its screen.
Viruses
Virus
Viruses are unneeded software that try to harm your computer. It has the ability to modify or delete files from your
computer. It can automatically create many copies of it itself and slow down the computer.
The Definition of Windows Operating System: History, Functions, and Features
meilinaeka
28 April 2023
Blogs
This article explains about the Windows operating system developed by Microsoft and released in 1985. In this
article, you will find the history of the evolution of the Windows operating system, its main functions in
managing computer resources.
Definition of Windows OS
Windows operating system is a computer program that manages all computer resources and provides services to
applications that run on top of it. This operating system was developed by Microsoft and released in 1985 under
the name Windows 1.0. Since then, the Windows operating system has continued to evolve and become one of the
most popular operating systems in the world. The Windows operating system is designed to run on various types of
hardware, including desktops, laptops, servers, and mobile devices. Windows uses a graphical user interface (GUI)
that allows users to interact with the computer through icons, buttons, and visual menus, rather than using text
commands like other operating systems. Windows also has many features such as multitasking capability, which
allows several applications to run simultaneously, as well as plug-and-play capability that makes it easy for users to
connect additional devices such as printers, scanners, and cameras.
History of Windows OS
This OS was first introduced by Microsoft in 1985 and has since undergone many developments and evolutions.
Windows 1.0, the first version of this OS, was released in November 1985. This OS was the first graphical operating
system designed for commercial use and could be used on PCs using Intel 8086 or 8088 processors. Windows 1.0
had a simple interface and offered support for several applications, such as Paintbrush, Write, and Calculator.
In 1987, Microsoft released Windows 2.0. This OS offered several improvements and enhancements compared to
the previous version, including support for more applications, the ability to run multiple programs simultaneously,
and the introduction of drag-and-drop functionality.
Windows 3.0, released in 1990, was the first truly popular Windows operating system. Windows 3.0 offered a more
modern and user-friendly interface, support for more programs, and the ability to run multiple programs
simultaneously. In addition, Windows 3.0 also included the introduction of features such as File Manager and
Program Manager.
Windows 95, released in 1995, was a very popular Windows operating system and a milestone for modern
operating systems. Windows 95 offered a completely new interface, which included the Start button and Taskbar,
as well as support for plug-and-play hardware. Windows 95 also added support for 32-bit programs and introduced
features such as Windows Explorer, Internet Explorer, and Microsoft Office software.
After Windows 95, Microsoft released several other versions of Windows, including Windows 98, Windows 2000,
Windows XP, Windows Vista, Windows 7, Windows 8, and Windows 10. Each version of this OS offered different
improvements and fixes, including performance improvements, enhanced interface, support for new hardware and
software, and better security features.
The Windows operating system has undergone incredible evolution since it was first launched in 1985. Each version
of Windows offers different improvements and fixes, and this operating system remains one of the most popular in
the world to this day.
Functions of the Windows Operating System
The functions of the Windows operating system are crucial in running various applications and programs on
computers or laptops. Some of the functions of the Windows operating system include:
Managing Computer Resources: The primary function of the Windows operating system is to manage and organize
computer resources such as CPU, RAM, and hard disk. The Windows operating system will complete various tasks
such as opening applications, accessing the internet, and printing documents using these resources.
Providing an Interface: The Windows operating system provides a Graphical User Interface (GUI) that allows users
to access and use various applications and programs easily. This interface allows users to select menus, click icons,
and navigate various applications easily.
Providing Compatibility: The Windows operating system is designed to support various hardware and software
devices, making it easier for users to install and use different applications and programs on their computers or
laptops. Windows also provides the ability to run programs and applications designed for different versions of the
Windows operating system.
Facilitating Network Setup: Windows operating system provides various tools to facilitate network setup, such as
network configuration, firewall configuration, and network access configuration. This allows users to connect to
networks and share data with other devices in the network easily.
Facilitating Security: The Windows operating system provides various security features such as anti-virus, anti-
malware, and firewall that help protect computers or laptops from virus and malware attacks. Windows also
provides tools to configure network security and access control to protect user’s important data.
Managing File Management: Windows also facilitates file management such as data storage, access rights
configuration, and file searching. Windows provides various tools to help users organize and store data and files on
their computers or laptops.
Facilitating System Setup: Windows provides various tools for system setup such as desktop appearance
configuration, sound and visual effects configuration, and operating system updates. This makes it easy for users to
configure and update the operating system according to their needs and preferences.
Providing Technical Support: Windows provides technical support for users in the form of customer service,
tutorials, and documentation. This helps users solve problems and optimize the use of Windows operating system
on their computers or laptops.
Advantages and Disadvantages of Windows Operating System
As the most widely used operating system in the world, Windows has various advantages and disadvantages that
need to be considered. Here are some of the advantages and disadvantages of the Windows operating system:
Advantages:
Wide Compatibility: Windows has extensive compatibility with various hardware and software devices, making it
possible for users to use different types of devices with this operating system.
Ease of Use: Windows is designed with a user-friendly and familiar user interface for many people, making it easy
to learn and use even for new users.
Rich Multimedia Features: Windows provides many multimedia features, such as Windows Media Player, that allow
users to play various types of audio and video files easily.
Large Developer Support: There are many developers who make applications and games for Windows, so users can
choose from a variety of application and game options available.
Long-term Support: Microsoft provides long-term support for each version of Windows, which means users can
receive updates and security support for several years after its release.
Disadvantages:
Vulnerability to Malware: As the most widely used operating system in the world, Windows is also more vulnerable
to virus and malware attacks, so users need to be more cautious in using it.
Hardware Requirements: Windows requires relatively high hardware requirements, making it difficult for users with
older computers or laptops to use the latest version of Windows.
Cost: Windows is not a free operating system, so users need to purchase a license to use it, which can be expensive.
Having too many features: In some cases, Windows can be too complicated with too many features, so users have
to take the time to learn and understand these features.
In conclusion, Windows OS has its strengths and weaknesses like any other operating system. However, its wide
compatibility and large developer support make it the top choice for many users. Users should take note of these
shortcomings and ensure to take necessary security measures to protect their devices from virus and malware
attacks.
20 Features Of Window 10 Operating System
ShiwKumar Gupta
1y
178.3k
9
2
300
Article
Introduction
Windows 10 is the new Microsoft Windows Operating System and successor to the Windows 8 (and Windows 8.1)
Operating System. It is a familiar and flexible computer operating system like Windows 7 with the Start menu
developed by Microsoft as part of the Windows NT family of operating systems.
Features of Windows 10
Windows 10 was released with mostly positive reviews upon its original announcement in July 2015; detractors
praised Microsoft's decision to downplay user-interface mechanics introduced by Windows 8 (including the full-
screen apps and Start screen). Here are the best features of the operating system.
It's free
One of the best new growth in Windows 10 is that it is completely free to upgrade. Microsoft made this declaration
at its January event in Redmond. The steady has said it will be available at no charge for the first year for Windows
8.1 and Windows Phone 8.1 users. It will also be free if you are still running Windows 7.
Start Menu
As we know previous to the January meeting, Windows 10 will mark the return of the much-loved Start Menu. In
the latest build shown, it has some updated graphics and can optionally go full-screen. In this feature half of the
menu looks pretty much like it did in Windows 7 but there's the clear addition of Live Tiles.
Universal apps
The universal app is a Windows experience that is made upon the Universal Windows Platform (UWP). It is good
news for someone using more than one Windows device. A package of apps with Photos, Videos, Music, Maps,
People and Messaging, and Mail and Calendar will look and feel the same across different devices and screen sizes.
Universal apps include a lot of backend enhancements for developers that encourage development on Windows
Phone and Windows 8. They are not completely the same in terms of code; nor does it mean that developers can
just push a button to make those apps on either platform.
Edge browser
Microsoft developed a new web browser called Edge; It gives you new ways to find stuff and read/write on the
web. Microsoft Edge is built with interoperability in mind, according to Microsoft. It’s a feature that includes a
reading mode and the ability to annotate, either with a keyboard, pen, or a finger. There's also integration with
Cortana to provide additional information, for example, when we are on a web page for a restaurant/Hotel Cortana
will make a booking and display information such as opening times.
Best new features in Windows 10
Enhancements The new OS is still in active development; the main features and enhancements of Windows 10 are
more or less feature-locked and are certain to the announced within Windows 10 Release to Manufacturing (RTM).
Microsoft is making changes suggested by Windows users around the globe. Since Windows 10 runs across all
devices, the OS will have unified settings. It means the end of a distinct control panel and PC settings.
There's also the Action Center now provides notifications and is synchronized across devices. Contact Support
Microsoft has added a new feature in Windows 10 is safe for users who fail to find any they need. Users can contact
the Phone or Chat Microsoft Tech Support Team using the Contact Support App, to solve the problem with Internet
Explorer Edge, Office, and Xbox. We can chat online on a call.
Microsoft Money
Microsoft Money is a personal finance management software program from Microsoft. It has capabilities for
viewing bank account balances, tracking expenses, and creating budgets.
Phone Companion
Microsoft announced a special type of phone companion app for Windows 10 that strategy to understand
smartphone owners to gap between their handset and computer. The phone companion app works like a hub
(center) to download apps like Skype, Outlook, and Xbox Music, etc.
Windows Phone
Windows 10 has launched but it does not get on your phone. It can’t say that Windows Phone 10 as the new
operating system will run phones, tablets and you’re PCs. Windows Phone is near as Windows 10 will arrive on
Windows Phone devices when it launches. Microsoft has given no other name for running it on a smartphone, small
tablets, and PCs. It looks much the same as Windows Phone 8 but with tweaks. Action Center is synced with your
other devices and the app menu will show recently installed apps at the top. Windows 10 is already here for your
computer and we have got all the details you need.
HoloLens
HoloLens is Microsoft's immersive headset. We discuss one platform, a single binary, a single app that can run
across all of these devices." This means it’s a futuristic, hologram-generating HoloLens headset. Windows 10 is the
first holographic computing platform. A developer can create holographic experiences with a set of APIs in the real
world. Microsoft receives the world’s first untethered holographic computer that doesn't need to connect to a PC
to work with the HoloLens.
Continuum Mode
Continuum is a method for Windows to define the hardware, we are always using the optimum interface on the
machine. If we are using a desktop or laptop with no touchscreen, then Windows will now know this and present
the traditional mouse and keyboard interface. The OS moves easily between laptop/ desktop (keyboard/mouse)
and tablet (touch) usage modes automatically. It will do this if it detects the loss or addition of a keyboard.
Whether you are new to Windows 10 because you recently upgraded after support for Windows 7 was
discontinued, or you haven’t explored more recent updates to the operating system, there are a lot of ways to
enhance your computing experience. There are many enhancements including an upgraded clipboard, new screen
capture, and dark mode. You can also enable speech recognition and dictation, control updates and customize the
tiles menu. Read more to find out about how to control your Windows 10 experience.
Windows Updates
The most recent major to the Windows 10 operating system was in November 2019, though smaller updates are
issued frequently. Windows 10 updates also include security patches, which are very important but recently have
made headlines for wreaking havoc on your system. You can control if your computer gets auto-updates, uninstall
troublesome updates, and more by typing “Windows Update” in the search bar. To uninstall an update go to
Windows Update – View Update History – Uninstall Updates. You can also download the Windows Update
Troubleshooter to hide problematic updates and prevent them from reinstalling. It is also a wise idea to set
automatic restore points so if something goes terribly wrong you can revert to a previous version of your system.
The restore point does not delete new files, but it is always a good idea to have redundant file backup.
Show Desktop Button
This feature has been in the Windows operating system since Windows 7, but still useful to know. In the far-right
corner of your screen there is a tiny, practically imperceptible vertical bar. If you click on it, you will be taken
directly to your desktop without having to close or minimize any applications. Click it again to return to where you
were. If you want to be able to glance at your desktop simply mouse over the button to “peek” at your desktop.
Enable Peek by going to Settings – Personalization – Taskbar – Use Peek to Preview the Desktop.
Windows 10 Tiles and Start Menu
Windows 10 uses “tiles” to show you applications, weather, and other information and replaced the standard
Windows start menu. If you don’t like the tiles you can customize them, remove them, or even revert to the
Windows 7 look and feel with a third party app. You can also access the “secret” start menu by right-clicking on the
Windows button in the lower-left corner of your screen or using the Windows keyboard shortcut Windows key + X.
Task View and Virtual Desktop
In the Windows 10 taskbar, there is an icon that looks a little like a ladder. If you click on it you will be able to see all
the applications you have open and go to them, or close them. It also shows you the documents you have worked
on in the past few days. If you click on “New Desktop” you can create virtual desktops. A virtual desktop will let you
create multiple desktops with default apps and docs open. For instance, you can have a desktop with your primary
productivity apps open (like Word, Excel and your practice management app) and one with your communication
tools like Outlook, Teams or Slack. By segregating your desktops, you can reduce distractions and focus on tasks
more effectively.
Silence All the Notifications
By default, Windows 10 pops up a lot of alerts. You can control this by using Focus Assist, which came out with the
April 2018 update. Go to Settings – System – Focus Assist and you can customize notifications for just about
everything. You can also add “quiet hours” so you can schedule deep work and get no notifications. You can
further refine notifications under “Notifications and Actions” so that you get only the notifications you want, when
you want them.
Speech Recognition/Dictation
You can dictate text, open applications and perform other tasks by voice. To enable and customize speech
recognition in Windows 10 press Windows key + H. If you haven’t enabled speech services, you will be guided
through the process via Settings. Like other speech recognition tools, you’ll need to say “period”, “comma”, “new
line”, etc. when dictating. If you need to correct something just say, “undo that”. You can also have text read back
to you, which may help identify tone and word choice. There are a lot of options and features you can leverage
with the speech recognition tools. If you don’t want to turn on speech recognition operating system-wide, Word
and Outlook have their own dictation tools built-in.
Cortana
Cortana is the Windows 10 “personal productivity assistant”, much like Siri in iOS. You can control what permissions
Cortana has and when to wake “her” up. In addition to voice commands you can cc Cortana on an email to help
find available time on your calendar. If you like using Siri and talking to your device give Cortana a try. If you prefer
not to have your computer listen to you then disable Cortana or log out of Cortana.
Dark Mode
In Windows 10 you can change your color scheme by going to Settings – Personalization – Colors. You can have one
theme for Windows menus and a different one for apps. You can customize the colors in each one, should you
choose to do so. Dark mode is very popular, as it helps reduce eye strain and also can help save your battery on a
laptop. It also makes icons much easier to see, especially in your taskbar. If you just want to change your color
scheme for Office 365 go into Outlook, click File – Office Account – Office Theme and choose from Colorful, Dark
Grey, Black or White.
Cloud Clipboard
You can save multiple items in your clipboard when you copy images or text to reuse in applications, as well as keep
a history of what you have copied. Because copies are now stored in the cloud you can access your history and
paste into an application on a completely different device. Enable the clipboard history by going to Settings –
Clipboard and turn on Clipboard history. You can choose whether the clipboard is cloud-enabled by toggling on
“sync across devices”. To enable your clipboard history press the Windows logo key + V. There are a lot of options
with this powerful feature.
Snip & Sketch
The handy Windows Snip tool has been around since Windows 7 and it lets you capture and crop any part of your
screen, a much more granular option than doing a print screen. The new clipping utility that came with the October
2018 update is called Snip and Sketch. In addition to “snipping” a select portion of your screen you can now have it
do a “countdown” before capturing the clip so you can open menus and other setups. You can annotate the clips
(thus Sketch), crop them and save them. However, if you need a powerful screen clipping and annotation tool you
still need something like Snagit.
Security Features
In Windows 10 you can turn on Windows Hello to unlock your computer with facial recognition, as well as log you
into apps like Dropbox, Office 365 apps and more. Additionally, Windows 10 comes with Microsoft Defender which
includes antivirus, firewalls, real-time protection and more. To configure it and learn more go to Settings – Security
Center – Windows Security. You can run Windows Defender and another antivirus product simultaneously.
Conclusion
There are lots of other tips, tricks and options in Windows 10. While you don’t necessarily think about the
operating system too much, focusing more on the apps that support productivity, communication and information,
knowing some ways to leverage the platform these apps sit on can help you leverage the power of your Windows-
based computer.
Introduction to Windows
In this topic, we will learn about the Introduction to Windows. Microsoft Windows is a multitasking operating
system developed by Microsoft Corporation, which uses Graphical User Interface to interact with users. Microsoft
was initially named “Traf-O-Data” in 1972, was renamed “Micro-soft” in November 1975, and then “Microsoft” on
November 26, 1976. Microsoft entered the marketplace in August 1981 by releasing version 1.0 of the operating
system Microsoft DOS (MS-DOS), a 16-bit command-line operating system. Bill Gates and Paul Allen founded
Microsoft, and the Windows operating system has been its primary product.
In this Introduction to Windows, we will also clear you about the latest OS release of Windows, “Windows 10”,
launched in 2015.
In a nutshell, below is how Microsoft Windows evolved:
Windows 1.0 – Nov 1985
Windows 2.0 – Dec 1987
Windows 3.0 – May 1990
Windows 95 – Aug 1995
Windows 98 – June 1998
Windows ME – Sep 2000
Windows XP – Oct 2001
Windows Vista – Nov 2006
Windows 7 – July 2009
Windows 8.0 – Oct 2012
Windows 8.1 – Oct 2013
Windows 10 – July 2015
Windows 11 – October 2021
Main Components of Windows
After learning about the Introduction to Windows, we are now going to study the main components of Windows.
The main components of the Windows Operating System are the following:
Configuration and maintenance
User interface
Applications and utilities
Windows Server components
File systems
Core components
Services
DirectX
Networking
Scripting and command-line
Kernel
NET Framework
Security
Deprecated components and apps
APIs
Source Link: https://en.wikipedia.org/wiki/List_of_Microsoft_Windows_components
When coming to GUI components, we have the following as the main components:
1. Desktop
It is the first screen you will see once the windows start. Here you will see “My Computer”, “My Documents”, “Start
Menu”, “Recycle Bin”, and the shortcuts of any applications that you might have created.
2. Taskbar
At the bottom, you will see a row known as the taskbar. It has the currently running applications; you can pin
applications you frequently use using the option Pin to Taskbar”.
3. Start Menu
This is located in the bottom left corner of Windows OS GUI. This is where the user can search for any setting and
any application for their use. Users can uninstall or repair applications from the control panel. The user can do a lot
of activities just by searching through the start menu.
4. My Computer
Double-clicking the “My Computer” menu will let you navigate between your different computer drives and the
control panel tools. You can see and manage the contents that are inside your drive.
5. Recycle Bin
Deleting an item from any of your drives using the “delete” button or simply clicking right-clicking and selecting the
“delete” option is not deleted completely. Instead, it is moved to the “Recycle Bin” folder of Windows. You can
recover your content if you have deleted it by mistake, or if you choose to delete the items from here, it will get
deleted permanently. Should you wish to delete the item in the first go itself without moving it to recycle bin, you
can use the key “Shift+Del”
Features of Windows
Windows Search: We can have multiple files and contents located on our system, and sometimes we may run out
of memory about the exact location of our file. Windows Search is a function included with Windows that allows
users to search their entire computer.
Windows File Transfer: We may need to transfer in or transfer out the files and contents from our machine to
other devices such as other computers or mobiles, and tablets. We can do this using an Easy Transfer Cable, CDs or
DVDs, a USB flash drive, wireless Bluetooth, a network folder, or an external hard disk.
Windows Updates: Windows includes an automatic update feature to keep its operating system safe and up-to-
date.
Windows taskbar: At the bottom-most part of your windows, you will see a row known as the taskbar. It has the
currently running applications; you can pin applications you frequently use using the option Pin to Taskbar”. The
taskbar is the main navigation tool for Windows.
Remote Desktop Connection: This Windows feature allows you to connect to another system and work remotely
on another system.
Windows Applications
A program that is written to run under the MS operating system is the Windows Application. Examples of
applications that run on Windows OS are Microsoft Outlook, Internet Explorer, Remote Desktop Condition,
Snipping Tool, and numerous others.
These applications provide great functionality for users to do their day-to-day activities. Every application
developed for them to launch on Windows should be compatible with the Windows operating system. That is why
when you install software, you might see two or three download options, download for Windows, download for
Linux and download for iOS. If you are interested in developing applications for Windows, you can start right here:
https://docs.microsoft.com/en-us/windows/win32/
Advantages and Disadvantages of Windows
After going through all the components, Features and the broad Introduction to Windows, we will explore the
advantages and disadvantages of Windows.
Advantages
Desktop as well as tablet-friendly OS.
Switching between applications is very easy.
Operating Windows does not require much technical knowledge.
Windows OS is the dominant OS and enjoys over 90% of the Market share.
MS OS has a great support community, and it also has the most significant number of applications.
Microsoft provides a robust set of Enterprise focused Operating Systems, Applications and services, making it the
most dominant player in the OS market.
More gaming.
Disadvantages
There are privacy and security concerns and incidents, and they are widespread. Linux OS is by far the most secure
OS ever built.
Cost for an upgrade.
Windows OS attracts many virus programs due to its significant market share and easy-to-breach paradigm.
Windows OS is not that much of touch-friendly.
History
DOS (Disk Operating System) was developed by Microsoft Inc. and released it as IBM PC DOS (known as MS-DOS)
for the first time on August 21, 1981, as MS-DOS 1.0, which was written by Tim Paterson.
It was last updated in April 1994 when MS-DOS 6.22 was released. Today, MS-DOS is no longer used at large scale;
however, the command shell, more commonly known as the Windows command line is still used by many users in
window OS.
It was the first OS for the PC (IBM PC) and is the underlying control program for Windows 3.1, 95, 98 and ME.
Windows NT, 2000 and XP emulate DOS in order to support existing DOS applications.
Introduction
This OS was written initially in assembly language and later advance version also with C language.
The first version of MS DOS was MS DOS 1.x and latest version is MS DOS 7.
Definition
DOS is a Free, single-user, single or Uni tasking, earlier, 16-bit, CUI/character based or command line(non-GUI),
standalone operating system from Microsoft for the PC that controls and regulates computer’s hardware and
peripheral devices behaviour and functionality.
Features
DOS contains a master control program that is automatically run when we start our personal computer (PC). DOS
stays in the computer all the time letting us run a program and manage files.
DOS uses the File Allocation Table (FAT) filesystem. This was initially FAT12 which supported up to 4078 clusters per
drive. Later, DOS 3.0 added support for FAT16 which used 16-bit allocation entries and supported up to 65518
clusters per drive.
Advantages:
o DOS (Disk Operating System) was the first widely installed operating system for personal computers.
o It is Free OS.
o It does File management works such as creation, updation, deletion of files.
Disadvantages:
o It doesn’t support GUI i.e. it has text based interface.
o It has Single user OS.
o It has Uni tasking OS.
o It is a 16-bit OS.
o The mouse didn’t work in it.
Terminology
Commands:
A command is a set of instructions used to perform a specific task on files or directory.
This command is executed/interpreted by OS interpreter to a machine language (0&1).
It is unique keyboard recognize by an OS.
There are two types of DOS Commands-
(a) Internal DOS commands
o These commands are loaded during/with the system booting with command.com file and are stayed and executed
from the RAM.
o These commands cannot be created, viewed, updated or deleted by the user because they do not exist in the form
of a file rather included in command.com file.
o These commands are in-built in DOS and are part of shell.
o There is no external file to read the internal commands.
o Examples are – DATE, TIME, VER, VOL, MD, CD, CLS, DEL, CLEAR, COPY CON, REM, RMDIR, TYPE, BREAK, CALL,
CMD/COMMAND, COLOR, ECHO, EXIT, CTTY, FOR, GOTO, IF, MD, MKDIR, LOCK, MOVE, PATH, PAUSE, PROMPT, RD,
REN, RMDIR, UNLOCK, TITLE etc.
(b) External DOS commands
o These commands are only loaded from disk to main memory as a file when they are invoked.
o These commands are loaded after system booting when required and are present and executed from the Disk.
o It can be invoked whenever necessary.
o These commands are not in-built in DOS and are part of shell.
o These commands can be viewed, updated, copied or deleted by the user because external files exist in the form of
a file as .exe, .bat, .com etc.
o Examples are – APPEND, ASSIGN, ATTRIB, CHKDSK, COMPACT, DEBUG, DEFRAG, DELTREE, DISKCOPY, EXTRACT,
FDISK, FIND, HELP, IPCONFIG, LABEL, LOGOFF, MORE, NET, NETSH, NETSTAT, PING, POWER , QBASIC, SCANDISK,
SHUTDOWN, TELNET, EDIT, SYS, FORMAT, XCOPY, PRINT, SORT, TREE, UNDELETE, UNFORMAT etc.
Directory(Folder):
Directory is a file used to store/list another files and directories(called sub-directory) inside it.
It is considered as a catalog of files & directory.
There are 3 types of directory used in MS-DOS –
(a) Root Directory
o A directory which is created automatically when the disk is formatted.
o For example –
For a floppy disk, drive A:\ and B:\ are root directory.
For a hard disk, drive C:\, D:\, E:\ etc. are root directory.
(b) Parent Directory
o Parent directory is a directory which is one level up to the current directory.
o For example –
C:\ Audio\Sad_Songs\01.mp3.
C:\ = Root directory.
Audio = Parent directory.
Sad_Songs = Current or working directory.
01.mp3 = An Audio file.
(c) Current or Working Directory
o The directory in which a user is working at that moment or currently.
o For example –
C:\ Audio\Sad_Songs\01.mp3.
C:\ = Root directory.
Audio = Parent directory.
Sad_Songs = Current or working directory.
01.mp3 = An Audio file.
Sub-directory (Sub-folder):
A directory is within/inside a directory is called sub-directory.
Wild Card Characters in MS DOS:
Wild Cards are special characters and symbols that represent one or more characters.
The wild card characters are – (*) Asterisk – used to represent one or more characters and (?) Question mark –
used to represent single character.
Wild card is mainly used to search specific pattern of files or directories.
DOS Commands
DOS commands are not case sensitive.
How to open DOS in Window XP/7/8.1/10 OS
(a) Press “Window key+r” – Type ”cmd or command” in box – ok.
Discover more
Computer programming
Computer
Science
computers
Programming
OR
(b) Right click on Start button – Choose and click “Command prompt or command prompt(admin)”.[In window 10
OS]
Discover more
Computer
computers
Programming
Science
Computer programming
NB: command prompt(admin) is used to open DOS in administrator mode.
Batch Command :
The batch command is a recovery console command that executes a series of commands written within a file called
Batch files.
Batch files allow MS-DOS and Microsoft Windows users to create a list of commands to run in sequence once the
batch file has been executed.
A simple batch file does not require any special programming skills and can be done by users who have a basic
understanding of MS-DOS commands.
A good example of Batch file is use of Shortcut icon in Windows. Another very good example of Batch file is the use
of Autoexec.bat file. (This is a boot batch file loaded each time the computer boots into MS-DOS and early versions
of Windows. This batch file contained all the necessary commands and programs used to run MS-DOS and
Windows each time the computer booted.).
A batch file is executed in two ways – By command prompt and double click on the batch file.
Batch files are created through two ways –
o Using MS-DOS:
A basic batch file is created using MS-DOS, the following steps are taken to create –
1. Open MS-DOS.
2. Create a new batch file by typing the name of file as edit test.bat at the MS-DOS prompt and then type press enter.
3. Now, type the require commands:
pause
dir c:\windows
dir c:\windows\system
4. Now, save the file again.
5. Once we are back at the MS-DOS prompt(C:\), type: test and press enter. (This will execute the test.bat file and
begin running the file. Because the first line is pause, we will first be prompted to press a key. Once we press a key
the batch file will run line-by-line; in this case, listing the files in the windows and windows\system directories
occurs.)
NB : Some versions of MS-DOS and bootable diskettes may not have the edit command; if this the case, we would
either need to obtain the edit.com file to access this file or use the copy con command to create a new file.
o Using MS-Windows:
A Windows user can use any simple text editor, such as Notepad, to create/save batch files with file extension .bat.
For this, we should follow the below steps –
1. Open Notepad.
2. Once notepad is open, type the commands in the file
@echo off
echo Batch file is started executing
pause
dir c:\windows
3. Save the file as “test.bat”, and choose “Save as type” option, as “All files”, otherwise it will save as a text file. Once
all of this has been done click the Save button and exit notepad.
4. Now, to run the batch file, by double-clicking on it or run the file like any other program. Once the batch file has
completed running it will close the window automatically.
NB: Here, @ symbol avoids echo back the text after the at symbol. This most commonly used as @ECHO OFF to
prevent any of the commands in the batch file from being displayed, just the information needed.
In batch file, CALL statement is used to run another batch file within a batch file. When the batch file that is called is
completed, the remainder of the original batch file command is completed. And if the called batch file does not
exist it will give an error message.
Chdir/Cd Command :
This command is used to change/switch and display the current directory.
Example:
o CD directory_name command is used to move to the new directory ahead from current working directory.
o CD.. command is used to move to the one directory back in one go (i.e., in parent/root directory) from current
working directory.
o CD/ command is used to move to the beginning or first directory back (i.e., in root directory) from current working
directory in one go.
o cd /d command is used to switch from one drive to another drive in one go such as c:\cd / e:\new folder5 to move
from c drive to new folder5 directory of e drive in one go.
Chkdsk command :
Chkdsk stands for Check Disk.
This command is used to check the status of the file system.
It scans the disk drive and helps in detecting error, repair and retrieval of data from bad sectors.
Syntax:
o C:\Chkdsk drive_name (Press Enter)
Example:
o C:\Chkdsk d: /r (Press Enter)
o C:\Chkdsk d: /f (Press Enter)
o C:\Chkdsk *.* (Press Enter)
Cls command :
This command is used to clear/clean the complete previous contents of the screen and leave only a prompt.
Syntax & Example:
C:\ cls (Press enter)
Copy Command :
This command is used to copy a complete file from one location to another.
Syntax:
C:\ copy source_file_name destination (press enter)
Example:
C:\ copy file1.txt d:\new folder (press enter)
C:\ copy *.txt f:\new folder1 (press enter)
Copy Con command :
Copy con command is used to write/create a new or fresh file with certain name in the current/working directory.
After the finishing of new file creation, CTRL+ Z button is pressed/required to save & exit from the created file
successfully to come again at DOS prompt.
This command actually copies the contents from the console and writes into a file.
This command does not edit the created file.
Syntax :
C:\ Copy Con Filename (Press enter)
Example
C:\ Copy Con File1.txt (Press enter)
——–
——–
———
Ctrl+Z (to save & exit)
Date Command :
The date command can be used to display the current date of the system and prompt for a new one. If no date is
entered, the current date will be kept.
This command is also used to change the date to an alternate date.
C:\ date (Press enter)
C:\>date
The current date is: 30-12-2022
Enter the new date: (dd-mm-yy)
Dir Command :
This command is used to display all the non-hidden files & sub-directories present/stored in that current/working
directory.
In another words, this command is used to view the contents of a directory (directory list=files & directories).
Syntax :
C:\ dir (Press enter)
C:\dir/option Directory_name (Press enter)
Example :
(i) C:\ dir (Press enter) [Display all the non-hidden files & sub-directories only from C drive/directory.]
(ii) D:\ dir (Press enter) [Display all the non-hidden files & sub-directories only from D drive/directory.]
(iii) C:\ New Folder\New Folder1\dir (Press enter) [Display all the non-hidden files & sub-directories only from New
Folder1 directory.]
(iv) C:\ dir New Folder (Press enter) [Display all the non-hidden files & sub-directories only from New Folder
directory of C drive.]
(v) C:\dir *.exe = To display or list all the non-hidden executables files from C drive.
(vi) C:\dir *.txt *.docx = To display or list all the .txt and .docx extension files from C drive.
There are several options used with dir command to display the contents according to the need. These are –
o dir/a = Used to display all the hidden and non-hidden files & directories from that current/working directory.
o dir/ah = Used to display all the hidden files & directories only from that current/working directory.
o dir/a-d = Used to display all the files only from that current/working directory.
o dir/ad = Used to display all the directories only from that current/ working directory.
o dir/a/s = Used to display all the directories with files and sub-directories from that current/working directory.
o dir ra*.* = Used to display all the files & directories starting with ra alphabets having any extension name from that
current/working directory.
o dir /s = List the files in the directory that we are in and all sub directories after that directory.
o dir /p = List all the files’ contents page wise.
o dir /w = List the file and directory related information width wise/horizontally.
o dir /s /w /p = List all the files and directories in the current directory and the sub directories after that, in wide
format and one page at a time.
o dir /on = List the files in alphabetical order by the names of the files.
o dir /o-n = List the files in reverse alphabetical order by the names of the files.
o dir \ /s |find “i” |more = To list all directories on the hard drive, one screen page at a time, and see the number of
files in each directory and the amount of space each occupies.
Del (Erase) command:
Del command is used to remove one or more files permanently from a directory.
Syntax:
C:\Del filename (Press Enter)
Example:
o C:\Del File1.txt (Press Enter)
o C:\Del *.txt (Press Enter)
DelTree command:
Deltree command is used to remove a directory along with sub-directory and files, if any, in one go, permanently.
Syntax:
C:\Deltree Directory_name (Press Enter)
Example:
o C:\Deltree Dir1 (Press Enter)
Echo command:
Echo command is used to display messages as we typed or turns command-echoing on or off.
Syntax:
o ECHO [message]
o ECHO [ON | OFF]
Example:
o C:\>echo Welcome
Welcome
Edit command:
Edit command allows a user to view, create, or modify the text files.
Syntax:
o C:\Edit filename (Press Enter)
Example:
o C:\ Edit File1.txt (Press Enter)
Exit command:
This command is used to come out/close/exit from the DOS.
It quits the command.com file.
Example :
C:\ exit (Press enter)
Format command:
This command is used to format the diskette and fixed disks drive.
This command initializes the directory and file allocation tables.
Syntax:
C:\ Format drive_name: (press enter)
Finger command:
This command is used to view information about users logged onto a remote computer.
This may include their e-mail address, full name, when last time user logged in, read their e-mail etc.
Syntax:
C:\ finger -l user@ localhost (Press enter)
Help command:
Help command is used to give the information about the given keyword from a MS-DOS prompt.
Syntax:
o C:\ keyword -help
or
o C:\keyword /?
Example:
o C:\dir -help
or
C:\dir /? [Gives the full detail about dir command]
o C:\help dir (Press enter)
o C:\help dir (Press enter)
o C:\help date (Press enter)
o C:\date /? (Press enter)
Hostname command:
Hostname command is used to display the name of current Host.
Syntax & Example:
o C:\ hostname (Press enter)
Ipconfig command:
ipconfig command is used verify a network connection or view network settings.
Syntax & Example:
o C:\ ipconfig (Press enter)
o C:\ ipconfig /all (Press enter)
Mkdir or MD Command :
This command is used to create a new & blank directory in the current working directory with unique name.
Syntax:
C:\ mkdir directory_name(Press enter)
C:\ md directory_name(Press enter)
Example :
C:\ mkdir India (Press enter)
C:\ md India (Press enter)
More Command :
More command is used to display information or file contents to be displayed one page at a time.
Example:
o C:\dir | more (Press enter)
o C:\file1.txt | more (Press enter)
Move Command :
This command is used to cut/move a complete file from one location to another.
This command is also used in renaming the files, sub-directories and directories.
Syntax:
o C:\ move source_file_name destination (press enter)
o C:\ move old_file_name new_file_name (press enter)
Example:
C:\ move file1.txt d:\new folder (press enter)
C:\ move *.txt f:\new folder1 (press enter)
Net Command :
Net command is used to manage, set, view and determine network settings.
Example:
o C:\net view (Press enter)
o C:\net config (Press enter)
Netstat Command :
Netstat command is used to display the TCP/IP network protocols statistics and information.
Example:
o C:\netstat (Press enter)
o C:\netstat -a (Press enter)
Ping Command :
Ping command is used to determine whether the network is able to communicate with the network or not.
Syntax:
o C:\ping IP_address/web_address (Press enter)
Prompt Command :
Prompt command is used to change the MS-DOS prompt letter.
Syntax:
o C:\prompt normal_text (Press Enter)
Example:
o C:\prompt raj (Press Enter)
o raj_
Some special characters are not used as prompt using normal characters rather it is used specifically such as for –
o equal sign(=) use $Q.
o dollar sign ($) use $$
o Current time use $T
o Current date use $D
o Current drive use $N
o Pipe symbol (|) use $B
o Greater than symbol (>) use $G
o Smaller than symbol (<) use $&
Type PROMPT without parameters to reset the default prompt again.
RM command:
Remove command is used to remove files permanently from a directory.
This command works similar as Del/Erase command.
Syntax:
C:\rm filename (Press Enter)
Example:
o C:\rm File1.txt (Press Enter)
o C:\rm *.txt (Press Enter)
Rmdir/RD Command :
This command is used to remove a blank directory from the root/parent/current working directory.
Syntax:
C:\ rd directory_name(Press enter)
Example:
C:\ rd folder1(Press enter)
Ren/Rename Command :
Ren command is used to rename files and directories from the original name to a new name.
Unlike the move command, this command cannot be used to rename sub-directories or rename file across drivers
or cannot specify a new drive etc.
Syntax:
o C:\ren directory_name or File_name (Press enter)
Example:
C:\ ren raj raj1 (press enter)
C:\ ren *.txt *.bak (press enter)
C:\ ren file1.txt file2.txt (press enter)
Sfc Command :
Sfc command scans all protected system files and replaces incorrect versions with correct Microsoft versions.
This is a great command to run when we are running into an issue that is difficult to troubleshoot.
Syntax:
o C:\ sfc (press enter)
Example:
o C:\ sfc (press enter)
o C:\ sfc /SCANNOW (press enter) [Scans all protected system files immediately.]
o C:\ sfc /SCANONCE (press enter)[Scans all protected system files once at the next boot.]
o C:\ sfc /SCANBOOT( press enter) [Scans all protected system files at every boot.]
Systeminfo Command :
This command displays the complete system information for installed OS and network hardware used in our
computer.
Syntax & Example:
o C:\ Systeminfo (press enter)
Time Command :
Time command is used to allow the user to view and edit the computer’s time.
Syntax:
o C:\time
Example:
o C:\time (press enter)
The current time is: 11:12:49.96
Enter the new time:
Title Command :
Title command enables a user to change the title of their MS-DOS window i.e., sets the window title for the
command prompt window.
Syntax:
o C:\Title String (press enter)
Example:
o C:\ Title Window DOS (press enter)
Tree Command :
Tree command is used to allow the user to view a listing of files and folders in an easy-to-read listing mode.
It usually lists in tree form of the current drive files and folders.
To stop the Tree view in the middle of the screen, press Ctrl+C.
Syntax:
o C:\Tree Drive_letter (press enter)
Example:
o C:\Tree (press enter)
o D:\Tree (press enter)
Type Command :
Type command allows the user to see the contents detail of a file.
Syntax:
o C:\Type filename (press enter)
Example:
o C:\Type file1.txt (press enter)
o C:\Type D:\file2.txt (press enter)
Ver Command :
Ver command is used to displays the version of MS-DOS or if running Windows 95 or above the version of
Windows.
Syntax:
o C:\ver (press enter)
Example:
o C:\ver (press enter)
Microsoft Windows [Version 10.0.17763.3770]
Vol Command :
Vol Command is used to display the volume information about the designated logical drive.
Syntax:
o C:\vol (press enter)
Example:
o C:\vol (press enter)
Volume in drive C is WIN 10 OS NEW
Volume Serial Number is K2E7-X516
Xcopy Command :
Xcopy Command is used to copy one or more files and directories including sub-directories from one location to
another.
Syntax:
o C:\Xcopy sourcefile destinationfile (press enter)
Example:
o C:\Xcopy c:\dirname1 d:\ (press enter)
MS DOS File System
The booting process of DOS involves three main system files of DOS into memory. These files are IO.SYS,
MSDOS.SYS and COMMAND.COM.
The operating files of MS DOS are broadly categorized into –
o System files
o Command-line Interpreter/Shell file
o User Configuration files
System files
System files are high priority files performing system tasks.
System files may be a plain text file or binary(bin) files.
There is no way to open a .sys file. It’s a driver file that is processed by an OS automatically.
IO.SYS (or IBMBIO.COM) file:
o It was an essential part of MS-DOS and Windows 9x systems but is not used in newer versions of Windows.
o IO. SYS is an Input- Output System files.
o IO. SYS is a hidden, executable, binary file which processes instructions when the operating system is booted.
MSDOS.SYS (or IBMDOS.COM) file:
o This file contains the DOS kernel which is the main components of DOS OS.
o This file manages the hardware, device drivers, file & directory management, memory & program management.
Command-line Interpreter/Shell
Command COM file :
o COMMAND.COM file is responsible for user’s command (Interactive mode) and batch files (Batch mode)
interpreter. Hence, this file is called the default command line interpreter for MS-DOS, Windows 95, Windows 98
and Windows Me OS.
o This file uses the DOS API to communicate with the disk operating system.
o Internal commands reside in the COMMAND.COM file as binary file.
o It is also called MS DOS Prompt.
User Configuration files
CONFIG.SYS File:
o CONFIG.SYS means Configuration System file.
o It is a text file that contains commands and instructions (i.e. settings) to load drivers, shell, buffers etc. for a DOS
system.
o This file is also used to load programs in memory to manage memory, hardware, printer, mouse, keyboard and
other peripherals.
o It is executed during system boot.
o After executing the command of the config.sys file, the autoexec.bat file is executed.
o This is a primary or main configuration file for OS/2 and DOS OS.
o The CONFIG.SYS file is read every time when a DOS system is booted. The system then reads that file and executes
the command following it.
o Users can easily make changes in this file, if required, as per their requirements in the system by simply saving the
changes made in the CONFIG.SYS file. Since it is a text file, it can be edited it in any simple editing program.
o This file is located in the root directory of the drive; from where system is booted.
o The most common commands of the CONFIG.SYS file are:
BUFFERS = This command is used to specify the buffer size.
FILES = This command is used to determine the number of files that a user can open simultaneously.
AUTOEXEC.BAT
o This is a special plain-text batch file that is executed after system boot.
o With the help of this command, we can run commands, programs and batch files automatically at the time of
startup or booting of the system, by adding them to this file.
Operating System
History :
o The first operating system used for real work was GM-NAA I/O, produced in 1956 by General
Motors’ Research division for its IBM 704.
Introduction :
All computers need some sort of hardware platform to run the software; these platforms are called
Operating System (OS).
Definition :
An operating system commonly called OS, is system software which acts as an interface between a
user and computer hardware. It also acts as a bridge between application programs and the
computer hardware.
Operating system is a program that acts as an interface between user of computer and the
computer hardware.
Operating System is the software that manages all the computers‘ resources to optimize its
performance provides common services for efficient execution of various application software and
acts as an interpreter between the hardware, application programs and the user.
Features :
o The purpose of an operating system is to provide an environment in which user can execute
program in a convenient and efficient manner.
o Operating system is an important part of almost every computer system i.e. operating system is
must in order to operate a computer. Without operating system computer is of no use.
o An operating system is essential for any computer to be useful to us.
o Examples are – Microsoft Windows, UNIX, LINUX, OS/360, OS/2 etc are some of the famous
examples of an Operating system.
o Operating system is one of the most important example of system software.
o An Operating System is one of the most important components of the computer software which is
essential to operate a computer.
o When a user or a program wants the hardware to do something, the request is always
communicated to and processed by the operating system.
o Operating system is installed in secondary memory permanently, while it’s some part are stored
permanently in read only memory during starting/loading of computer system.
o For large systems, the operating system has even greater responsibilities and powers.
o Some part of the operating system resides in random access memory and the computer begins to
execute this part of the system.
o The majority of modern home computers use some form of Microsoft’s operating systems.
o Operating systems performs basic tasks, such as recognizing input from the keyboard, sending
output to the display screen, keeping track of files and directories on the disk and controlling
peripheral devices.
o The original & initial Microsoft operating system was called DOS (Disk Operating System) though
most computers use Windows. Windows comes in various versions beginning with version 3.x then
95, 98, XP and currently Windows 7/8/10.
o A few computers use IBM’s O/S2.
o Apple’s Mac use their own operating system beginning with OS 1 though most modern Macs use
version 8.x or 9.x. Apple’s latest version is OS 10.1.x.
o Some computer professionals, Internet Service Providers (ISP) and mainframe computer users use
an operating system such as UNIX/Linux, Windows NT or 2000 or server based operating systems.
o In short, when computer is turned on, it first needs to load some main components of operating
system into RAM sometimes referred to a booting up. It checks all its components and will usually
display a message if there is a problem. It is also known as Power on Self Test (POST). Loading the
system is usually automatic. Once the system is loaded, the user can start the application or
program that he/she going to use.
Functions :
o It manages all resources of computer system.
o The operating system controls the input and output or directs the flow of information to and from
the CPU. Much of this is done automatically by the system.
o A OS controls and manages all the hardware components of a computer as well as it also controls
input and output, memory and files. In other word a OS can be seen as a manager of the system.
o It optimizes and utilizes various kinds of resources like I/O devices, Memory, CPU etc.
Examples :
(i) MS Windows Operating System (Such as Windows 10, Windows 8/8.1, Windows 7, Windows Vista,
Windows XP, Window 98, Window 95, Window 3.11/3.1, MS DOS etc.)
(iii) Linux( such as Fedora, Red Hat, Debian, Ubuntu, Kali etc.)/Unix Operating System
(iv) Chrome OS
(vi) Solaris OS
(viii) CentOS
(ix) Android
(x) iOS
Introduction
The evolution of operating systems (OS) can be traced back to the early days of computing when the first
computers were designed for specific purposes and were operated by engineers and technicians.
The evolution of operating systems (OS) can be traced back to the 1950s when the first mainframe
computers were developed. At that time, computer systems were very large, expensive and required
specialized operators to run them.
During this time(the 1950s), computers were large, expensive, and used by only a few individuals or
organizations. As a result, the early operating systems were very simple and were primarily designed to
control the hardware.
The evolution of operating systems has been driven by advances in hardware technology and the needs of
users. As technology continues to evolve, operating systems will evolve to meet new demands and provide
new features and functionality.
Generation of OS
Discover more
computers
Science
Programming
Computer programming
Computer
Category of Evolution of OS
The evolution of operating systems (OS) can be traced back to the 1950s when early computers were first
developed.
Over the years, operating systems have evolved and become more advanced, with new features and
improvements being added along the way.
The evolution category of operating systems are:
1.
2. Time-Sharing Systems:
o In the 1960s, time-sharing operating systems were introduced, which allowed multiple
users to access/share a single computer system simultaneously through terminals.
o These systems were designed to allow users to interact with the computer in real-time,
making it possible to run interactive applications.
5. Networking Systems:
o The 1990s saw the widespread adoption of networking technologies, which allowed
computers to communicate with each other and share resources.
o This led to the development of client-server architectures and distributed systems where
multiple computers could share resources over a network.
o Windows NT and UNIX operating systems of this era were designed to support multiple
users and provide advanced networking capabilities.
7. Cloud Computing:
o In recent years, the rise of cloud computing has led to the development of cloud-based
operating systems, which run on remote servers and provide access to applications and
data over the internet.
o Here, applications and data are hosted on remote servers rather than on local machines.
o Examples of cloud-based operating systems include Google’s Chrome OS and Microsoft’s
Windows 365.
Over time, operating systems became more complex and powerful, with the ability to support multiple
users and handle a variety of input and output devices.
Icons
There are several small 2D or 3D graphical images scattered over the desktop are called icons.
It represents various items found on a desktop in simpler form which is used in several work of computer.
These Icons may also be found on the toolbars and in the menus of several computer application software
such as MS Office Suite(Microsoft Word, Microsoft Excel etc.).
Icons are more user-friendly compare to text-based commands and hence helps even a lay person to use
computer easily.
We can also rename an icon name by right-clicking on it.
Types of Icons :
There are two types of Icons- (a) System Icons and (b) Shortcut Icons
Discover more
Computer
Science
Programming
computers
Computer programming
Discover more
computers
Computer
Computer programming
Science
Programming
o Examples are – This PC, Control Panel, Recycle Bin, My Documents etc.
o Examples are – MS Office icons, Other User’s Application icons etc.
Desktop
Desktop is the first stable screen/background area on our computer screen, appears after soft/normal
booting process.
We can customize/arrange our desktop in several ways. We can add background picture, background color
etc. on a desktop.
In most modern computers, the desktop is an important part of Graphical User Interface (GUI) and is
believed that it is the gateway(Entry or Exit Place) of a computer system.
A desktop is a place which holds many icons, files, folders for first time use.
Some of the main components of Desktop Screen are:
o My Computer/This PC
o My Documents
o Recycle Bin
o Task Bar
My Computer/This PC
o This is one of the main icon that displays the main components of our computer.
o We can open any of the items in this window to further examine the components of our computer
by clicking on my computer icon.
o It displays different drives/disks attached with the computer.
My Documents
o It is the default space in Microsoft Windows where all the user created files are stored
automatically unless we specifically instruct the computer to save at a different specified location.
o It is a special folder where the system stores user’s files, pictures, music, download contents etc.
Recycle Bin
o In windows Operating System, Recycle Bin is a temporary folder which holds files and folders
before it is permanently deleted from the storage devices.
o The Recycle Bin only stores, files deleted from hard drives, not from removable media, such as
memory cards and pen drive/ flash drives.
o It also doesn’t store files deleted from network drives.
o We can restore the files from the recycle bin to its original deleted location automatically or we can
cut the files from recycle bin and paste it at desired location.
o It is also possible that we can bypass the recycle bin and the files deleted will be directly removed
from the computer without moving it to the recycle bin. For this we keep holding the SHIFT key and
delete the desired files. Sometimes it might happen that we need to restore the files which has
been deleted accidentally.
o We can restore the deleted files from recycle bin. For this we follow these steps : –
Double click the icon of recycle bin on the desktop
Choose a file which we want to restore back and right-click on it and then click restore
To restore all the files in the recycle bin click on restore all items.
Taskbar
o The taskbar is the long horizontal bar/area at the bottom of desktop screen.
o It is used to launch and monitor running applications.
o It has four main sections:
(i) The Start button area which opens the start menu.
(ii) The Quick Launch Toolbar area which allows us to open programs quickly with a single click.
(iii) The Middle Section allows us to see the documents and programs which we have opened and
to switch between them.
(iv) The Notification Area, which includes a small system application such as clock and icons which
shows the status of certain programs and computer settings.
(A) On the basis of the operating system’s architecture, it is divided into the following categories –
Discover more
database
computers
processes
device
multimedia
Computer hardware
computer
operating system
Multimedia
(B) On the basis of the use/application of the operating system, it is divided into the following
categories –
Discover more
computer system
device
database
real-time
processes
Server
Real-time systems
Database
(C) On the basis of the number of users use the operating system at a time, it is divided into the
following categories –
(D) On the basis of tasks performed at a time, the operating system is divided into the following
categories –
Discover more
Computer hardware
hardware
Multimedia
computers
Software
Process
servers
server
multimedia
software
(E) On the basis of the use of the operating system in a computer network environment, it is divided
into the following categories –
(F) On the basis of the function performed by an operating system individually, it is divided into the
following categories –
(G) On the basis of use whether it is free/paid or source code is available or not, an operating system
is divided into the following categories –
(H) On the basis of use in different types of devices/machines, the Operating system is divided into
the following categories –
(I) On the basis of the technique used by the operating system, it is divided into the following
categories –
(A)
(a) Network Operating System
Definition
o
A network operating system is a collection of system software (OS) and associated/related
protocols that allow a set of autonomous computers, which are interconnected by certain
mediums in a computer network, to be used together in a coordinated, convenient and
cost-effective manner.
Features/Characteristics
o
This operating system works in a computer network environment.
Network operating systems focus on the use of remote services and resources existing in a
network of computer systems.
In this operating system, there are multiple individual computers (usually called client
computers) that are connected with at least a single or more remote/server machines with
a network medium either with air or cable.
Here, each client computer has its own same/different private operating system, memory,
processor, etc. along with a global remote system having a powerful specific operating
system.
In this environment, each user normally works on his/her own client system.
In this environment, users are typically aware of where/ which system, each of their files is
kept and must move files from one system to another with explicit file transfer commands
with the help of a remote machine.
The system/environment has little or no fault tolerance i.e. if 10% of the personal
computers crash, only 10% of the users are out of business.
Advantages
o
In this environment, users can access the various resources of the network hosts.
It provides, controlling access to particular resources to authorized users.
The use of remote resources by the users appears to be identical to the use of local
resources.
It provides up-to-the-minute network documentation online.
Definition
o
Distributed operating systems are operating systems for a network of autonomous
computers connected by a communication network.
Features
o
A distributed operating system controls and manages the hardware and software resources
of a distributed system such that its users view the entire system as a powerful monolithic
computer system.
When a program is executed in a distributed system, the user is not aware of where the
program is executed or of the location of the resources accessed.
Distributed operating systems focus on the effective utilization of resources in distributed
computing environments.
Users have unique IDs in a distributed system.
Distributed systems today cover a wide spectrum of computer hardware, software, and
topological configurations; resource-sharing services range from offline access to real-time
access and topologies vary from locally distributed to geographically distributed.
Examples – LOCUS, etc.
Structure
o
A distributed operating system consists of similar to a traditional operating system, viz.,
process synchronization, deadlocks, process scheduling, file systems, interprocess
communication, memory and buffer management, failure recovery, etc.
They lack both shared memory and a physical global clock, and unpredictable
communication delays make the design of distributed operating systems much more
difficult.
Advantages
o
Cost-effectiveness in both computations and resources.
Resource sharing
Resource sharing is the main motivation for distributed systems.
The nature of the distribution and sharing of resources in a distributed system has
changed due to advances in networking technology.
Reliability
Reliability is the availability of a resource despite failures in a system. A distributed
environment enhanced the availability of resources through the Operating System,
redundancy of resources, and communication paths. For example, the availability
of a disk resource can be increased by having two or more disks located at different
sites in the system. If one disk is unavailable due to a disk or site failure, a program
can use some
other disk. The availability of a data resource, e.g., a file, can be similarly enhanced
by keeping copies of the file at various sites in the system.
Communication
Communication between users at different locations is greatly facilitated by using a
distributed system easily. Communication continued even when users migrate to
different sites of a system.
Incremental growth
Distributed systems are capable of incremental growth, i.e., It is the capabilities of
a system (mainly its processing power) can be enhanced at a price proportional to
the nature and size of the enhancement.
Definition
o
A multiprocessor operating system is an operating system that can manage and distribute
workload across multiple processors (also known as CPUs or cores) in a computer system.
This allows multiple tasks to be executed simultaneously, which can significantly improve
system performance and efficiency.
A multiprocessor operating system is a type of operating system that is designed to work
with computer systems that have multiple processors or CPUs (Central Processing Units).
Features
o
A typical multiprocessor system consists of a set of processors that share a set of physical
memory blocks over an interconnection network.
A user is not aware of the presence of multiple processors and their interconnection
network in a multiprocessor system.
The main goal of a multiprocessor operating system is to distribute the workload between
different processors in an efficient and balanced manner, thereby improving the overall
system performance.
1. Task scheduling: The operating system must be able to assign tasks to different
processors and ensure that each processor is being utilized efficiently.
2. Memory management: The operating system must be able to manage memory
access between multiple processors and ensure that there are no conflicts or data
corruption.
3. Inter-processor communication: The operating system must provide a mechanism
for communication between different processors, allowing them to share data and
synchronize their operations.
4. Load balancing: The operating system must be able to balance the workload
between different processors to ensure that each processor is working at optimal
capacity.
5. Scalability: The operating system must be scalable, allowing for the addition of
more processors as the system grows in size.
Examples of multiprocessor operating systems include Linux, Unix, Windows Server, and
macOS Server.
These operating systems are designed to work with multi-core processors, which are
processors that contain multiple processing cores on a single chip.
In a multiprocessor system, the operating system must manage the allocation of system
resources such as memory, input/output devices, and processing power among the
processors. It must also ensure that tasks are executed in a coordinated and efficient
manner, without interference or conflicts between them.
One of the key challenges/disadvantages is designing of a multiprocessor operating system.
This requires careful synchronization and coordination mechanisms, such as locks and
semaphores, to ensure that only one processor can access a shared resource at any given
time.
Structure
o
The design of multiprocessor operating systems is the same as in a traditional operating
system. But process synchronization, task scheduling, memory management, and
protection and security become more complex because the main memory is shared by
many physical processors.
Function
o
A multiprocessor operating system controls and manages the hardware and software
resources such that users view the entire system as a powerful uni-processor system.
Types
(I)
o
A loosely coupled multiprocessor system is a type of computer system in
which multiple processors or computers work together to perform a task,
but they operate independently and are not tightly interconnected.
In other words, the processors in a loosely coupled system do not share a
memory or communicate with each other in real time.
A loosely coupled multiprocessor system is a type of computer architecture
where multiple processors are interconnected to work together as a whole
in a distributed manner.
Each processor in a loosely coupled system has its own memory and can
execute tasks independently.
When multiple processors need to work together on a task, they
communicate through a network or other communication mechanism,
such as message passing or shared files. This allows the processors to
operate independently and work on different tasks simultaneously,
without interfering with each other.
A loosely coupled multiprocessor system is typically used for tasks that can
be divided into smaller subtasks that can be executed independently by
different processors.
Loosely coupled systems are more scalable and flexible than tightly
coupled systems.
They are often used in applications that require a high degree of
parallelism and can tolerate some latency in communication between
processors.
Examples of loosely coupled multiprocessor systems include clusters of
computers connected by a network, and multi-core processors within a
single computer.
These systems can be used for a variety of applications such as scientific
simulations, data processing, and web serving.
o
A tightly coupled multiprocessor system(TCMP) is a computer system or
architecture that has multiple processors that share a common memory
and are closely/tightly interconnected (such that they can rapidly exchange
data and synchronize their activities), allowing them to work together as a
single system.
In a tightly coupled multiprocessor system, the processors can access and
modify the shared memory concurrently, which enables parallel processing
and improves system performance.
In a tightly coupled multiprocessor system, the processors communicate
with each other through shared memory, message passing, or a
combination of both.
One example of a tightly coupled multiprocessor system is a symmetric
multiprocessing (SMP) system, where each processor has equal access to
the system’s resources and can perform any task assigned to it.
Tightly coupled multiprocessor systems are commonly used in high-
performance computing, scientific simulations, and data processing
applications.
Overall, tightly coupled multiprocessor systems offer advantages in terms
of performance and scalability but also require careful design to ensure
proper synchronization, load balancing, and fault tolerance.
One advantage of a TCMP is that it can achieve higher performance than a
single-processor system since the workload can be divided among multiple
processors.
The disadvantage of this system is its design and programming which can
be complex since the processors must communicate effectively and
coordinate their activities to avoid conflicts and ensure the consistency of
data. Also, TCMPs can be more expensive to build and maintain than single
processor systems, due to the need for specialized hardware and software.
TCMPs are widely used in high-performance computing environments,
such as supercomputers and data centers, where the ability to process
large amounts of data quickly is essential.
(II)
o
A homogeneous multiprocessor system is a computer system or
architecture that has multiple identical/same-type processors or CPUs, that
share a common memory and are interconnected by a bus or network or
connected together as a single shared-memory architecture.
In this system, all the processors have the same access capabilities to
memory and other system resources, and they are all capable of executing
the same set of instructions or same tasks.
The main advantage of a homogeneous multiprocessor system is that it
allows for parallel processing of tasks, which can significantly improve
system performance. This is because each processor can work on a
separate task simultaneously.
The goal of this system is to achieve maximum performance by dividing a
task into smaller parts that can be executed simultaneously. The
processors work together to process the task, which results in faster
execution times.
Homogeneous multiprocessor systems are commonly used in high-
performance computing, scientific simulations, and large-scale data
processing applications. They are also used in embedded systems, such as
in automotive or aerospace applications, where high reliability and fault
tolerance are required.
The disadvantage of this system is – (i) Its complex design and
programming, as it requires specialized knowledge of parallel processing
techniques and efficient memory management. (II) Additionally,
communication between processors can also be complex, as data must be
shared and coordinated between them to ensure the proper execution of
tasks. (III) Also, a homogeneous multiprocessor system is that it can be
difficult to scale. As the number of processors in the system increases, the
amount of communication between the processors can become a
bottleneck, which can limit the overall performance of the system.
o
A heterogeneous multiprocessor system (HMP) is a type of computer
system that contains multiple processing units of different types,
architectures, or capabilities, such as CPUs, GPUs, DSPs(Digital Signal
Processors), or FPGAs, that work together to execute a program or a
variety of programs.
The use of heterogeneous processors in a single system allows for better
performance and power efficiency for a wide range of applications, as each
processor type can handle tasks that they are specialized in, and can work
together in parallel to execute tasks faster.
In an HMP system, the processors are interconnected using a high-speed
communication network, such as a shared memory bus, a point-to-point
network, or a high-speed interconnect, to enable efficient data sharing and
task allocation.
HMP systems are becoming more prevalent in high-performance
computing and embedded systems due to their ability to provide efficient
parallel computing capabilities while reducing power consumption.
HMP systems require specialized hardware and software to effectively
manage and distribute tasks among the different processing units. The
software used to manage HMP systems is typically designed to take
advantage of the unique capabilities of each processor type and distribute
tasks according to their needs.
The examples/use/applications of HMP systems are – scientific
simulations, machine learning, and video processing, among others. HMP
systems are commonly used in applications that require high-performance
computing, such as scientific simulations, machine learning, and video
processing.
The main advantage of an HMP system is its ability to leverage the
strengths of different processor architectures to achieve high performance
and energy efficiency. For example, CPUs are generally good at handling
serial tasks, GPUs(Graphics Processor Units) are well-suited for parallel
tasks, and FPGAs(field-programmable gate arrays) can be programmed for
specialized tasks with high data throughput requirements.
Disadvantage: The designing and programming of HMP systems can be
complex and challenging, as it requires expertise in multiple processor
architectures and their associated programming models.
(III)
o
A shared memory multiprocessor system is a type of computer architecture
that allows multiple processors to access a shared pool of memory.
In this type of system, each processor has its own cache memory, but they
all share a common physical memory space.
The benefit of a shared memory system is that it can provide high
performance and flexibility for certain types of applications that require a
lot of memory access.
This system is used in scientific simulations, database management, and
multimedia processing.
With a shared memory system, multiple processors can access the same
data simultaneously, which can reduce the overall processing time.
One of the main disadvantages is that multiple processors of this system
do not try to access the same memory location at the same time, which
can lead to data corruption or inconsistencies. Another challenge with this
shared memory system is scalability,i.e., as the number of processors in a
system increases, the amount of contention for shared resources also
increases, which can lead to performance degradation. To overcome this
challenge, some shared memory systems use a hierarchical architecture
that allows for better scalability by partitioning the memory and cache into
smaller, more manageable units. Thus, this system requires careful design
and management to ensure that they operate efficiently and reliably.
o
A Distributed Memory Multiprocessor System(DMMS) is a type of parallel
computing architecture in which multiple processors are connected to each
other and have their own local memory.
In this architecture, the system is divided into multiple nodes or
processors, each having its own local memory and processing power.
In a DMMS, each processor has its own local memory, which is directly
accessible to that processor. However, to access data stored in another
processor’s memory, a message-passing mechanism is required. This is
because each processor has its own address space and cannot directly
access the memory of another processor.
The communication between the nodes is achieved through a network that
connects them, and each node can access data stored in its local memory
and in the memory of other nodes in the system. The processors work
together to solve a common problem, with each processor contributing its
processing power and memory.
They provide high performance and scalability, allowing users to process
large amounts of data and perform complex calculations in a shorter
amount of time.
Distributed Memory Multiprocessor Systems are used in a wide range of
applications, including scientific simulations, big data analytics, and
machine learning.
One common example of Distributed Memory Multiprocessor System is a
cluster, which is a group of computers connected to each other through a
network and working together to solve a common problem. Another
example is a supercomputer, which is a highly powerful computer
consisting of multiple processors and a large amount of memory. Some
popular DMMS systems include MPI (Message Passing Interface), OpenMP,
and OpenACC.
The programming model for a DMMS is more complex.
o
Uniform Memory Access (UMA) is a computer memory architecture in
which the time it takes to access any location in memory is essentially the
same, regardless of the memory location or the processor accessing the
memory.
In UMA, all processors have equal access to a shared memory pool, which
is typically implemented using a single shared bus.
In UMA systems, all processors can access any memory location with equal
latency, which simplifies programming and makes it easier to develop
parallel algorithms. In other words, the access time to any memory
location in UMA is uniform for all processors in the system.
In this architecture, each processor has equal access to the memory and
can execute programs concurrently.
One of the disadvantages of this system is that the UMA system suffers
from memory contention and scalability issues as the number of
processors increases, as multiple processors may be contending for the
same memory location at the same time, leading to performance
degradation. It may also suffer from scalability issues as the number of
processors increases.
UMA architectures are commonly used in symmetric multiprocessing
(SMP) systems, where multiple processors share a common memory and a
common bus or interconnect.
In UMA systems, the memory access time is not dependent on the location
of the memory module or the processor accessing it. This is in contrast to
Non-Uniform Memory Access (NUMA) systems, where the access time to a
memory location may depend on the location of the processor relative to
the memory module.
The advantage of UMA architecture is that it is simple to design and
implement, and it allows for efficient communication between processors
because they share the same memory.
The disadvantage of the UMA system is that, as the number of processors
in the system increases, the memory bandwidth may become a bottleneck,
leading to performance degradation. Additionally, UMA systems may not
be scalable beyond a certain number of processors, making NUMA a more
suitable architecture for larger systems.
Examples of UMA systems are multi-socket servers, high-performance
computing clusters, and some shared-memory parallel computers.
(B)
(a) Embedded Operating System
Definition
o
Embedded Operating Systems are small OSs, designed to operate on small machines or
devices with less autonomy.
An embedded operating system (OS) is a specialized type of operating system designed to
run on embedded systems, which are small, dedicated computer systems that perform
specific tasks or functions.
Features
o
They are very compact and extremely efficient by design and are able to operate with a
limited amount of resources.
Embedded operating systems are typically optimized for low-power, limited-memory, and
low-cost devices with specific hardware requirements.
Embedded operating systems are commonly used in a wide variety of applications,
including industrial automation, automotive systems, consumer electronics, medical
devices, and more i.e., these systems are commonly used in various electronic devices such
as smartphones, digital cameras, routers, and medical equipment.
Embedded operating systems are typically designed to be lightweight, fast, and reliable,
with a small footprint. They are usually optimized for specific hardware architectures and
are often customized to fit the requirements of the embedded system.
They are typically tailored to the specific requirements of the application, which can
include real-time processing, low latency, and high reliability.
Embedded systems are designed to perform specific tasks and often have limited
resources, such as memory and processing power.
Some popular embedded operating systems are:
1. FreeRTOS(A popular real-time operating system (RTOS) that is open-source and
designed for microcontrollers.)
2. ThreadX
3. VxWorks(A real-time operating system that is commonly used in mission-critical
applications such as aerospace, defense, and medical devices.)
4. Linux Embedded(Embedded distributions such as OpenEmbedded, Yocto Project,
Minix 3, and Buildroot are designed specifically for embedded systems.)
5. Android (for mobile devices and other embedded systems)
6. Windows Embedded (Windows CE for industrial and embedded systems)
o
Advantages
o
It makes small machines or devices operable.
History
o
In the early 1950s, General Motors Research Laboratories (GMRL) introduced the
first Single-Stream batch processing systems.
Batch OS was very popular in the 1970s.
Introduction
o
Early computers, before batch OS, were very expensive, due to less processor utilization.
Also, the wasted time is due to poor scheduling and setup time.
Batch processing is the most primitive type of operating system.
This operating system is termed “batch operating” because the input data (job) are first
collected and filtered as groups or batches of a similar type having similar needs into sets
of records and each batch is processed sequentially as a unit(group). Thus, a Batch is
defined as a group of jobs with similar needs. The batch operating system allows users to
form batches.
Each user prepares its jobs/batch on an offline device like a punch card and submits it to
the computer operator for further processing. Now, OS keeps a number of jobs in memory
and executes them without any manual information, and jobs are processed in the order of
submission, i.e., first come first served fashion.
Definition
o
The batch operating system is a type of earlier and comparatively less used os in which os
executes each batch sequentially, processing all jobs of a batch and considering them as a
single process.
Features
o
The principle behind the simple batch-processing system is the use of a piece of software
known as the monitor.
In batch OS, the user no longer has direct access to the processor. Instead, the user submits
the job on cards or tape to a computer operator, who batches the jobs together
sequentially and places the entire batch on an input device, for use by the monitor. Each
program is constructed to branch back to the monitor when it completes processing, at
which point the monitor automatically begins loading the next program.
With a batch operating system, processor time alternates between the execution of user
programs and the execution of the monitor.
Examples are – Payroll Systems, Transactions Processing Systems, forecasting, statistical
analysis, & large scientific number-crunching programs, etc.
Types
There are mainly two types of batch operating systems. These are:-
(i) Simple Batched System: Use the basic concept of batch during operations.
Advantages
o
They don’t require any special or specific hardware to input data.
Scheduling & memory management in a batch system is very simple.
Batch operating systems load less stress on the CPU and include minimal user interaction.
Here, batch processes are completed even in the absence of humans’ direct interaction.
Disadvantages
o
Debugging is very difficult in batch systems.
A user cannot interact with the batch operating system after the submission of the job or
during the processing i.e. the computer system and the user has no direct interaction.
Computer operators must have full knowledge of batch systems.
In a batch system, whenever an error occurs in one of the jobs of a batch then, all the
remaining jobs get affected i.e; they have to wait until the error is resolved.
Due to turnaround delays & offline debugging, the batch is not very convenient for
program development.
Definition
o
An OS is used in time-critical environments where data must be processed extremely
quickly in time because the output influences immediate decisions.
Features
o
Real-time systems are used for space flights, airport traffic control, industrial processes,
sophisticated medical equipment, telephone switching, etc.
A real-time system must be 100 percent responsive in time.
In real-time systems, the correctness of the computations not only depends upon the
logical correctness of the computation but also upon the time at which the results are
produced.
In this system, if the timing constraints of the system are not met, system failure is said to
have occurred.
Real-time operating systems are especially used in environments where a large number of
events, mostly external to the computer system, must be accepted & processed in a short
time or within certain deadlines.
A primary objective of real-time systems is to provide quick event-response times and thus
meet the scheduling deadlines. User convenience & resource utilization are of secondary
concern to real-time system designers.
It is common for a real-time system to be expected to process bursts of thousands of
interrupts per second without missing a single event. Such requirements usually cannot be
met by multi-programming alone, and real-time operating systems usually rely on some
specific policies & techniques for doing their job. The work of Multitasking operations in
this OS is accomplished by scheduling processes for execution independently of each other.
Each process is assigned a certain level of priority that corresponds to the relative
importance of the event that it services. The processor is normally allocated to the highest-
priority process among those that are ready to execute. Higher-priority processes usually
preempt execution of the lower-priority processes. This form of scheduling called priority-
based preemptive scheduling is used by a majority of real-time systems.
Unlike, time-sharing OS, the process population in real-time systems is fairly static, and
there is comparatively little moving of programs between primary & secondary storage. On
the other hand, processes in real-time systems tend to cooperate closely, thus
necessitating support for both separation & sharing of memory.
The time-critical device management is one of the main characteristics of real-time
systems. In addition to providing sophisticated forms of interrupt management & I/O
buffering, real-time operating systems often provide system calls to allow user processes to
connect themselves to interrupt vectors & to service events directly. File management is
usually found only in larger installations of real-time systems.
There are some embedded real-time systems that also exist such as an onboard
automotive controller, which may not even have any secondary storage, to fulfill the
requirements.
The primary objective of file management in real-time systems is usually the speed of
access, rather than the efficient utilization of secondary storage
Examples are – PSOS, VRTX, etc.
Definition
o
Features
o
In this, OS divides the computer’s resources such as CPU time, memory, and storage among
the users or applications that are running simultaneously.
These operating systems provide various tools and features that allow users to manage
their resources and applications effectively, such as process scheduling, file sharing, and
network management.
In a time-sharing system, each user has a terminal or computer connected to the main
computer, and the OS allocates a small amount of CPU time to each user in a round-robin
fashion.
This OS allows multiple users to use the computer simultaneously and interact with it in
real-time as if they were the only user.
Time-sharing OSs are particularly useful in situations where many users need access to a
single computer, such as in universities, research labs, or business environments.
They also facilitate collaborative work and allow users to share data and resources.
Examples of time-sharing OSs include Unix, Linux, and Windows Server.
Most time-sharing systems use time-slicing scheduling.
Time-sharing is a popular representative of multi-programmed, multi-user systems.
The mode of operation in this OS generally provides a quick response time to interactive
programs.
In addition to general program-development environments, many large computer-aided
design & text-processing systems belong to this category.
Examples are – Multics, etc.
o
Definition
o
A database operating system (DBOS) is a specialized type of operating system that is
specifically designed to manage the database and control access to a database.
Features
o
The primary function of a DBOS is to ensure the efficient and secure management of the
database.
It also provides tools for users to manipulate data and perform queries.
It provides an interface between the user and the database management system (DBMS)
and is responsible for performing tasks such as file management, memory management,
and access control.
A database operating system plays a critical role in the efficient and reliable management
of data in large-scale enterprise environments.
A DBOS also provides features such as transaction management, concurrency control, and
backup and recovery. These features ensure the reliability and availability of the database
even in the face of hardware failures, power outages, or other disruptions.
The choice of a particular DBOS depends on the specific needs and requirements of an
organization or application.
A DBOS provides a layer of software between the database applications and the hardware.
DBOS is designed to handle large amounts of data and to provide efficient access to that
data.
It includes features such as concurrency control, backup and recovery, security, and query
optimization.
It also provides a set of tools for managing the database, such as utilities for loading data
into the database, monitoring database activity, and tuning database performance.
These systems are widely used in many enterprise environments to support mission-critical
applications such as e-commerce, customer relationship management, and financial
transactions.
Examples
o
Some examples of database operating systems include Oracle, Solaris, IBM DB2, MySql,
PostgreSQL, and Microsoft SQL Server. These database packages perform several complex
specialized operations like operating systems such as data storage, retrieval, concurrency
control, backup and recovery, partitioning, clustering, advanced data compression,
encryption, integration with other database products, and query optimization.
It offers features like data warehousing, data mining, and analysis services.
It offers features like replication, high availability, and support for multiple storage engines.
It is also widely used for mission-critical applications that require high availability,
scalability, and performance.
Definition
o
A multimedia operating system is a type of operating system that is specifically designed to
handle complex multimedia tasks such as audio, video, graphics, and animation.
Features
o
These tasks require a significant amount of processing power and memory, as well as
specialized software and hardware components such as sound cards and video cards.
Multimedia operating systems typically provide features such as:
1.
1.
1.
1. Support for various multimedia formats and codecs.
2. High-performance multimedia playback and recording capabilities.
3. Advanced graphics and animation rendering capabilities.
4. Audio and video editing tools.
5. Network streaming and sharing features.
o
Examples of multimedia operating systems include Windows Media Center, Mac OS X,
Android, and Linux distributions such as Ubuntu Studio and AV Linux. These operating
systems are often used by professionals in the media and entertainment industries, as well
as by home users who want to create or consume multimedia content on their computers.
(C)
(a) Single User Operating System
Definition
o
A single-user operating system is a type of operating system designed to be used by only
one user at a time.
Features
o
This OS is designed to work on personal computers and does not require complex
configurations or a network connection.
The single-user operating system provides a single GUI interaction interface for the user to
interact with the computer hardware and other applications.
Single-user operating systems typically provide features such as file management, security,
and system updates.
This OS also supports a wide range of software applications, including productivity tools,
multimedia applications, and games.
Single-user operating systems are popular among individual users who require a simple and
intuitive interface to perform everyday tasks on their personal computers.
This type of operating system is typically found on personal computers and workstations,
where a single user interacts with the system using a keyboard, mouse, and display.
Single-user operating systems are designed to provide a user-friendly interface that allows
users to easily navigate and control the system.
These operating systems are designed to provide a wide range of functionality to individual
users, including web browsing, multimedia playback, document editing, and gaming.
Some popular examples of single-user operating systems are Microsoft Windows, Apple
macOS, and Linux distributions like Ubuntu and Fedora.
Introduction
o
Multi-user os is also called Multi-access OS.
Definition
o
Multi-user operating system is a powerful OS that supports or handles multiple users’ work
at a time.
Multi-access operating systems allow simultaneous access to a computer system through
two or more terminals.
Features
o
Multi-user operating systems provide facilities for the maintenance of individual user
environments & therefore require user accounting.
An example of a Multi-user OS is (a dedicated transaction processing system, such as) an
airline or railway ticket reservation system, that supports hundreds of active terminals
under the control of a single program.
(D)
(a) Uni/Single Tasking Operating System
History
o
These operating systems were prevalent in early computer systems, where the
computational resources were limited, and there was a need to optimize system
performance.
“Uni Tasking” is a term that refers to an operating system that only allows a single task to
run at a time.
While uni-tasking systems were common in the early days of computing, modern operating
systems are typically multi-tasking, meaning they can run multiple processes or tasks
concurrently.
Definition
o
Uni-tasking operating systems are those that allow only one task to be executed at a time.
Features
o
In a uni-tasking operating system, the entire system’s resources are dedicated to executing
a single task, which makes the system more efficient.
However, this system cannot handle multiple tasks simultaneously, which can be a
significant disadvantage in modern computer systems.
Uni-tasking operating systems may still be used in specialized systems, such as embedded
systems, where the focus is on executing a single task efficiently and reliably. However, for
most general-purpose computing tasks, multi-tasking operating systems are the best ones.
In such a system, the CPU (central processing unit) is dedicated to executing a single
process or task, and no other tasks can be executed until the current task is completed or
interrupted.
While uni-tasking systems have the advantage of simplicity and predictable behavior, they
may not be suitable for modern computing environments that require more complex and
demanding workloads.
One of the best examples of Uni tasking OS is MS-DOS.
Introduction
o
The terms multitasking & multiprocessing are often used interchangeably, although in
multiprocessing more than one CPU is involved whereas in multitasking only one CPU is
involved, it switches from one program to another so quickly that it gives the appearance
of executing all of the programs at the same time.
Definition
o
Multitasking OS is a type of OS that allows more than one program to run concurrently.
This ability to execute more than one task at the same time, a task being a program is
called multitasking.
Features
o
A multitasking OS is distinguished from others by its ability to support the concurrent
execution of two or more active processes.
Multitasking is usually implemented by maintaining the code & data of several processes in
memory simultaneously.
There are two basic types of multitasking: –
(i) preemptive multitasking (ii) cooperative multitasking
(i) In preemptive multitasking, the OS parcels out CPU time slices to each program. OS/2, Windows 95,
Windows NT, & UNIX are examples of preemptive multitasking.
Definition
o
A multiprogramming operating system permits multiple programs to be loaded into
memory & execute the programs concurrently/at the same time.
Multiprogramming is a technique used in operating systems to allow multiple programs to
run simultaneously on a computer system.
Features
o
MPOS enables a computer to run more than one program concurrently, which can increase
the overall throughput, efficiency, and utilization of the system’s resources.
In a multiprogramming operating system, the CPU is shared among multiple programs
through a technique called time-sharing. The operating system assigns a slice of time to
each program, allowing it to execute a few instructions before switching to another
program. This process continues in a round-robin fashion until all programs have
completed their execution.
Multiprogramming operating systems can also take advantage of features such as virtual
memory, which allows programs to use more memory than is physically available by
swapping data between the memory and the hard disk.
A multiprogramming operating system is designed to manage the execution of multiple
programs by allowing them to share the CPU and other system resources.
In a multiprogramming operating system, the CPU is shared among multiple programs, with
each program being allocated a certain amount of time to execute. This allows several
programs to run at the same time, improving the overall efficiency of the system.
To manage the execution of multiple programs, the operating system must be able to
allocate and deallocate system resources such as memory, CPU time, and I/O devices. This
is typically done using a scheduler, which decides which program to execute next based on
various factors such as priority, resource availability, and time slice.
One of the main advantages of a multiprogramming operating system is that it can improve
system utilization by allowing multiple programs to run simultaneously, which can reduce
idle time and increase throughput. However, it can also introduce complexities such as
resource contention, synchronization, and scheduling overhead, which must be managed
by the operating system.
Examples are – Windows O/S, UNIX or Linux O/S, etc.
(E)
(a) Client’s Operating System
Definition
o
A client operating system is an operating system that is designed/installed to run on
personal computers, and other devices used by individuals and uses services provided by a
server.
Features
o
It is designed to be used by individual users, rather than managing server resources.
The choice of client operating system will depend on the needs and preferences of the
user, as well as the applications and services that the user needs to access.
A client operating system provides an interface that allows users to interact with the
computer and its applications.
Some common examples of client operating systems are Windows, Mac OS, Linux,
Android, and iOS.
Definition
o
A server operating system is a type of operating system designed to run on servers, which
are computers that provide services to other computers or devices on a network.
Features
o
Server Operating Systems are the ones that run on the dedicated machine servers.
Server Operating Systems allow sharing of multiple resources like hardware, files, or print
services.
Web pages are stored on a dedicated server to handle requests and responses.
Server operating systems are typically more robust and stable than desktop operating
systems and are designed to handle heavier workloads and manage multiple users and
applications simultaneously.
A server operating system is an operating system that is specifically designed to operate on
server hardware and manage the resources of a server.
This OS often includes features such as security and networking tools, virtualization
capabilities, and remote administration capabilities.
Choosing the right server operating system depends on factors such as the type of
applications and services that will be running on the server, hardware requirements, and
user preferences.
A server operating system is different from a desktop operating system in that it is
optimized for running server applications, providing network services, and managing data
storage.
Examples of popular server operating systems are:- Microsoft Windows Server[to provide
features such as Active Directory, web server support, and virtualization.], macOS Server[to
provide file sharing, email server support, and VPN connectivity], Linux (such as CentOS,
Ubuntu, Red Hat, and Debian), Unix (such as Solaris, AIX, and HP-UX), FreeBSD(A Unix-like
operating system that is often used on servers, particularly for web hosting).
o
(F)
(a) Multithreaded Operating System
Introduction
o
Threads are lightweight processes that share the same memory space and can
communicate with each other more efficiently than separate processes.
Definition
o
A multithreaded operating system is an operating system that can handle multiple threads
of execution concurrently.
A multithreaded operating system is a type of operating system that supports the
execution of multiple threads of execution within a single process.
Features
o
Multithreaded OS allows different parts of a single program(thread) to run concurrently.
In a multithreaded operating system, a single process can have multiple threads of
execution that can run simultaneously on different CPU cores or processors.
For this OS, the programmer must carefully design the program in such a way that all the
threads can run at the same time without interfering with each other.
In a multithreaded operating system, each thread can execute independently of the others,
allowing for improved performance and responsiveness.
In addition to improving performance and responsiveness, multithreading also allows for
better resource utilization.
With multiple threads of execution, an operating system can allocate resources more
efficiently, reducing the amount of time that a system spends idle or waiting for resources
to become available.
Multithreading allows an operating system to handle multiple tasks simultaneously,
improving the overall performance and responsiveness of the system.
Multithreaded operating systems are common in modern computing systems, including
desktop computers, servers, and mobile devices.
The concept of multithreading allows a program to divide its work into smaller, more
manageable parts, and execute these parts concurrently. For example, in a web server, a
separate thread can be used to handle each incoming connection, allowing multiple clients
to be served simultaneously. Similarly, in a database management system, a separate
thread can be used to handle each query, improving the overall throughput of the system.
Similarly again, if a user is running a word processing application, a multithreaded
operating system can allow the user to type, while at the same time, the system can
perform background tasks such as saving the document, checking for spelling errors, and
updating the user interface.
Examples of multithreaded operating systems are Linux, Windows, macOS, and Android.
UNIX
UNIX operating systems are used in widely-sold workstation products from Sun Microsystems, Silicon
Graphics, IBM, and a number of other companies.
The UNIX environment and the client/server program model were important elements in the development
of the Internet and the reshaping of computing as centered in networks rather than in individual
computers.
Linux, a UNIX derivative available in both free software and commercial versions, is increasing in popularity
as an alternative to proprietary operating systems.
UNIX is written in C.
Both UNIX and C were developed by AT&T and freely distributed to government and academic institutions,
causing it to be ported to a wider variety of machine families than any other operating system. As a result,
UNIX became synonymous with open systems.
UNIX is made up of the kernel, file system and shell (command line interface). The major shells are the
Bourne shell (original), C shell and Korn shell.
The UNIX has more than 600 commands that manipulate data and text in every way conceivable.
UNIX is widely used in mission critical applications for client/server and transaction processing systems.
The multiple UNIX versions are widely used as Sun’s Solaris, Digital’s UNIX, HP’s HP-UX, IBM’s AIX and SCO’s
UnixWare.
A large number of IBM mainframes also run UNIX applications, because the UNIX interfaces were added to
MVS and OS/390, which have obtained UNIX branding.
LINUX
Programming
computers
Science
Computer
WINDOWS
MACINTOSH
The Macintosh (often called the Mac OS), introduced in 1984 by Apple Computer, was the first widely-sold
personal computer with a graphical user interface (GUI).
The Mac OS was designed to provide users with a natural, intuitively understandable, and, in general, user-
friendly computer interface. This includes the mouse, the use of icons or small visual images to represent
objects or actions, the point-and-click and click-and-drag actions, and a number of window operation ideas.
Microsoft was successful in adapting user interface concepts first made popular by the Mac in its first
Windows operating system.
The primary disadvantage of the Mac is that there are fewer Mac applications on the market than for
Windows. However, all the fundamental applications are available, and the Macintosh is a perfectly useful
machine for almost everybody.
The Macintosh has its own operating system, Mac OS which, in its latest version is called Mac OS X.
Originally built on Motorola’s 68000 series microprocessors.
Mac versions today are powered by the PowerPC microprocessor, which was developed jointly by Apple,
Motorola, and IBM. While Mac users represent only about 5% of the total numbers of personal computer
users.
Macs are highly popular and almost a cultural necessity among graphic designers and online visual artists
and the companies they work for.
3. DOS consumes comparatively quite low power. Windows consumes comparatively high power.
8. Multimedia operation is not supported in DOS. Multimedia operation is fully supported by Windows OS.
DOS supports only single operation in single Windows supports single or multiple operations in
10. window/interface at a time called SDI (Single single/multiple windows/interfaces at a time called MDI
Document Interface). (Multiple Document Interface).
Deadlock is a situation in which no process/job got Starvation is a situation in which the low priority
1. blocked and no process/job proceeds i.e. they all are process/job got blocked and the high priority
inert. processes proceed.
2. Deadlock is an infinite waiting process and is uncertain. Starvation is a long waiting but certain and not infinite.
In deadlock, the requested/used resource is blocked by In starvation, the requested resource is continuously
4.
the other process. be used by the higher priority processes.
2. The size of pages are fixed. The size of segments are not fixed.
3. Here, procedures and data cannot be separated. Here, procedures and data can be separated.
5. Here, the type of the fragmentation is internal. Here, the type of fragmentation is external.
Here, the size of the page is determined by available Here, the size of the page is determined by the user
6.
memory. activity.
It executes multiple same or different tasks at a time It executes multiple same or different tasks at a time
1.
using a single processor. using multiple processors.
5. It allows fast switching among various tasks. It allows smooth processing of multiple tasks at once.
It requires more time to execute multiple tasks as It requires less time for multiple jobs processing as
6.
compared to multiprocessing. compared to multitasking.
The process is an instance of computer A program is a collection of instructions that mainly performs a
1.
program that is being executed. specific task when executed by the computer.
Open source software (OSS) is a type of software whose source code is available to the public. This means
that anyone can view, modify, and distribute the software as they see fit. OSS is an alternative to the
traditional proprietary software model, where the source code is kept secret and users are restricted in their
ability to modify or share the software.
OSS is rooted in the belief that software should be a community resource. Developers from around the
world can collaborate on OSS projects, making improvements and adding features that benefit everyone.
This collaborative model also allows for rapid innovation, as the combined efforts of many developers can
often outpace those of a single company.
However, it’s important to note that ‘open source’ doesn’t necessarily mean ‘free’. While many OSS
projects are available for free, developers can choose to charge for their software if they wish. The key
difference is that the source code must be available to users, allowing them to modify and distribute the
software as they see fit.
The concept of open source software may seem like a new phenomenon, but its roots go back several
decades. In the early days of computing, software was often shared freely among academics and
researchers. The idea was to foster collaboration and accelerate innovation, much like the open source
movement today.
However, as the software industry began to take shape in the 1970s and 1980s, companies started to see the
value in proprietary software. They began to keep their source code secret, selling licenses to users that
allowed them to use the software but not to modify or distribute it. This shift towards proprietary software
marked a significant change in the software industry, and it was not without controversy.
In the late 1980s and early 1990s, a group of developers led by Richard Stallman began to push back
against this trend. They believed that software should be a communal resource, and they started to develop
free software that anyone could modify and distribute. This marked the beginning of the modern open
source movement, and it has been growing ever since.
Open Source vs. Closed Source Software
Closed source software (also called custom code or proprietary code) is protected, preventing unauthorized
users from using or viewing the source code. Any attempts to modify, delete, or copy closed code
components can result in legal repercussions or voiding the warranty.
In contrast, open source software allows users to copy, delete, or modify code components, keeping the
code open so that others can include this code in their program, according to the licensing granted by the
open source code creators. Let’s explore the key differences between open and closed source software.
Support
Closed source software is typically developed and maintained by a single company or organization. This
centralized development structure allows for dedicated customer support teams that can quickly address
any issues or concerns that users may encounter. Closed source software often comes with comprehensive
documentation, tutorials, and training resources to assist users in maximizing its potential.
On the other hand, open source software is developed by a community of contributors who collaborate to
improve and enhance the software. While this distributed development model fosters innovation and
creativity, it can sometimes lead to limited support options.
However, open source communities are known for their vibrant and helpful nature. Users can seek
assistance from forums, discussion groups, and online communities where experienced developers and
enthusiasts are eager to lend a helping hand. Open source software also benefits from continuous updates
and improvements driven by a diverse range of contributors.
Usability
Closed source software is often designed with a focus on user-friendliness and intuitive interfaces.
Extensive user testing and interface design considerations are conducted to ensure a smooth and seamless
user experience. Closed source software tends to have a consistent look and feel, making it easier for users
to navigate and accomplish their tasks efficiently.
Open source software, on the other hand, may have varying degrees of usability. Since it is developed by a
diverse community, usability can sometimes be a hit or miss. However, many open source projects have
made significant strides in improving their user interfaces and overall user experience. Some projects even
have dedicated teams focused on enhancing usability.
Moreover, open source software provides users with the freedom to customize and tailor the software to
their specific needs, which can greatly enhance usability for those willing to invest time and effort.
Security
Closed source software often benefits from a dedicated team of security experts who identify and patch
vulnerabilities. The proprietary nature of closed source software can make it more difficult for malicious
actors to exploit security loopholes, as the source code is not freely available for scrutiny. Additionally,
closed source software companies often prioritize security audits and regular updates to ensure that their
software remains robust against emerging threats.
Open source software involves a different approach to security. The transparency of the source code allows
a global community of developers to scrutinize it for vulnerabilities and suggest improvements. This
collaborative effort can lead to rapid identification and resolution of security issues.
Open source software communities often have a culture of sharing security best practices and collectively
addressing vulnerabilities. However, users must still exercise caution and ensure that they are using official
releases from trusted sources, and that the open source projects they are using are regularly maintained.
Cost
Closed source software is typically associated with licensing fees or subscription-based pricing models.
These costs can vary greatly depending on the complexity and intended use of the software. While closed
source software may require an upfront investment, it often comes bundled with customer support, regular
updates, and additional features.
Open source software is generally available for free. Users can download, use, and modify the software
without any licensing costs. This makes OSS an attractive option for individuals and organizations with
limited budgets. However, it is important to consider the total cost of ownership when evaluating open
source software. While the software itself may be free, there may be additional costs associated with
implementation, customization, and ongoing support.
Flexibility
Closed source software offers limited flexibility. Users are generally limited to the features and
functionalities provided by the software vendor. Customization options may be limited, and users may have
to rely on the vendor’s roadmap for future enhancements. While closed source software can often meet the
needs of a broad range of users, it may not be suitable for those seeking extensive customization or
integration capabilities.
By providing users with access to the source code, open source software allows for customization and
modification to meet specific needs. This flexibility is particularly valuable for organizations with unique
requirements or those seeking to integrate the software with existing systems and workflows. Users can
tailor the software to their precise needs, whether it involves adding new features, removing unnecessary
ones, or integrating with other software solutions.
Open source licenses are legal agreements that specify how an OSS can be used, modified, and distributed.
These licenses are crucial for maintaining the open nature of OSS. Without them, developers could
potentially restrict how their software is used, undermining the whole concept of open source.
There are many different types of open source licenses, each with its own set of conditions and
permissions. Some licenses are very permissive, allowing users to do almost anything with the software.
Others, known collectively as ‘copyleft’ licenses, are more restrictive, placing certain limitations on how
the software can be modified or distributed. Including components with copyleft licenses in your software
can severely limit your ability to commercialize or monetize the software.
The Apache License is a permissive license created by the Apache Software Foundation. It allows users to
use, modify, and distribute the software, provided that they comply with the terms of the license. This
includes providing attribution to the original author and including a copy of the license in any distributions.
One of the notable features of the Apache License is its grant of a patent license. This means that
contributors automatically grant a license to their patents that would be infringed by the software. This
feature helps to protect users and developers from potential patent litigation.
MIT License
The MIT License is named after the Massachusetts Institute of Technology, where it originated. The MIT
License is simple and straightforward, allowing users to do whatever they want with the software, as long
as they provide attribution and include a copy of the license.
The MIT License does not include a patent license, which differentiates it from the Apache License.
However, its simplicity and permissiveness have made it a popular choice for many open source projects.
BSD License
The BSD License originated at the University of California, Berkeley. There are several variations of the
BSD License, but they all allow users to use, modify, and distribute the software, provided they meet
certain conditions.
These conditions usually involve providing attribution and including a copy of the license. Some versions
of the BSD License also include a non-endorsement clause, which prohibits the use of the names of the
contributors to endorse or promote products derived from the software.
The GNU General Public License (GPL) was created by Richard Stallman and the Free Software
Foundation, and it has been instrumental in the growth of the open source movement.
The GPL requires that any derivative works must also be licensed under the GPL. This ensures that the
software remains open and free for everyone to use, modify, and distribute. But at the same time, it
severely restricts the ability to commercialize any software based on GPL-licensed technology.
Affero GPL (AGPL)
The GNU Lesser General Public License (LGPL) is a less restrictive version of the GPL. It allows
developers to use LGPL-licensed code in proprietary software, provided that they make the source code for
the LGPL components available.
The LGPL is a popular choice for libraries and other software components that are designed to be used in a
variety of projects. It provides a balance between openness and flexibility. However, like the GPL, the
LGPL is still a copyleft license, which means that it imposes certain restrictions on how the software can
be used.
The Eclipse Public License (EPL) is used by the Eclipse Foundation for their widely-used Eclipse IDE,
among other projects. While the EPL is a copyleft license, it is less restrictive than the GPL. It allows
developers to use EPL-licensed code in proprietary software, without requiring that the entire software be
EPL-licensed.
The Mozilla Public License (MPL) is used by the Mozilla Foundation for their Firefox web browser and
other projects. The MPL is similar to the EPL in that it allows for the use of MPL-licensed code in
proprietary software.
The MPL has a unique feature called the ‘file-level copyleft’. This means that modifications to existing
MPL-licensed files must be released under the MPL, but new files can be licensed as the developer sees fit.
The Business Source License (BSL) is a type of software license that offers a unique approach to balancing
the benefits of open source and proprietary software. It was created by MariaDB Corporation founder
Michael “Monty” Widenius. Here are some key features of the BSL:
Time-delayed source code release: Under a BSL license, the source code of the software is eventually made
available as open source after a predetermined delay period. This period is typically a few years, after
which the software automatically converts to a standard open-source license, such as the General Public
License (GPL).
Limited use rights initially: During the delay period, users have limited rights to use, modify, and share the
software. The license may impose certain restrictions, such as on the size of the organization that can use it
for free, or the types of uses (like non-commercial) that are permitted without a paid license.
Commercial use: The BSL allows the original creators to monetize the software during the delay period.
Companies or individuals who wish to use the software for purposes beyond what is allowed for free under
the BSL typically need to purchase a commercial license.
Promotion of open source ideals: After the delay period ends and the software becomes open source
under a traditional open-source license, the community benefits from access to the source code. This
delayed release mechanism promotes open source ideals while allowing the creators to derive commercial
benefits initially.
Key Use Cases for Open Source Software
Open source plays an important role in the domain of web servers and hosting. The Apache HTTP Server,
one of the most prominent OSS projects, has been a cornerstone of the internet, powering a significant
portion of the web’s sites. Its success is attributed to its robustness, flexibility, and active community
support. Additionally, Nginx, another OSS web server, has gained popularity for its high performance,
scalability, and efficient handling of concurrent connections, making it an ideal choice for high-traffic
websites.
In hosting environments, open source tools like the LAMP stack (Linux, Apache, MySQL,
PHP/Python/Perl) enable businesses and developers to deploy web applications efficiently. The stack’s
components are all open source, ensuring a cohesive and cost-effective platform for web development and
hosting.
Database Management
In the realm of database management, OSS offers a range of solutions that cater to different needs, from
lightweight databases like SQLite to powerful, enterprise-grade systems like PostgreSQL and MySQL.
These open source databases provide users with the ability to handle data effectively, whether for small
personal projects or large, complex applications.
Open source databases offer high levels of customization and flexibility, allowing businesses to tailor the
database system to their specific requirements. The vibrant communities around these databases contribute
to their reliability and security, with regular updates and patches addressing emerging threats or issues.
In cloud computing, open source technologies have been transformative. Notable examples are OpenStack
for cloud infrastructure management, Docker for containerization and Kubernetes for container
orchestration. These tools offer organizations the ability to build and manage scalable, flexible cloud
environments without being tied to proprietary solutions.
The collaborative nature of open source development accelerates innovation in the cloud computing space,
with new features and integrations being developed at a rapid pace. Furthermore, the transparency and
modifiability of open source software provide users with greater control over their cloud infrastructure.
Real-Time Operating Systems (RTOS) are crucial for applications requiring immediate processing and
response to external events. OSS projects like FreeRTOS and RTLinux provide the foundation for
developing real-time applications in areas such as embedded systems, automotive, and IoT devices.
These open source RTOS platforms allow developers to access and modify the source code, enabling them
to optimize and customize the operating system according to their project’s specific needs. The availability
of source code and a community of developers also helps in identifying and fixing bugs and security issues
quickly.
Security Tools
The open source community has contributed significantly to cybersecurity, offering a wide array of tools
for monitoring, penetration testing, and securing networks. Tools like Wireshark for network protocol
analysis, Zed Application Proxy (ZAP) for dynamic application testing, and Metasploit for security
vulnerability testing, are indispensable to security professionals.
Open source security tools benefit from the collective expertise of a global community. Additionally, these
tools can be customized to suit the unique security needs of each organization.
An open source software policy is a set of rules and guidelines that govern how an organization or
individual uses and contributes to open source software. The policy outlines the practices and procedures
that need to be followed when dealing with open source software to ensure compliance with licenses,
protect intellectual property rights, and maintain the integrity of the software.
The policy also provides guidance on how to contribute to open source projects. This includes how to
submit changes, how to handle disputes, and how to acknowledge the work of others. It’s important to
remember that contributing to open source requires more than writing code. It can also involve reporting
bugs, improving documentation, and helping other users.
Keeping an open source software policy helps in managing risks associated with the use of open source
software. It ensures that the organization is aware of its responsibilities and obligations when using and
distributing open source software. A well-defined policy can also help in decision-making processes,
ensuring that the use of open source software aligns with the organization’s strategic goals.
Open source security is a growing concern in software development organizations, because most enterprise
software projects include thousands of open source components. Here are a few ways to ensure open
source software does not compromise the security of your applications.
Securing open source software requires continuous monitoring for security updates. Many open-source
projects are updated regularly, and it’s crucial to keep track of these updates and apply them promptly.
Updates often contain security patches that fix known vulnerabilities in the software. Failing to apply these
patches can leave your systems exposed to potential threats. Therefore, it is essential to have a system in
place that alerts you when new updates or vulnerabilities are detected.
Implement Open Source Vulnerability Scanning
Vulnerability scanning is a proactive approach to identifying security weaknesses in your OSS. It involves
using specialized tools to scan your software for known vulnerabilities. These tools compare the
components of your software against databases of known vulnerabilities, such as the National Vulnerability
Database (NVD).
Open source vulnerability scanning helps you identify and fix vulnerabilities before they can be exploited.
Regular scanning can also help you understand your software better, allowing you to make informed
decisions about its security. New vulnerabilities are discovered regularly, so continuous scanning is
necessary to ensure your software remains secure.
A binary repository is a storage location where you can keep binary files, the compiled version of your
software. Using a binary repository can significantly improve the security of your open source software.
A binary repository allows you to keep track of all the versions of your software. This makes it easier to
manage updates and patches, ensuring that you are always using the latest, most secure version of your
software.
A binary repository can also help you control access to your software. You can set permissions to
determine who can access your software, preventing unauthorized access and potential security threats.
Software composition analysis (SCA) is a method used to identify open source components within your
software and assess their security risks. It provides a comprehensive view of your software’s composition,
allowing you to understand its security posture better.
SCA tools can identify known vulnerabilities in the open-source components of your software. They can
also check for compliance with licensing requirements, ensuring that you are not violating any open-source
licenses.
By providing a detailed view of your software’s composition, SCA can help you manage your open-source
usage effectively. It allows you to make informed decisions about the open-source components you use,
enhancing the security and compliance of your software.
Now that we’ve covered the important considerations for using open source software in your projects, let’s
review some of the world’s most popular open source software.
Implement GitOps
GitOps is an operational framework that takes DevOps practices a step further, using Git as the single
source of truth for managing infrastructure and application configurations. This approach enhances open
source security by introducing a more controlled, auditable, and automated deployment process.
When implementing GitOps, all changes to the infrastructure and application configurations are made via
Git pull requests. These changes are automatically applied to the production environment using continuous
deployment (CD) tools. This method ensures that every modification is tracked and reviewed, reducing the
risk of unauthorized changes or misconfigurations that could lead to security vulnerabilities.
GitOps minimizes the attack surface by limiting direct access to production environments. By enforcing
changes through a controlled Git workflow, it ensures that only approved and verified updates are
deployed, thereby reducing the chances of introducing vulnerabilities into your software ecosystem.
Related product offering: Octopus Deploy | Continuous Delivery and Deployment Platform
WordPress is an open source content management system that powers approximately 40% of all websites.
It’s known for its simplicity, flexibility, and user-friendly interface. With WordPress, you can create any
type of website, from a personal blog to a full-fledged e-commerce site.
WordPress has an extensive ecosystem of plugins and themes. With thousands of options to choose from,
you can extensively customize your website. And because it’s open source, you have the freedom to
modify and extend WordPress to suit your specific needs.
Drupal
Drupal is an open-source content management system (CMS) used to make websites and applications. It’s
flexible and customizable, allowing you to build and manage different kinds of sites—from simple blogs to
complex user communities.
One of the key strengths of Drupal is its scalability. It’s designed to handle high-traffic sites and has proven
its ability to scale in real-world scenarios at various organizations that manage large volumes of content.
The Drupal community is large and diverse, with hundreds of thousands of active contributors constantly
working on enhancing its features and performance. This active participation ensures that Drupal remains a
robust platform for web development.
Joomla
Official project page: https://www.joomla.org/
Repository: https://github.com/joomla/joomla-cms
GitHub stars: 4.4K
Joomla is another open-source CMS that empowers you to create dynamic websites and applications. It
offers a balance between ease-of-use and extensibility, making it a popular choice among both beginners
and experienced developers.
Joomla offers an application framework that allows developers to create add-ons, extending the power of
Joomla beyond a simple CMS. The Joomla community is large and active. They are committed to keeping
Joomla user-friendly, powerful, and up-to-date.
Ghost
Ghost is an open source blogging platform that’s gaining popularity for its focus on simplicity and
usability. Unlike WordPress, which has evolved into a full content management system, Ghost remains true
to its blogging roots.
Ghost is designed to make blogging as straightforward as possible. Its clean, clutter-free interface lets you
focus on your content. And while it may not have as many plugins and themes as WordPress, Ghost offers
enough customization options to make your blog distinct.
Strapi
Strapi is an open-source headless CMS that allows developers to build, deploy, and manage content-rich
applications in a short time. It’s built on JavaScript, one of the most popular programming languages,
making it a familiar environment for many developers.
As a headless CMS, Strapi decouples the front-end and back-end development. This gives developers the
freedom to use any technology stack they prefer for the front-end, while Strapi manages the back-end.
Docker is an open source platform that automates the deployment of applications inside software
containers. It provides an additional layer of abstraction and automation of operating-system-level
virtualization on Windows and Linux.
Docker utilizes the resource isolation features of the Linux kernel (such as cgroups and kernel namespaces)
and a union-capable file system (like OverlayFS) to allow independent containers to run within a single
Linux instance, avoiding the overhead of starting and maintaining virtual machines.
Docker can package and run an application in a loosely isolated environment called a container. This
isolation and security allow you to run many containers simultaneously on a given host. Containers are
lightweight and contain everything needed to run the application, ensuring that software runs reliably when
moved from one computing environment to another.
Kubernetes
Kubernetes is a portable, extensible, open source platform for managing containerized workloads and
services. It supports declarative configuration and highly flexible automation. It has a large, rapidly
growing ecosystem, with Kubernetes services, support, and tools widely available.
Kubernetes provides a platform for automating deployment, scaling, and operations of application
containers across clusters of hosts. Originally developed by Google and donated to the Cloud Native
Computing Foundation (CNCF), it offers a solution for container orchestration, increasing the scalability,
availability, and efficiency of applications.
**Learn more in these detailed guides:
Introduction to Kubernetes
Kubernetes architecture**
OpenShift is a family of containerization software products developed by Red Hat. Its flagship product is
the OpenShift Container Platform—an on-premises platform as a service built around Docker containers,
which are orchestrated and managed by Kubernetes on a foundation of Red Hat Enterprise Linux.
OpenShift provides developers with an integrated development environment (IDE) for building and
deploying Docker-formatted containers, and then managing them with the open source Kubernetes
container orchestration platform. This platform has been built with enterprise requirements like security in
mind, offering automated updates and scaling.
Rancher
Official project page: https://www.rancher.com/
Repository: https://github.com/rancher/rancher
GitHub stars: 21.8K
Rancher enables organizations to deploy and manage Kubernetes at scale. It’s designed to address the
operational and security challenges of managing multiple Kubernetes clusters across different
infrastructure providers.
The Rancher community helps make Kubernetes accessible to everyone. Their contributions focus on
simplifying the complexities of Kubernetes, making it easier for more organizations to adopt and benefit
from this technology.
TensorFlow is an open source software library for high performance numerical computation. Its flexible
architecture allows easy deployment of computation across a variety of platforms (CPUs, GPUs, TPUs),
and from desktops to clusters of servers to mobile and edge devices.
Originally developed by researchers and engineers from the Google Brain team, TensorFlow has become
popular among developers for machine learning and artificial intelligence projects. It has a broad
ecosystem of tools, libraries, and community resources that lets researchers push new experiments in
ML/AI and developers build and deploy ML/AI-powered applications.
PyTorch
PyTorch is an open source machine learning library developed by Facebook’s AI Research lab (FAIR). It
has gained popularity in the research community for its flexibility, ease of use, and dynamic computational
graph. PyTorch’s design is intuitive, particularly for those familiar with Python programming, making it a
preferred choice for both academic research and development in industrial applications.
One of the key strengths of PyTorch is its user-friendly interface. It offers a straightforward platform for
building deep learning models, allowing researchers and developers to focus more on the design aspect of
their models rather than the technical intricacies of the library. Its dynamic computational graph, where the
graph is built at runtime, offers more flexibility and ease for making changes and debugging. This feature
is particularly useful for complex models and novel architectures in research.
Keras is a high-level neural networks API, written in Python and capable of running on top of TensorFlow,
CNTK, or Theano. It was developed with a focus on enabling fast experimentation.
Keras is designed for human beings emphasizing usability and developer experience. Its API is intuitive
and easy to experiment with, making it approachable for newcomers while also robust enough for expert
practitioners. It provides clear and actionable feedback for user errors and has extensive documentation and
developer guides.
OpenCV
The library has more than 2,500 optimized algorithms, which includes a comprehensive set of both classic
and state-of-the-art computer vision and machine learning algorithms. These algorithms can be used to
detect and recognize faces, identify objects, classify human actions in videos, track camera movements,
track moving objects, and extract 3D models of objects.
GPTScript
GPTScript is a scripting language designed to enable users to automate tasks and interact with a Large
Language Model (LLM) using OpenAI. It combines the simplicity of natural language with the power of
traditional scripting tools like Bash and Python, as well as the capability to make HTTP service calls. This
unique blend allows users to execute a wide range of tasks, from mundane automation to complex database
operations, using intuitive language commands.
The language’s primary strength lies in its natural language-based syntax, which significantly reduces the
learning curve for new users. This accessibility makes GPTScript an ideal tool for a variety of applications,
including retrieval-augmented generation (RAG), task automation, and the creation of agents and
assistants. Additionally, it can be used for data analysis and the integration of vision, image, and audio
processing tasks into scripts. Another notable feature is its ability to manage memory and construct
chatbots.
HuggingFace Transformers
HuggingFace Transformers is a library that provides thousands of pre-trained models to perform tasks on
texts such as classification, information extraction, question answering, summarization, translation, and
text generation in over 100 languages. Its aim is to make state-of-the-art NLP easier to use for everyone.
Transformers provide a straightforward way to use models like BERT, GPT-2, T5, and others.
HuggingFace has become a go-to library for anyone working with textual data, thanks to its simplicity,
efficiency, and broad community support. Whether you’re a researcher, developer, or a company, it offers
cutting-edge capabilities with just a few lines of code.
Fast.ai
Official project page: https://www.fast.ai/
Repository: https://github.com/fastai/fastai
GitHub stars: 25.5K
Fast.ai is a deep learning library that provides high-level components aimed to enable fast experimentation
with deep neural networks. It sits on top of PyTorch and is designed to make deep learning more accessible
and easier to implement for practitioners.
Fastai’s library is structured around best practices with a focus on educational purposes, making it not just
a tool for production but also a learning platform for those new to deep learning. It automates many of the
tedious aspects of training deep learning models, making the process more intuitive and allowing
developers and researchers to move faster through their projects.
Redis, which stands for Remote Dictionary Server, is an open source, in-memory data structure store. It is
used as a database, cache, and message broker. One of the reasons for Redis’s popularity is its versatility. It
supports a wide range of data structures, including strings, hashes, lists, and sets.
Redis’s in-memory nature allows it to deliver ultra-fast read and write operations. This makes it an
appropriate choice for high-performance applications. With numerous contributors from around the world,
the Redis community is continually improving and innovating.
PostgreSQL
PostgreSQL is an open source relational database system. It is known for its robustness, scalability, and full
compliance with SQL standards. PostgreSQL supports a broad array of data types, including complex ones
like arrays and hstore (for storing key-value pairs).
One of the key features of PostgreSQL is its extensibility. You can define your own data types, operators,
and functions. This allows you to tailor the database system to your specific needs. And like Redis,
PostgreSQL has a strong community backing it, ensuring its continuous development and improvement.
MongoDB
MongoDB is an open source NoSQL database that provides high performance, high availability, and easy
scalability. It works on the concept of collections and documents, which makes it flexible and adaptable to
real-world data.
MongoDB offers the ability to handle large amounts of data with diverse structures. It’s suitable for dealing
with big data applications and real-time analytics. MongoDB’s horizontal scaling capability ensures that as
your data grows, your database can grow with it.
MariaDB
MariaDB is an open source relational database management system that is a fork of MySQL. It was created
by the original developers of MySQL after concerns over its acquisition by Oracle Corporation. MariaDB
is designed to maintain high compatibility with MySQL, ensuring a drop-in replacement capability with
library binary parity and exact matching with MySQL APIs and commands.
MariaDB has a few features that set it apart from MySQL. It includes a variety of storage engines,
including high-performance ones like Aria, ColumnStore, and Spider. It also offers advanced replication
for scaling.
Cassandra
Apache Cassandra is an open source distributed database system designed for managing large amounts of
data across many commodity servers, providing high availability with no single point of failure. Cassandra
offers robust support for clusters spanning multiple datacenters, with asynchronous masterless replication
allowing low latency operations for all clients.
Cassandra’s strengths revolve around its ability to handle large amounts of data, its resilient nature, and its
exceptional performance. Because it’s designed for distributed environments, it’s highly scalable and can
seamlessly grow with your data.
Related product offering: Configu: NetApp Instaclustr Data Platform | Open-Source Data Infrastructure
Platform
Apache Hadoop is an open-source framework designed for distributed storage and processing of large data
sets across clusters of computers using simple programming models. It is particularly known for its ability
to handle petabytes and exabytes of data. Hadoop breaks down big data into smaller pieces to be processed
in parallel, significantly speeding up data processing.
A key feature of Hadoop is its Hadoop Distributed File System (HDFS), which allows for high throughput
access to application data and is suitable for applications with large data sets. The framework also includes
Hadoop YARN, a job scheduling and cluster resource management system, and Hadoop MapReduce, a
programming model for large-scale data processing.
Spark is known for its speed, ease of use, and general-purpose nature. It can run programs up to 100x faster
than Hadoop MapReduce in memory, or 10x faster on disk. Spark also supports a wide array of tasks,
including SQL queries, streaming data, machine learning, and graph processing.
Apache Spark
PySpark
Elasticsearch
Elasticsearch is a highly scalable open-source search engine and analytics engine. It allows you to store,
search, and analyze big volumes of data quickly and in near real time. Elasticsearch is generally used as the
underlying engine/technology that powers applications with complex search features and requirements.
Elasticsearch uses a document-oriented approach, storing data in JSON format. This makes it easy to store,
search, and manage complex data structures. It’s built on top of the Apache Lucene library, offering a full-
text search engine with a powerful combination of features for search queries.
Elasticsearch is part of the ELK Stack (Elasticsearch, Logstash, Kibana), which is widely used for log
analysis in IT environments. This stack provides a powerful platform for searching, analyzing, and
visualizing data in real-time.
Note: In 2021 Elasticsearch transitioned from an Apache 2.0 open source license to the SSPL 1.0 license,
which is more restrictive. The software is still free and source code is available, but there are some
limitations on how it can be deployed in production.
OpenSearch is an open source search and analytics suite derived from Elasticsearch and Kibana. It provides
a scalable solution for search, observability, and data analysis across various types of datasets. OpenSearch
offers powerful full-text search capabilities, real-time data analysis, and advanced visualization tools.
One of OpenSearch’s key strengths is its flexibility and extensibility, allowing users to tailor it to their
specific needs. It supports a wide range of plugins and integrations, making it suitable for diverse use cases
such as log analytics, security monitoring, and more. The project is actively maintained by a large
community, ensuring ongoing improvements and feature additions.
Kafka
Official project page: https://kafka.apache.org/
Repository: https://github.com/apache/kafka
GitHub stars: 26.3K
Apache Kafka is an open source streaming platform that provides real-time data feeds. It’s designed to
handle data streams from multiple sources and deliver them to multiple consumers. Kafka is known for its
high throughput, reliability, and replication capabilities, making it a popular choice for big data and real-
time analytics applications.
Kafka’s distributed architecture ensures that it can handle massive amounts of data. It also provides fault
tolerance, ensuring that data feeds remain up and running even in the event of a failure. Kafka’s
community is active and enthusiastic, continually contributing to its development and refinement.
Related product offering: Configu: NetApp Instaclustr Data Platform | Open-Source Data Infrastructure
Platform
Related technology update: [Blog] How to Get the Most out of Your Kafka Cluster
Splunk
Splunk is a software platform widely used for monitoring, searching, analyzing, and visualizing machine-
generated data in real time. It processes and indexes the data to make it searchable, and then enables users
to create graphs, reports, alerts, dashboards, and visualizations.
Although not entirely open source, Splunk offers a free version with limited functionality, popular in the
community for data analysis and operational intelligence. It’s particularly valued for its ability to make
sense of large amounts of unstructured data, providing insights that can drive operational performance and
business results.
Learn more in the detailed guide to Splunk Cloud
Slurm
Slurm (Simple Linux Utility for Resource Management) is an open source, fault-tolerant, and highly
scalable cluster management and job scheduling system for large and small Linux clusters. Slurm is used
by many of the world’s supercomputers and computer clusters to manage job scheduling.
Slurm prioritizes scalability and portability. It allocates exclusive and/or non-exclusive access to resources
(compute nodes) to users for some duration of time. It also provides a framework for starting, executing,
and monitoring work on the set of allocated nodes. Finally, it arbitrates contention for resources by
managing a queue of pending work.
OpenTelemetry provides a unified standard for telemetry data collection across various programming
languages and platforms. This makes it easier for developers to implement observability and maintain
consistency in their monitoring practices.
The project supports multiple data sources and export formats, making it adaptable to different monitoring
backends such as Prometheus, Jaeger, and Zipkin. It is backed by a large community and achieved
Graduated status in the Cloud Native Computing Foundation (CNCF).
Zipkin
Zipkin is an open source distributed tracing system that helps developers monitor and troubleshoot latency
issues in microservice architectures. It collects and visualizes trace data, allowing teams to understand the
flow of requests through their systems and identify performance bottlenecks.
Zipkin supports various data collection methods and integrates with numerous platforms, making it a
versatile tool for observing complex, distributed systems. It provides an intuitive UI for viewing traces and
includes powerful features like trace sampling and customizable data storage options. Zipkin is widely used
in the industry and is backed by a robust community of contributors.
Vault HashiCorp
Vault by HashiCorp is an open source tool designed to securely manage secrets and protect sensitive data.
It allows organizations to control access to passwords, API keys, certificates, and other secrets in a
centralized and auditable manner. Vault offers robust features like dynamic secrets, encryption as a service,
and secret leasing and revocation, making it an essential tool for managing secrets in modern infrastructure.
Vault supports a wide range of authentication methods and provides fine-grained access control through
policies. It also offers high availability, scaling to meet the needs of large organizations, and is backed by
an active community of contributors and users.
Related technology update: [Webinar] Secure CD at scale with Octopus Deploy and HashiCorp Vault
Definition
Open source software (OSS) is software that is distributed with its source code, making it available for use,
modification, and distribution with its original rights. Source code is the part of software that most
computer users don’t ever see; it’s the code computer programmers manipulate to control how a program
or application behaves. Programmers who have access to source code can change a program by adding to
it, changing it, or fixing parts of it that aren’t working properly. OSS typically includes a license that
allows programmers to modify the software to best fit their needs and control how the software can be
distributed.
What's in your code?
Explore insights into the current state of open source security and get recommendations for securing your
open source supply chain
The idea of making source code freely available originated in 1983 from an ideological movement
informally founded by Richard Stallman, a programmer at MIT. Stallman believed that software should be
accessible to programmers so they could modify it as they wished, with the goal of understanding it,
learning about it, and improving it.i Stallman began releasing free code under his own license, called the
GNU Public License. This new approach and ideology surrounding software creation took hold and
eventually led to the formation of the Open Source Initiative in 1998.i
The Open Source Initiative (OSI) was created to promote and protect open source software and
communities.ii In short, the OSI acts as a central informational and governing repository of open source
software. It provides rules and guidelines for how to use and interact with OSS, as well as providing code
licensing information, support, definitions, and general community collaboration to help make the use and
treatment of open source understandable and ethical.ii
Open source code is usually stored in a public repository and shared publicly. Anyone can access the
repository to use the code independently or contribute improvements to the design and functionality of the
overall project.
OSS usually comes with a distribution license. This license includes terms that define how developers can
use, study, modify, and most importantly, distribute the software.iii According to the Black Duck®
KnowledgeBase, five of the most popular licenses are:
MIT License
GNU General Public License (GPL) 2.0—this is more restrictive and requires that copies of modified code
are made available for public use
Apache License 2.0
GNU General Public License (GPL) 3.0
BSD License 2.0 (3-clause, New or Revised)—this is less restrictiveiv
When source code is changed, OSS must include what was altered as well as the methods involved. Depending on
the license terms, the software resulting from these modifications may or may not be required to be made
available for free.iii
GNU/Linux
Mozilla Firefox
VLC media player
SugarCRM
GIMP
VNC
Apache web server
LibreOffice
jQuery
Is open source software bug-free?
The short answer is no. With multiple parties making modifications and improvements, it’s inevitable that
open source software will contain quality, performance, and security flaws. However, the broad base of
code contributors can also mean that bugs are identified and fixed faster.
No matter the type of software—open source or commercial—code flaws will exist. The main difference is
who is responsible for fixing the bugs; for commercial software, vendors are responsible, whereas the
consumer is responsible for open source software. With a robust set of AppSec tools and practices in place,
OSS can be easily secured.
What are the differences between open source and closed source software?
Factors Open source Closed source
Price Available for nominal or zero Cost varies based upon the scale of the
licensing and usage charges. software.
Freedom to Completely customizable but it Change requests must be made to the company
customize depends on the open source license. selling the software. This includes bug fixes,
Requires in-house expertise. features, and enhancements.
User-friendliness Typically less user-friendly, but it can Typically more user-friendly. As a for-profit
depend on the goals of the project and product, adoptability and user experience are
those maintaining it. often key considerations.
After-sales support Some very popular pieces of open Dedicated support teams are in place. The level
source software (e.g., OSS distributed of service available depends on the service-
by Red Hat or SUSE) have plenty of level agreement (SLA).
support. Otherwise, users can find help
through user forums and mailing lists.
Security Source code is open for review by The company distributing the software (i.e.,
anyone and everyone. There is a software owner) guarantees a certain level of
widespread theory that more eyes on support, depending on the terms of the SLA.
the code makes it harder for bugs to Because the source code is closed for review,
survive. However, security bugs and there can be security issues. If issues are found,
flaws may still exist and pose the software distributor is responsible for fixing
significant risk. them.
Vendor lock-in No vendor lock-in due to the In most cases, large investments are made in
associated cost. Integration into proprietary software. Switching to a different
systems may create technical vendor or to an open source solution can be
dependency. costly.
Stability This will depend on the current user Older, market-based solutions are more stable.
base, the parties maintaining the New products have similar challenges as open
software, and the number of years in source products. If a distributor discontinues an
the market. application, the customer may be out of luck.
Popularity Some open source solutions are very In some industries, proprietary software is
popular and are even market leaders more popular, especially if it has been in the
(e.g., Linux, Apache). market for many years.
Total cost of TCO is lower and upfront due to TCO is much higher and depends on the size of
ownership (TCO) minimal or no usage cost, and depends the user base.
on the level of maintenance required.
Community The community participating in Closed community.
participation development, review, critique, and
enhancement of the software is the
essence of open source.
Interoperability This will depend on the level of This will depend on the development standards.
with other open maintenance and goals of the group,
source software but it is typically better than closed
source software.
Tax calculation Difficult due to undefined monetary Definite.
value.
Enhancements or Can be developed by the user if Request must be made to the software owner.
new features needed.
Suitability for OSS might not be technically well- Most proprietary software goes through
production designed or tested in a large-scale multiple rounds of testing. However, things can
environment production environment. still go wrong when deployed in a production
environment.
Financial The financial industry tends to avoid Financial institutions prefer proprietary
institution open source solutions. If used, a software.
considerations vetting process must take place.
Warranty No warranty available. Best for companies with security policies
requiring a warranty and liability indemnity.
While open source software offers a multitude of benefits, it introduces a whole new level of software risk
management. It is critical that an organization utilizing OSS, or acquiring codebases that contain OSS in a
merger or acquisition, truly understand what is in their code so they can effectively manage and secure it.
The Black Duck solution suite offers complete open source coverage, so you can use OSS confidently.
If you want to learn more about open source risk and how to mitigate it, here are some steps you can take:
1. Read the annual "Open Source Security and Risk Analysis" (OSSRA) report to understand the current state
of open source vulnerabilities and risks.
2. Research open source risk management organizations and consulting firms that can provide guidance and
tools for identifying and addressing open source risks in your own organization.
3. Look for articles, blogs, and webinars online that offer tips and best practices for managing open source
risk.
4. Get in touch with experts in the field of open source risk management, to get personalized advice and
guidance tailored to your organization's specific needs.
5. Consider implementing automated solutions for open source management and security, to help you detect
and resolve vulnerabilities, and to stay compliant with open source licenses.
By taking these steps, you can learn more about open source risk and take the necessary steps to mitigate it,
ensuring the security and compliance of your organization's software.
What are the Black Duck offerings for open source software security?
Software composition analysis (SCA) tools help teams manage the security, quality, and license
compliance risks that come with the use of open source and third-party code in applications and containers.
SCA helps you understand what’s in your code, and provides a comprehensive Software Bill of Materials
(SBOM).
Black Duck Audit Services provide fast analysis of open source, legal, security, and quality risks for
merger and acquisition due diligence or internal reporting. Black Duck offers several audits.
Open source and third-party code audit. This provides a complete open source bill of materials for the
target codebase, and shows all open source components and associated license obligations and conflict
analysis.
Open source risk assessment. This uses Black Duck Security Advisories to deliver a detailed view of open
source risks in the codebase, including known security vulnerabilities. The assessment result can serve as a
high-level action plan to prioritize research and potential remediation actions.
Web services and API risk audit. This lists the external web services used by an application, offering insight
into potential legal and data privacy risks. Armed with this data, you can quickly evaluate web services risks
across three key categories: governance, data privacy, and quality.
Open source software (OSS) is a type of software whose source code is available to the public. This means
that anyone can view, modify, and distribute the software as they see fit. OSS is an alternative to the
traditional proprietary software model, where the source code is kept secret and users are restricted in their
ability to modify or share the software.
OSS is rooted in the belief that software should be a community resource. Developers from around the
world can collaborate on OSS projects, making improvements and adding features that benefit everyone.
This collaborative model also allows for rapid innovation, as the combined efforts of many developers can
often outpace those of a single company.
However, it’s important to note that ‘open source’ doesn’t necessarily mean ‘free’. While many OSS
projects are available for free, developers can choose to charge for their software if they wish. The key
difference is that the source code must be available to users, allowing them to modify and distribute the
software as they see fit.
The concept of open source software may seem like a new phenomenon, but its roots go back several
decades. In the early days of computing, software was often shared freely among academics and
researchers. The idea was to foster collaboration and accelerate innovation, much like the open source
movement today.
However, as the software industry began to take shape in the 1970s and 1980s, companies started to see the
value in proprietary software. They began to keep their source code secret, selling licenses to users that
allowed them to use the software but not to modify or distribute it. This shift towards proprietary software
marked a significant change in the software industry, and it was not without controversy.
In the late 1980s and early 1990s, a group of developers led by Richard Stallman began to push back
against this trend. They believed that software should be a communal resource, and they started to develop
free software that anyone could modify and distribute. This marked the beginning of the modern open
source movement, and it has been growing ever since.
Closed source software (also called custom code or proprietary code) is protected, preventing unauthorized
users from using or viewing the source code. Any attempts to modify, delete, or copy closed code
components can result in legal repercussions or voiding the warranty.
In contrast, open source software allows users to copy, delete, or modify code components, keeping the
code open so that others can include this code in their program, according to the licensing granted by the
open source code creators. Let’s explore the key differences between open and closed source software.
Support
Closed source software is typically developed and maintained by a single company or organization. This
centralized development structure allows for dedicated customer support teams that can quickly address
any issues or concerns that users may encounter. Closed source software often comes with comprehensive
documentation, tutorials, and training resources to assist users in maximizing its potential.
On the other hand, open source software is developed by a community of contributors who collaborate to
improve and enhance the software. While this distributed development model fosters innovation and
creativity, it can sometimes lead to limited support options.
However, open source communities are known for their vibrant and helpful nature. Users can seek
assistance from forums, discussion groups, and online communities where experienced developers and
enthusiasts are eager to lend a helping hand. Open source software also benefits from continuous updates
and improvements driven by a diverse range of contributors.
Usability
Closed source software is often designed with a focus on user-friendliness and intuitive interfaces.
Extensive user testing and interface design considerations are conducted to ensure a smooth and seamless
user experience. Closed source software tends to have a consistent look and feel, making it easier for users
to navigate and accomplish their tasks efficiently.
Open source software, on the other hand, may have varying degrees of usability. Since it is developed by a
diverse community, usability can sometimes be a hit or miss. However, many open source projects have
made significant strides in improving their user interfaces and overall user experience. Some projects even
have dedicated teams focused on enhancing usability.
Moreover, open source software provides users with the freedom to customize and tailor the software to
their specific needs, which can greatly enhance usability for those willing to invest time and effort.
Security
Closed source software often benefits from a dedicated team of security experts who identify and patch
vulnerabilities. The proprietary nature of closed source software can make it more difficult for malicious
actors to exploit security loopholes, as the source code is not freely available for scrutiny. Additionally,
closed source software companies often prioritize security audits and regular updates to ensure that their
software remains robust against emerging threats.
Open source software involves a different approach to security. The transparency of the source code allows
a global community of developers to scrutinize it for vulnerabilities and suggest improvements. This
collaborative effort can lead to rapid identification and resolution of security issues.
Open source software communities often have a culture of sharing security best practices and collectively
addressing vulnerabilities. However, users must still exercise caution and ensure that they are using official
releases from trusted sources, and that the open source projects they are using are regularly maintained.
Cost
Closed source software is typically associated with licensing fees or subscription-based pricing models.
These costs can vary greatly depending on the complexity and intended use of the software. While closed
source software may require an upfront investment, it often comes bundled with customer support, regular
updates, and additional features.
Open source software is generally available for free. Users can download, use, and modify the software
without any licensing costs. This makes OSS an attractive option for individuals and organizations with
limited budgets. However, it is important to consider the total cost of ownership when evaluating open
source software. While the software itself may be free, there may be additional costs associated with
implementation, customization, and ongoing support.
Flexibility
Closed source software offers limited flexibility. Users are generally limited to the features and
functionalities provided by the software vendor. Customization options may be limited, and users may have
to rely on the vendor’s roadmap for future enhancements. While closed source software can often meet the
needs of a broad range of users, it may not be suitable for those seeking extensive customization or
integration capabilities.
By providing users with access to the source code, open source software allows for customization and
modification to meet specific needs. This flexibility is particularly valuable for organizations with unique
requirements or those seeking to integrate the software with existing systems and workflows. Users can
tailor the software to their precise needs, whether it involves adding new features, removing unnecessary
ones, or integrating with other software solutions.
Open source licenses are legal agreements that specify how an OSS can be used, modified, and distributed.
These licenses are crucial for maintaining the open nature of OSS. Without them, developers could
potentially restrict how their software is used, undermining the whole concept of open source.
There are many different types of open source licenses, each with its own set of conditions and
permissions. Some licenses are very permissive, allowing users to do almost anything with the software.
Others, known collectively as ‘copyleft’ licenses, are more restrictive, placing certain limitations on how
the software can be modified or distributed. Including components with copyleft licenses in your software
can severely limit your ability to commercialize or monetize the software.
The Apache License is a permissive license created by the Apache Software Foundation. It allows users to
use, modify, and distribute the software, provided that they comply with the terms of the license. This
includes providing attribution to the original author and including a copy of the license in any distributions.
One of the notable features of the Apache License is its grant of a patent license. This means that
contributors automatically grant a license to their patents that would be infringed by the software. This
feature helps to protect users and developers from potential patent litigation.
MIT License
The MIT License is named after the Massachusetts Institute of Technology, where it originated. The MIT
License is simple and straightforward, allowing users to do whatever they want with the software, as long
as they provide attribution and include a copy of the license.
The MIT License does not include a patent license, which differentiates it from the Apache License.
However, its simplicity and permissiveness have made it a popular choice for many open source projects.
BSD License
The BSD License originated at the University of California, Berkeley. There are several variations of the
BSD License, but they all allow users to use, modify, and distribute the software, provided they meet
certain conditions.
These conditions usually involve providing attribution and including a copy of the license. Some versions
of the BSD License also include a non-endorsement clause, which prohibits the use of the names of the
contributors to endorse or promote products derived from the software.
The GNU General Public License (GPL) was created by Richard Stallman and the Free Software
Foundation, and it has been instrumental in the growth of the open source movement.
The GPL requires that any derivative works must also be licensed under the GPL. This ensures that the
software remains open and free for everyone to use, modify, and distribute. But at the same time, it
severely restricts the ability to commercialize any software based on GPL-licensed technology.
The GNU Lesser General Public License (LGPL) is a less restrictive version of the GPL. It allows
developers to use LGPL-licensed code in proprietary software, provided that they make the source code for
the LGPL components available.
The LGPL is a popular choice for libraries and other software components that are designed to be used in a
variety of projects. It provides a balance between openness and flexibility. However, like the GPL, the
LGPL is still a copyleft license, which means that it imposes certain restrictions on how the software can
be used.
The Eclipse Public License (EPL) is used by the Eclipse Foundation for their widely-used Eclipse IDE,
among other projects. While the EPL is a copyleft license, it is less restrictive than the GPL. It allows
developers to use EPL-licensed code in proprietary software, without requiring that the entire software be
EPL-licensed.
The Mozilla Public License (MPL) is used by the Mozilla Foundation for their Firefox web browser and
other projects. The MPL is similar to the EPL in that it allows for the use of MPL-licensed code in
proprietary software.
The MPL has a unique feature called the ‘file-level copyleft’. This means that modifications to existing
MPL-licensed files must be released under the MPL, but new files can be licensed as the developer sees fit.
The Business Source License (BSL) is a type of software license that offers a unique approach to balancing
the benefits of open source and proprietary software. It was created by MariaDB Corporation founder
Michael “Monty” Widenius. Here are some key features of the BSL:
Time-delayed source code release: Under a BSL license, the source code of the software is eventually made
available as open source after a predetermined delay period. This period is typically a few years, after
which the software automatically converts to a standard open-source license, such as the General Public
License (GPL).
Limited use rights initially: During the delay period, users have limited rights to use, modify, and share the
software. The license may impose certain restrictions, such as on the size of the organization that can use it
for free, or the types of uses (like non-commercial) that are permitted without a paid license.
Commercial use: The BSL allows the original creators to monetize the software during the delay period.
Companies or individuals who wish to use the software for purposes beyond what is allowed for free under
the BSL typically need to purchase a commercial license.
Promotion of open source ideals: After the delay period ends and the software becomes open source
under a traditional open-source license, the community benefits from access to the source code. This
delayed release mechanism promotes open source ideals while allowing the creators to derive commercial
benefits initially.
Open source plays an important role in the domain of web servers and hosting. The Apache HTTP Server,
one of the most prominent OSS projects, has been a cornerstone of the internet, powering a significant
portion of the web’s sites. Its success is attributed to its robustness, flexibility, and active community
support. Additionally, Nginx, another OSS web server, has gained popularity for its high performance,
scalability, and efficient handling of concurrent connections, making it an ideal choice for high-traffic
websites.
In hosting environments, open source tools like the LAMP stack (Linux, Apache, MySQL,
PHP/Python/Perl) enable businesses and developers to deploy web applications efficiently. The stack’s
components are all open source, ensuring a cohesive and cost-effective platform for web development and
hosting.
Database Management
In the realm of database management, OSS offers a range of solutions that cater to different needs, from
lightweight databases like SQLite to powerful, enterprise-grade systems like PostgreSQL and MySQL.
These open source databases provide users with the ability to handle data effectively, whether for small
personal projects or large, complex applications.
Open source databases offer high levels of customization and flexibility, allowing businesses to tailor the
database system to their specific requirements. The vibrant communities around these databases contribute
to their reliability and security, with regular updates and patches addressing emerging threats or issues.
In cloud computing, open source technologies have been transformative. Notable examples are OpenStack
for cloud infrastructure management, Docker for containerization and Kubernetes for container
orchestration. These tools offer organizations the ability to build and manage scalable, flexible cloud
environments without being tied to proprietary solutions.
The collaborative nature of open source development accelerates innovation in the cloud computing space,
with new features and integrations being developed at a rapid pace. Furthermore, the transparency and
modifiability of open source software provide users with greater control over their cloud infrastructure.
Real-Time Operating Systems (RTOS) are crucial for applications requiring immediate processing and
response to external events. OSS projects like FreeRTOS and RTLinux provide the foundation for
developing real-time applications in areas such as embedded systems, automotive, and IoT devices.
These open source RTOS platforms allow developers to access and modify the source code, enabling them
to optimize and customize the operating system according to their project’s specific needs. The availability
of source code and a community of developers also helps in identifying and fixing bugs and security issues
quickly.
Security Tools
The open source community has contributed significantly to cybersecurity, offering a wide array of tools
for monitoring, penetration testing, and securing networks. Tools like Wireshark for network protocol
analysis, Zed Application Proxy (ZAP) for dynamic application testing, and Metasploit for security
vulnerability testing, are indispensable to security professionals.
Open source security tools benefit from the collective expertise of a global community. Additionally, these
tools can be customized to suit the unique security needs of each organization.
An open source software policy is a set of rules and guidelines that govern how an organization or
individual uses and contributes to open source software. The policy outlines the practices and procedures
that need to be followed when dealing with open source software to ensure compliance with licenses,
protect intellectual property rights, and maintain the integrity of the software.
The policy also provides guidance on how to contribute to open source projects. This includes how to
submit changes, how to handle disputes, and how to acknowledge the work of others. It’s important to
remember that contributing to open source requires more than writing code. It can also involve reporting
bugs, improving documentation, and helping other users.
Keeping an open source software policy helps in managing risks associated with the use of open source
software. It ensures that the organization is aware of its responsibilities and obligations when using and
distributing open source software. A well-defined policy can also help in decision-making processes,
ensuring that the use of open source software aligns with the organization’s strategic goals.
Best Practices for Open Source Security and Compliance
Open source security is a growing concern in software development organizations, because most enterprise
software projects include thousands of open source components. Here are a few ways to ensure open
source software does not compromise the security of your applications.
Securing open source software requires continuous monitoring for security updates. Many open-source
projects are updated regularly, and it’s crucial to keep track of these updates and apply them promptly.
Updates often contain security patches that fix known vulnerabilities in the software. Failing to apply these
patches can leave your systems exposed to potential threats. Therefore, it is essential to have a system in
place that alerts you when new updates or vulnerabilities are detected.
Vulnerability scanning is a proactive approach to identifying security weaknesses in your OSS. It involves
using specialized tools to scan your software for known vulnerabilities. These tools compare the
components of your software against databases of known vulnerabilities, such as the National Vulnerability
Database (NVD).
Open source vulnerability scanning helps you identify and fix vulnerabilities before they can be exploited.
Regular scanning can also help you understand your software better, allowing you to make informed
decisions about its security. New vulnerabilities are discovered regularly, so continuous scanning is
necessary to ensure your software remains secure.
A binary repository is a storage location where you can keep binary files, the compiled version of your
software. Using a binary repository can significantly improve the security of your open source software.
A binary repository allows you to keep track of all the versions of your software. This makes it easier to
manage updates and patches, ensuring that you are always using the latest, most secure version of your
software.
A binary repository can also help you control access to your software. You can set permissions to
determine who can access your software, preventing unauthorized access and potential security threats.
Software composition analysis (SCA) is a method used to identify open source components within your
software and assess their security risks. It provides a comprehensive view of your software’s composition,
allowing you to understand its security posture better.
SCA tools can identify known vulnerabilities in the open-source components of your software. They can
also check for compliance with licensing requirements, ensuring that you are not violating any open-source
licenses.
By providing a detailed view of your software’s composition, SCA can help you manage your open-source
usage effectively. It allows you to make informed decisions about the open-source components you use,
enhancing the security and compliance of your software.
Now that we’ve covered the important considerations for using open source software in your projects, let’s
review some of the world’s most popular open source software.
Implement GitOps
GitOps is an operational framework that takes DevOps practices a step further, using Git as the single
source of truth for managing infrastructure and application configurations. This approach enhances open
source security by introducing a more controlled, auditable, and automated deployment process.
When implementing GitOps, all changes to the infrastructure and application configurations are made via
Git pull requests. These changes are automatically applied to the production environment using continuous
deployment (CD) tools. This method ensures that every modification is tracked and reviewed, reducing the
risk of unauthorized changes or misconfigurations that could lead to security vulnerabilities.
GitOps minimizes the attack surface by limiting direct access to production environments. By enforcing
changes through a controlled Git workflow, it ensures that only approved and verified updates are
deployed, thereby reducing the chances of introducing vulnerabilities into your software ecosystem.
Related product offering: Octopus Deploy | Continuous Delivery and Deployment Platform
WordPress is an open source content management system that powers approximately 40% of all websites.
It’s known for its simplicity, flexibility, and user-friendly interface. With WordPress, you can create any
type of website, from a personal blog to a full-fledged e-commerce site.
WordPress has an extensive ecosystem of plugins and themes. With thousands of options to choose from,
you can extensively customize your website. And because it’s open source, you have the freedom to
modify and extend WordPress to suit your specific needs.
Drupal
Official project page: https://www.drupal.org/
Repository: https://github.com/drupal/drupal
GitHub stars: 4K
Drupal is an open-source content management system (CMS) used to make websites and applications. It’s
flexible and customizable, allowing you to build and manage different kinds of sites—from simple blogs to
complex user communities.
One of the key strengths of Drupal is its scalability. It’s designed to handle high-traffic sites and has proven
its ability to scale in real-world scenarios at various organizations that manage large volumes of content.
The Drupal community is large and diverse, with hundreds of thousands of active contributors constantly
working on enhancing its features and performance. This active participation ensures that Drupal remains a
robust platform for web development.
Joomla
Joomla is another open-source CMS that empowers you to create dynamic websites and applications. It
offers a balance between ease-of-use and extensibility, making it a popular choice among both beginners
and experienced developers.
Joomla offers an application framework that allows developers to create add-ons, extending the power of
Joomla beyond a simple CMS. The Joomla community is large and active. They are committed to keeping
Joomla user-friendly, powerful, and up-to-date.
Ghost
Ghost is an open source blogging platform that’s gaining popularity for its focus on simplicity and
usability. Unlike WordPress, which has evolved into a full content management system, Ghost remains true
to its blogging roots.
Ghost is designed to make blogging as straightforward as possible. Its clean, clutter-free interface lets you
focus on your content. And while it may not have as many plugins and themes as WordPress, Ghost offers
enough customization options to make your blog distinct.
Strapi
Strapi is an open-source headless CMS that allows developers to build, deploy, and manage content-rich
applications in a short time. It’s built on JavaScript, one of the most popular programming languages,
making it a familiar environment for many developers.
As a headless CMS, Strapi decouples the front-end and back-end development. This gives developers the
freedom to use any technology stack they prefer for the front-end, while Strapi manages the back-end.
Docker is an open source platform that automates the deployment of applications inside software
containers. It provides an additional layer of abstraction and automation of operating-system-level
virtualization on Windows and Linux.
Docker utilizes the resource isolation features of the Linux kernel (such as cgroups and kernel namespaces)
and a union-capable file system (like OverlayFS) to allow independent containers to run within a single
Linux instance, avoiding the overhead of starting and maintaining virtual machines.
Docker can package and run an application in a loosely isolated environment called a container. This
isolation and security allow you to run many containers simultaneously on a given host. Containers are
lightweight and contain everything needed to run the application, ensuring that software runs reliably when
moved from one computing environment to another.
Kubernetes is a portable, extensible, open source platform for managing containerized workloads and
services. It supports declarative configuration and highly flexible automation. It has a large, rapidly
growing ecosystem, with Kubernetes services, support, and tools widely available.
Kubernetes provides a platform for automating deployment, scaling, and operations of application
containers across clusters of hosts. Originally developed by Google and donated to the Cloud Native
Computing Foundation (CNCF), it offers a solution for container orchestration, increasing the scalability,
availability, and efficiency of applications.
Introduction to Kubernetes
Kubernetes architecture**
Openshift Container Platform
OpenShift is a family of containerization software products developed by Red Hat. Its flagship product is
the OpenShift Container Platform—an on-premises platform as a service built around Docker containers,
which are orchestrated and managed by Kubernetes on a foundation of Red Hat Enterprise Linux.
OpenShift provides developers with an integrated development environment (IDE) for building and
deploying Docker-formatted containers, and then managing them with the open source Kubernetes
container orchestration platform. This platform has been built with enterprise requirements like security in
mind, offering automated updates and scaling.
Rancher
The Rancher community helps make Kubernetes accessible to everyone. Their contributions focus on
simplifying the complexities of Kubernetes, making it easier for more organizations to adopt and benefit
from this technology.
TensorFlow is an open source software library for high performance numerical computation. Its flexible
architecture allows easy deployment of computation across a variety of platforms (CPUs, GPUs, TPUs),
and from desktops to clusters of servers to mobile and edge devices.
Originally developed by researchers and engineers from the Google Brain team, TensorFlow has become
popular among developers for machine learning and artificial intelligence projects. It has a broad
ecosystem of tools, libraries, and community resources that lets researchers push new experiments in
ML/AI and developers build and deploy ML/AI-powered applications.
PyTorch
PyTorch is an open source machine learning library developed by Facebook’s AI Research lab (FAIR). It
has gained popularity in the research community for its flexibility, ease of use, and dynamic computational
graph. PyTorch’s design is intuitive, particularly for those familiar with Python programming, making it a
preferred choice for both academic research and development in industrial applications.
One of the key strengths of PyTorch is its user-friendly interface. It offers a straightforward platform for
building deep learning models, allowing researchers and developers to focus more on the design aspect of
their models rather than the technical intricacies of the library. Its dynamic computational graph, where the
graph is built at runtime, offers more flexibility and ease for making changes and debugging. This feature
is particularly useful for complex models and novel architectures in research.
Keras is a high-level neural networks API, written in Python and capable of running on top of TensorFlow,
CNTK, or Theano. It was developed with a focus on enabling fast experimentation.
Keras is designed for human beings emphasizing usability and developer experience. Its API is intuitive
and easy to experiment with, making it approachable for newcomers while also robust enough for expert
practitioners. It provides clear and actionable feedback for user errors and has extensive documentation and
developer guides.
OpenCV
The library has more than 2,500 optimized algorithms, which includes a comprehensive set of both classic
and state-of-the-art computer vision and machine learning algorithms. These algorithms can be used to
detect and recognize faces, identify objects, classify human actions in videos, track camera movements,
track moving objects, and extract 3D models of objects.
GPTScript
GPTScript is a scripting language designed to enable users to automate tasks and interact with a Large
Language Model (LLM) using OpenAI. It combines the simplicity of natural language with the power of
traditional scripting tools like Bash and Python, as well as the capability to make HTTP service calls. This
unique blend allows users to execute a wide range of tasks, from mundane automation to complex database
operations, using intuitive language commands.
The language’s primary strength lies in its natural language-based syntax, which significantly reduces the
learning curve for new users. This accessibility makes GPTScript an ideal tool for a variety of applications,
including retrieval-augmented generation (RAG), task automation, and the creation of agents and
assistants. Additionally, it can be used for data analysis and the integration of vision, image, and audio
processing tasks into scripts. Another notable feature is its ability to manage memory and construct
chatbots.
HuggingFace Transformers
HuggingFace Transformers is a library that provides thousands of pre-trained models to perform tasks on
texts such as classification, information extraction, question answering, summarization, translation, and
text generation in over 100 languages. Its aim is to make state-of-the-art NLP easier to use for everyone.
Transformers provide a straightforward way to use models like BERT, GPT-2, T5, and others.
HuggingFace has become a go-to library for anyone working with textual data, thanks to its simplicity,
efficiency, and broad community support. Whether you’re a researcher, developer, or a company, it offers
cutting-edge capabilities with just a few lines of code.
Fast.ai
Official project page: https://www.fast.ai/
Repository: https://github.com/fastai/fastai
GitHub stars: 25.5K
Fast.ai is a deep learning library that provides high-level components aimed to enable fast experimentation
with deep neural networks. It sits on top of PyTorch and is designed to make deep learning more accessible
and easier to implement for practitioners.
Fastai’s library is structured around best practices with a focus on educational purposes, making it not just
a tool for production but also a learning platform for those new to deep learning. It automates many of the
tedious aspects of training deep learning models, making the process more intuitive and allowing
developers and researchers to move faster through their projects.
Redis, which stands for Remote Dictionary Server, is an open source, in-memory data structure store. It is
used as a database, cache, and message broker. One of the reasons for Redis’s popularity is its versatility. It
supports a wide range of data structures, including strings, hashes, lists, and sets.
Redis’s in-memory nature allows it to deliver ultra-fast read and write operations. This makes it an
appropriate choice for high-performance applications. With numerous contributors from around the world,
the Redis community is continually improving and innovating.
PostgreSQL
PostgreSQL is an open source relational database system. It is known for its robustness, scalability, and full
compliance with SQL standards. PostgreSQL supports a broad array of data types, including complex ones
like arrays and hstore (for storing key-value pairs).
One of the key features of PostgreSQL is its extensibility. You can define your own data types, operators,
and functions. This allows you to tailor the database system to your specific needs. And like Redis,
PostgreSQL has a strong community backing it, ensuring its continuous development and improvement.
MongoDB
MongoDB is an open source NoSQL database that provides high performance, high availability, and easy
scalability. It works on the concept of collections and documents, which makes it flexible and adaptable to
real-world data.
MongoDB offers the ability to handle large amounts of data with diverse structures. It’s suitable for dealing
with big data applications and real-time analytics. MongoDB’s horizontal scaling capability ensures that as
your data grows, your database can grow with it.
MariaDB
MariaDB is an open source relational database management system that is a fork of MySQL. It was created
by the original developers of MySQL after concerns over its acquisition by Oracle Corporation. MariaDB
is designed to maintain high compatibility with MySQL, ensuring a drop-in replacement capability with
library binary parity and exact matching with MySQL APIs and commands.
MariaDB has a few features that set it apart from MySQL. It includes a variety of storage engines,
including high-performance ones like Aria, ColumnStore, and Spider. It also offers advanced replication
for scaling.
Cassandra
Apache Cassandra is an open source distributed database system designed for managing large amounts of
data across many commodity servers, providing high availability with no single point of failure. Cassandra
offers robust support for clusters spanning multiple datacenters, with asynchronous masterless replication
allowing low latency operations for all clients.
Cassandra’s strengths revolve around its ability to handle large amounts of data, its resilient nature, and its
exceptional performance. Because it’s designed for distributed environments, it’s highly scalable and can
seamlessly grow with your data.
Related product offering: Configu: NetApp Instaclustr Data Platform | Open-Source Data Infrastructure
Platform
Apache Hadoop is an open-source framework designed for distributed storage and processing of large data
sets across clusters of computers using simple programming models. It is particularly known for its ability
to handle petabytes and exabytes of data. Hadoop breaks down big data into smaller pieces to be processed
in parallel, significantly speeding up data processing.
A key feature of Hadoop is its Hadoop Distributed File System (HDFS), which allows for high throughput
access to application data and is suitable for applications with large data sets. The framework also includes
Hadoop YARN, a job scheduling and cluster resource management system, and Hadoop MapReduce, a
programming model for large-scale data processing.
Spark is known for its speed, ease of use, and general-purpose nature. It can run programs up to 100x faster
than Hadoop MapReduce in memory, or 10x faster on disk. Spark also supports a wide array of tasks,
including SQL queries, streaming data, machine learning, and graph processing.
Apache Spark
PySpark
Elasticsearch
Elasticsearch is a highly scalable open-source search engine and analytics engine. It allows you to store,
search, and analyze big volumes of data quickly and in near real time. Elasticsearch is generally used as the
underlying engine/technology that powers applications with complex search features and requirements.
Elasticsearch uses a document-oriented approach, storing data in JSON format. This makes it easy to store,
search, and manage complex data structures. It’s built on top of the Apache Lucene library, offering a full-
text search engine with a powerful combination of features for search queries.
Elasticsearch is part of the ELK Stack (Elasticsearch, Logstash, Kibana), which is widely used for log
analysis in IT environments. This stack provides a powerful platform for searching, analyzing, and
visualizing data in real-time.
Note: In 2021 Elasticsearch transitioned from an Apache 2.0 open source license to the SSPL 1.0 license,
which is more restrictive. The software is still free and source code is available, but there are some
limitations on how it can be deployed in production.
OpenSearch is an open source search and analytics suite derived from Elasticsearch and Kibana. It provides
a scalable solution for search, observability, and data analysis across various types of datasets. OpenSearch
offers powerful full-text search capabilities, real-time data analysis, and advanced visualization tools.
One of OpenSearch’s key strengths is its flexibility and extensibility, allowing users to tailor it to their
specific needs. It supports a wide range of plugins and integrations, making it suitable for diverse use cases
such as log analytics, security monitoring, and more. The project is actively maintained by a large
community, ensuring ongoing improvements and feature additions.
Kafka
Official project page: https://kafka.apache.org/
Repository: https://github.com/apache/kafka
GitHub stars: 26.3K
Apache Kafka is an open source streaming platform that provides real-time data feeds. It’s designed to
handle data streams from multiple sources and deliver them to multiple consumers. Kafka is known for its
high throughput, reliability, and replication capabilities, making it a popular choice for big data and real-
time analytics applications.
Kafka’s distributed architecture ensures that it can handle massive amounts of data. It also provides fault
tolerance, ensuring that data feeds remain up and running even in the event of a failure. Kafka’s
community is active and enthusiastic, continually contributing to its development and refinement.
Related product offering: Configu: NetApp Instaclustr Data Platform | Open-Source Data Infrastructure
Platform
Related technology update: [Blog] How to Get the Most out of Your Kafka Cluster
Splunk
Splunk is a software platform widely used for monitoring, searching, analyzing, and visualizing machine-
generated data in real time. It processes and indexes the data to make it searchable, and then enables users
to create graphs, reports, alerts, dashboards, and visualizations.
Although not entirely open source, Splunk offers a free version with limited functionality, popular in the
community for data analysis and operational intelligence. It’s particularly valued for its ability to make
sense of large amounts of unstructured data, providing insights that can drive operational performance and
business results.
Learn more in the detailed guide to Splunk Cloud
Slurm
Slurm (Simple Linux Utility for Resource Management) is an open source, fault-tolerant, and highly
scalable cluster management and job scheduling system for large and small Linux clusters. Slurm is used
by many of the world’s supercomputers and computer clusters to manage job scheduling.
Slurm prioritizes scalability and portability. It allocates exclusive and/or non-exclusive access to resources
(compute nodes) to users for some duration of time. It also provides a framework for starting, executing,
and monitoring work on the set of allocated nodes. Finally, it arbitrates contention for resources by
managing a queue of pending work.
OpenTelemetry provides a unified standard for telemetry data collection across various programming
languages and platforms. This makes it easier for developers to implement observability and maintain
consistency in their monitoring practices.
The project supports multiple data sources and export formats, making it adaptable to different monitoring
backends such as Prometheus, Jaeger, and Zipkin. It is backed by a large community and achieved
Graduated status in the Cloud Native Computing Foundation (CNCF).
Zipkin
Zipkin is an open source distributed tracing system that helps developers monitor and troubleshoot latency
issues in microservice architectures. It collects and visualizes trace data, allowing teams to understand the
flow of requests through their systems and identify performance bottlenecks.
Zipkin supports various data collection methods and integrates with numerous platforms, making it a
versatile tool for observing complex, distributed systems. It provides an intuitive UI for viewing traces and
includes powerful features like trace sampling and customizable data storage options. Zipkin is widely used
in the industry and is backed by a robust community of contributors.
Vault HashiCorp
Vault by HashiCorp is an open source tool designed to securely manage secrets and protect sensitive data.
It allows organizations to control access to passwords, API keys, certificates, and other secrets in a
centralized and auditable manner. Vault offers robust features like dynamic secrets, encryption as a service,
and secret leasing and revocation, making it an essential tool for managing secrets in modern infrastructure.
Vault supports a wide range of authentication methods and provides fine-grained access control through
policies. It also offers high availability, scaling to meet the needs of large organizations, and is backed by
an active community of contributors and users.
Related technology update: [Webinar] Secure CD at scale with Octopus Deploy and HashiCorp Vault
Open source software (OSS) refers to software whose source code is publicly available and can be freely
used, modified, and shared by anyone. The source code is the human-readable instructions and statements
that make up a software program. By making the source code openly accessible, open source software
allows for transparency, collaboration, and continuous improvement by a global community of developers.
1. Open Collaboration: Anyone can contribute to the development, modification, and improvement of the
software by submitting code changes, bug fixes, or new features.
2. Free Distribution: Open source software can be freely distributed, copied, and used without paying
licensing fees or royalties.
3. Access to Source Code: The source code is openly available, allowing users to inspect, modify, and learn
from it.
4. Community-Driven Development: Open source projects often have a vibrant community of developers,
users, and enthusiasts who collaborate, provide feedback, and drive the software's evolution.
Open source software has gained widespread adoption across various domains, including operating systems
(e.g., Linux), web browsers (e.g., Mozilla Firefox), office suites (e.g., LibreOffice), source code managers,
(e.g., Gitness, GitHub) and programming languages (e.g., Python, Ruby). Many companies, organizations,
and individuals contribute to and benefit from open source projects, as it promotes innovation, cost-
effectiveness, and the sharing of knowledge.
The open source movement has had a significant impact on the software industry, fostering transparency,
collaboration, and rapid innovation. It empowers users to have control over the software they use and
encourages the development of robust, secure, and adaptable solutions.
The development process of open source software is a collaborative endeavor that harnesses the collective
efforts of a global community. Unlike proprietary software, where the source code is closely guarded, open
source projects embrace transparency by making their code openly available to anyone interested. This
accessibility allows developers from around the world to contribute their skills, expertise, and ideas
towards improving and enhancing the software.
At the core of open source software lies a vibrant ecosystem of passionate individuals who voluntarily
dedicate their time and effort to the project. These contributors can range from seasoned professionals to
hobbyists and students, each bringing their unique perspectives and experiences to the table. They work
together, reviewing each other's code, identifying and fixing bugs, proposing new features, and refining
existing functionalities.
Many open source projects have established robust governance models and guidelines to manage and
coordinate the development efforts effectively. These models often involve a core team of maintainers who
oversee the project's direction, review code contributions, and ensure adherence to coding standards and
best practices. Additionally, open source communities foster open discussions, allowing users and
developers to provide feedback, report issues, and share ideas for future development.
The power of open source software lies in its ability to leverage the collective intelligence and diverse
perspectives of its contributors. By embracing transparency, collaboration, and continuous improvement,
open source projects can rapidly evolve, adapt to changing needs, and produce high-quality software that
benefits users worldwide.
Open source software offers a multitude of advantages that have contributed to its widespread adoption and
success. At its core, the open and collaborative nature of open source development fosters innovation,
flexibility, and cost-effectiveness. By making the source code freely available, open source software
empowers users and developers alike, unlocking a world of possibilities.
One of the most significant benefits of open source software is the ability to scrutinize and modify the code
to suit specific needs. This level of transparency and customizability ensures that the software can be
tailored to meet unique requirements, whether it's adding new features, optimizing performance, or
enhancing security. The freedom to adapt the software eliminates the constraints often associated with
proprietary systems, allowing for greater flexibility and control.
From a financial perspective, open source software offers significant cost savings compared to proprietary
alternatives. With no licensing fees or expensive maintenance contracts, open source solutions provide a
cost-effective option, particularly for organizations with limited budgets. This accessibility democratizes
software development and empowers individuals and businesses to leverage powerful tools without
substantial financial barriers.
Open source software often exhibits increased interoperability and compatibility with other systems and
platforms. This interoperability is facilitated by the use of open standards and protocols, allowing for
seamless integration and data exchange across different environments. Consequently, organizations can
avoid vendor lock-in and enjoy greater flexibility in their technology stack.
The benefits of open source software extend beyond the technical realm. The open source movement has
fostered a vibrant community of individuals who collaborate, share knowledge, and contribute to the
collective advancement of software development. This community-driven approach promotes transparency,
fosters a culture of learning, and encourages the free exchange of ideas, ultimately driving innovation and
progress.
While open source software offers numerous benefits, it also presents a set of challenges that must be
carefully navigated. One of the primary concerns revolves around the potential lack of centralized support
and maintenance. Unlike proprietary software, where a dedicated team is responsible for providing
technical assistance and regular updates, open source projects often rely on the voluntary efforts of a
dispersed community. This decentralized approach can lead to inconsistent or delayed support, particularly
for niche projects with smaller communities.
Another challenge lies in the complexities associated with managing and coordinating the contributions
from a diverse global community. Open source projects can receive code submissions from developers
with varying skill levels and coding styles, which can make it difficult to maintain a cohesive codebase and
ensure adherence to established standards and best practices. Effective governance and quality control
mechanisms become crucial to mitigate these challenges and ensure the overall integrity of the software.
Security is another area of concern that requires vigilance in the open source ecosystem. While the open
nature of the code allows for transparency and peer review, it also exposes the software to potential
vulnerabilities if not properly maintained and monitored. Malicious actors may attempt to introduce
security flaws or backdoors, compromising the integrity of the software. Consequently, open source
projects must prioritize security audits, prompt patching, and proactive measures to safeguard against
potential threats.
Furthermore, open source software can sometimes lack comprehensive documentation and user support
resources. As contributions come from various individuals, documentation may be incomplete or
inconsistent, making it challenging for users, particularly those with limited technical expertise, to
effectively utilize and troubleshoot the software. Clear and well-maintained documentation is essential for
widespread adoption and user satisfaction.
Lastly, the open source model can present challenges related to intellectual property rights and licensing
compliance. With multiple contributors and different licenses involved, it becomes crucial to ensure that all
code contributions adhere to the project's licensing terms and do not infringe on any third-party intellectual
property rights. Failure to properly manage licensing and legal aspects can lead to potential legal disputes
and hinder the project's progress.
While open source software offers significant advantages, addressing these challenges requires a proactive
approach, effective governance, and a strong commitment from the community to ensure the long-term
success and sustainability of open source projects.
Open source and closed source software represent two contrasting approaches to software development and
distribution. The fundamental distinction lies in the accessibility and transparency of the source code,
which shapes the development process, licensing models, and overall philosophy behind each approach.
Open source software embraces a collaborative and transparent model, where the source code is freely
available for anyone to inspect, modify, and distribute. This openness fosters a vibrant community of
developers who can contribute to the software's improvement, identify and fix bugs, and propose new
features. The open nature of the code allows for peer review, promoting quality assurance and security
through collective scrutiny.
In contrast, closed source software, also known as proprietary software, keeps the source code tightly
guarded and inaccessible to the general public. The code is considered a trade secret, owned and controlled
by the software company or individual developer. Users of closed source software are typically granted a
license to use the software under specific terms and conditions, but they do not have the ability to modify
or redistribute the code.
The development process for open source software is decentralized and driven by a global community of
contributors, while closed source software is typically developed by a dedicated team within a company or
organization. This centralized approach in closed source development allows for tighter control over the
software's direction and roadmap but may limit the diversity of ideas and contributions.
Licensing models also differ significantly between open source and closed source software. Open source
projects often utilize licenses that encourage free distribution and modification, such as the GNU General
Public License (GPL) or the MIT License. These licenses ensure that the software remains free and open
for future generations. In contrast, closed source software is typically distributed under proprietary licenses
that restrict modification, redistribution, and reverse engineering.
Open source software tends to prioritize interoperability and adherence to open standards, promoting
compatibility across different platforms and systems. Closed source software, on the other hand, may
prioritize vendor lock-in strategies, making it more challenging to integrate with other systems or migrate
data to alternative solutions.
The philosophical underpinnings of open source and closed source software also diverge. Open source
advocates promote the free sharing of knowledge, collaboration, and community-driven development,
aligning with values of transparency and freedom. Closed source models often prioritize commercial
interests, intellectual property protection, and maintaining competitive advantages through proprietary
technologies.
While both approaches have their merits and suit different needs, the choice between open source and
closed source software ultimately depends on an organization's or individual's requirements, priorities, and
alignment with the respective philosophies and development models.
The Open Source Initiative (OSI) is a globally recognized non-profit organization dedicated to promoting
and advocating for open source software. Founded in 1998, the OSI plays a pivotal role in shaping the open
source movement and setting standards for what qualifies as truly open source.
At the core of the OSI's mission lies the Open Source Definition, a set of criteria that establishes the
fundamental principles and requirements for software to be considered open source. This definition serves
as a guideline for developers, ensuring that open source projects adhere to principles of free redistribution,
access to source code, derived works, non-discrimination, and the absence of restrictive license terms.
The OSI's efforts extend beyond defining open source software; it also serves as a steward of the open
source community. The organization maintains a widely recognized list of open source licenses that
comply with the Open Source Definition, providing a reference for both developers and users to ensure
compliance and promote legal certainty.
The OSI actively campaigns for the adoption and use of open source software across various sectors,
including government, education, and industry. It works to raise awareness about the benefits and
advantages of open source solutions, fostering a culture of collaboration, transparency, and innovation.
Through educational initiatives, events, and advocacy efforts, the OSI aims to empower individuals,
organizations, and communities to embrace open source practices. It provides resources, guidance, and
support to help navigate the complexities of open source licensing, governance, and best practices.
The OSI's role as a neutral and independent authority in the open source ecosystem is crucial. It serves as a
trusted voice, promoting the principles of open source and facilitating collaboration among stakeholders,
including developers, businesses, and policymakers.
By upholding the ideals of open source software and fostering a vibrant community, the Open Source
Initiative plays a vital role in shaping the future of software development and driving innovation through
open and collaborative practices.
Open source software are programs whose source code is freely available for anyone to view, modify, and
distribute. Read on to learn more about open source software, how it works, and a few examples of such
software.
Open source software refers to computer programs whose source code is freely available on repositories
like GitHub for anyone to view, use, modify, and distribute.
From powering smartphones to running critical infrastructure systems worldwide, open source plays an
indispensable role in our digital lives.
This guide explains the meaning of open source software, how it works, its applications, and its key
differences from closed source software. It also discusses the importance of open source and its pros and
cons.
Open source software are computer programs whose source code is available for anyone to view, modify,
and distribute. Unlike closed source software, which keeps its code hidden from users, open source
software encourages collaboration and transparency among developers.
This means individuals or organizations can freely access the underlying code of an open source program,
making it customizable and adaptable to meet specific needs.
The concept of open source software traces back to the late 20th century when Richard Stallman launched
the Free Software Movement in 1983. It aimed to promote user freedom by providing access to the
software’s source code. The movement gained momentum with Linus Torvalds’ development of Linux in
1991, marking a significant milestone in open source history.
Since then, numerous projects have emerged under various open source licenses, such as GNU General
Public License (GPL) and Apache License. Today, open source software is crucial in powering many
technological advancements across industries worldwide.
How Does Open Source Software Work?
Open source software operates on a collaborative model, where the source code is freely available to
users. This accessibility allows developers to study how the software functions, identify and fix bugs, and
add new features. Communities of developers, enthusiasts, and users form around projects, working
together to improve the software.
Open source collaboration takes various forms, such as contributing code changes, suggesting
improvements, creating documentation, or providing support to others. By pooling diverse skills and
perspectives, these communities can evolve software more effectively.
To facilitate collaboration, open source projects utilize version control systems like Git. Version control
systems allow multiple developers to work on the same codebase simultaneously, track changes, and
manage contributions. Public code repositories, such as GitHub or GitLab, serve as platforms for hosting
open source projects.
These repositories provide a central location where developers can share their code, manage issues, and
collaborate. Users can report bugs, suggest new features, or engage in discussions, fostering a sense of
community participation and feedback. Further, through forking and branching, contributors can create
independent copies of the software, enabling experimentation and the development of alternative
versions.
Open source projects typically have structured release management processes. This involves testing,
documentation updates, and ensuring compatibility across various platforms. Regular releases ensure that
users have access to stable and reliable software while allowing contributors to showcase their
contributions.
The open source philosophy embraces continuous improvement. Feedback drives the evolution of open
source software, motivating developers to refine existing features, fix bugs, and enhance cyber security.
See More: What Is Network Software? Definition, Types, Components, and Best Practices
Open Source vs. Closed Source Software — 6 Key Differences
Open source and closed source software are two distinct approaches to developing and distributing
computer programs. Here are six key differences between the two:
1. Access to source code
In open source software, the source code is freely available for users to view, modify, and distribute. This
allows developers to customize the software according to their needs and contribute improvements back
to the community.
On the other hand, closed source or proprietary software keeps its source code hidden from users. Only the
compiled version of the software is provided, making it difficult for users to understand how it works or
make any modifications. This lack of transparency can be limiting for developers who want more control
over their software.
2. Availability of features
Closed source software often has more features than open source software because the companies or
teams behind closed source software have dedicated resources and expertise. They can invest a lot of time,
money, and specialized knowledge into developing and improving their software, allowing them to
continuously add new features and enhance user experience.
Additionally, closed source software companies sometimes integrate their products with other proprietary
solutions or services, providing additional functionalities that make the software seem more feature-rich.
On the other hand, open source software relies on community collaboration. While open source software
may not have as many built-in features as closed source software, it offers other advantages such as
reliability, stability, security, and the flexibility for users to modify and customize it to suit their needs
3. Transparency of functionality
By providing transparency in functionality, open source software promotes trust and allows users to
understand how their applications work on a deeper level. They can examine every line of code and ensure
no malicious activities occur within the software.
Openness fosters collaboration among developers as they can review each other’s work and identify bugs
or issues more effectively.
Also, closed source software keeps its source code hidden from users. This lack of transparency raises
questions about what exactly is happening behind the scenes. Users cannot inspect the inner workings of
closed source software and may have concerns regarding potential security vulnerabilities or hidden
functionalities.
In contrast, with closed source software, users must rely solely on the developer’s claims about its
functionality without being able to verify them independently.
4. Availability of customer support
In closed source software, companies typically offer dedicated customer support services to assist users
with any issues they may encounter. This includes access to a helpdesk or hotline where users can reach
out for assistance.
Moreover, open source software often relies on community-based support. Users can seek help from
online forums, discussion boards, or mailing lists where fellow developers and enthusiasts provide guidance
and solutions.
While this approach may lack the immediate response time offered by dedicated customer support teams,
it benefits from a wider pool of expertise as multiple individuals contribute their knowledge and
experiences to troubleshoot problems.
5. Licensing systems
Open source software is typically released under licenses that allow users to view, modify, and distribute
the source code freely. The most common open source license is the GNU General Public License (GPL),
which ensures that any modifications or derivative works must also be made available as open source.
Further, closed source software is protected by proprietary licenses that restrict access to the underlying
code. These licenses often require users to pay for a license and prohibit them from modifying or
redistributing the software without explicit permission from the copyright holder.
Examples of popular closed source licenses include Microsoft’s End-User License Agreement (EULA), which
governs Azure and all other MS products, and Apple’s Software License Agreement.
6. Commercial usage and reusability restrictions
Open source software allows for unrestricted commercial use, meaning individuals or businesses can use
the software for profit without any limitations. Additionally, open source software typically has no
restrictions on code reuse, allowing developers to modify and distribute the code as they see fit.
Closed source software often comes with strict licensing agreements that limit its commercial usage. These
licenses may require users to purchase a specific license or pay royalties to use the software commercially.
Similarly, closed source software may have stringent restrictions on code reuse, prohibiting developers
from modifying or distributing the code without explicit permission from the owner.
Open source software (OSS) has gained significant importance in today’s digital landscape, providing
numerous benefits to developers, businesses, and the broader technology community. Here are the top
reasons why open source—the software, the communities, and the philosophy—is so crucial:
Importance of Open Source Software
Providing access to the underlying source code allows developers to modify, customize, and build upon
existing software solutions. This level of flexibility enables researchers and innovators to experiment with
new ideas, add features, or solve specific problems without starting from scratch.
Moreover, the collaborative environment promotes knowledge sharing and collective problem-solving.
Researchers can leverage this vast pool of expertise to enhance their projects or find innovative solutions
for complex challenges.
2. Supports startups and solopreneurs
Open source software has been a game-changer for startups and solopreneurs, providing them with the
tools they need to thrive in an increasingly competitive market. The accessibility and affordability of open
source solutions make it easier for these small businesses to get off the ground without breaking the bank.
Startups can leverage open source software for a wide range of needs, from building their website or
mobile app to managing customer relationships and streamlining internal operations. With access to high-
quality, free, or low-cost software, startups can allocate their limited resources toward other crucial
aspects of their business, like marketing or product development.
Additionally, open source communities often provide support and forums where entrepreneurs can
connect with like-minded individuals facing similar challenges along their journey.
3. Assists in training software developers
Open source software plays a crucial role in training software developers and fostering their skills. With
open source projects, aspiring programmers can gain hands-on experience by contributing to real-world
applications. This practical approach allows them to understand the intricacies of coding, collaborate with
experienced developers, and improve their problem-solving abilities.
By participating in communities, software developers can learn from experts and receive valuable feedback
on their work. They can study well-established codebases, explore different programming languages like
Python, C, or C++, and understand best practices followed by industry professionals.
Whether they are working on bug fixes or adding new features to existing projects, these opportunities
enable them to gain confidence in their abilities while making tangible contributions to the world of
technology.
4. Improves software quality as a result of public scrutiny
One major advantage of open source software is that it undergoes constant review and feedback from a
large community of developers. With the source code available to anyone, bugs and vulnerabilities can be
identified and fixed faster than in closed source software. This collaborative effort ensures that the
software is thoroughly tested by experts, leading to higher-quality products.
The transparency provided by open source development allows for continuous improvement through
public scrutiny.
When developers encounter a bug or an issue in closed source software, they have limited options for
finding a solution. They often rely on customer support from the vendor, which can be time-consuming and
may not always provide satisfactory results.
In contrast, open source software allows developers to access the source code and collaborate with other
community members facing similar challenges.
This enables a faster resolution of problems as multiple minds work together to identify and fix issues
swiftly. The collective knowledge and diverse perspectives within the community contribute to an
accelerated problem-solving process, benefiting both individual developers and the wider software
development ecosystem. This is also a great way to find solutions to new and unprecedented problems.
7. Supports cybersecurity testing
In the world of software development, cybersecurity is a critical concern. Open source software plays a
crucial role in supporting application security testing. With open source software, developers can access
the underlying code and actively identify and fix security vulnerabilities. This allows for thoroughly
examining the software’s security measures, making detecting any potential weaknesses or flaws easier.
Moreover, open source projects often benefit from a large community of users and developers who
collaborate on identifying and resolving security issues efficiently. This collective effort helps ensure that
any vulnerabilities are quickly addressed through regular updates and patches. The availability of public
repositories enables continuous monitoring by security experts worldwide.
Pros and Cons of Open Source Software
Despite the importance of open source, OSS is not without its share of challenges. Let us take an objective
look at the pros and cons of open source software to decide if it is right for you:
Pros of open source software
Open source projects are typically developed and maintained by a community of volunteers who share
their skills and knowledge for free. This means anyone can access and use the software without paying
hefty licensing fees or upfront costs.
Moreover, since the source code is available to everyone, businesses can modify and customize the
software according to their specific needs, eliminating the need for expensive proprietary software as a
service (SaaS) solutions.
2. Ease of integration
Unlike closed source software, which often requires proprietary application programming interfaces (APIs)
and protocols, open source software promotes interoperability and compatibility with other systems. This
means developers can seamlessly integrate different components or modules from various sources without
facing major hurdles.
3. No vendor lock-in
With OSS, there is no vendor lock-in. This means users are not tied to a single provider or limited by
proprietary formats or protocols. Developers are free to modify and customize the software according to
their specific needs without any restrictions imposed by a single vendor.
Additionally, it ensures that users can easily switch between different vendors if they find one better suited
to their requirements or if they encounter any issues with the current provider. This flexibility ultimately
leads to cost savings and improved efficiency in the long run.
4. Greater flexibility
Developers have access to the underlying source code, giving them the freedom to make any necessary
changes or enhancements. This flexibility empowers organizations and individuals to tailor the software to
meet their unique requirements, whether adding new features or integrating with other systems. The
ability to customize open source software makes it a versatile solution for various applications across
various industries.
This inherent flexibility in open source software ensures that businesses can build agile IT infrastructures
that evolve alongside their changing needs—a valuable advantage that closed source alternatives may not
offer.
5. The power of community
Unlike closed source software, where development and improvement are limited to a specific team or
company, open source projects benefit from a global community of developers, users, and enthusiasts. The
power of community lies in its ability to harness the collective intelligence of passionate individuals who
share a common goal: creating high-quality software accessible to all.
See More: What Is Agile Software Development? Life Cycle, Methodology, and Examples
Cons of open source software
Open source software, despite its many advantages, also has some drawbacks that users should be aware
of:
1. No prompt support
Open source software often relies on community forums and user-driven discussions for troubleshooting
and problem-solving. While these forums can be helpful in many cases, they may not always provide
immediate or timely responses to users’ queries or issues. This can be frustrating for individuals or
businesses requiring urgent technical assistance.
It’s important to consider this aspect before choosing open source software, especially if your organization
requires a high level of technical support and quick resolution for any potential problems that may arise
during usage.
2. Complexity and difficult learning curve
Unlike closed source software, which often comes with user-friendly interfaces and comprehensive
documentation, open source software might require some technical expertise to navigate effectively.
The sheer flexibility and customization options offered by open source software can lead to higher
complexity. Users may need to invest time in understanding the intricacies of the software’s framework
and architecture, configuration settings, and dependencies. This can be daunting for those unfamiliar with
coding or with limited technical knowledge.
3. Limited features
Open source software (OSS) offers numerous benefits, but it’s important to acknowledge that there can
also be limitations. One of these is the potential for limited features in certain OSS projects. This is because
OSS development relies on community contributions and volunteer efforts, which may result in prioritizing
core functionalities over additional features.
4. High maintenance needs
Since these projects rely on community contributions and updates, they require regular upkeep to ensure
compatibility and security. This includes regularly monitoring for updates, bug fixes, and security patches.
Open source applications can become vulnerable to security breaches or compatibility issues with other
software components without proper maintenance.
Additionally, because multiple contributors are involved in the development of open source projects,
coordinating updates can be more complex compared to closed source alternatives, where a single vendor
oversees all aspects of maintenance.
5. Less stable builds
The open nature of OSS means that anyone can modify and update the code, which can lead to unintended
consequences. As a result, users may experience occasional crashes or glitches while using open source
applications. However, passionate developers within the community work diligently to address these issues
and release patches and updates to improve stability over time.
See More: What Is Integrated Development Environment (IDE)? Meaning, Software, Types, and Importance
Open Source Software Examples
There are numerous examples of open source software that have made a significant impact in the tech
industry. These include:
VLC Media Player: VLC Media Player, developed by VideoLAN, is a highly popular open source media
player that supports a wide range of audio and video formats. With its humble beginnings in 1996 as an
academic project at École Centrale Paris, VLC has become one of today’s most widely used media players.
GIMP: GIMP, short for GNU Image Manipulation Program, is a powerful open source image editing
software. The history of GIMP dates back to 1995 when Spencer Kimball and Peter Mattis developed the
software as a class project at the University of California, Berkeley. Over the years, it has evolved into a
highly versatile tool used by graphic designers, photographers, and artists worldwide.
Mozilla Firefox: Mozilla Firefox is a popular web browser that has gained immense popularity among
users worldwide. It was created by the Mozilla Foundation and was initially released in 2004 as an open
source project.
Linux: Linux is a powerful open source operating system that has revolutionized the computing world.
Developed by Linus Torvalds in 1991, Linux was initially created as an alternative to proprietary operating
systems like Windows and macOS. It is based on the Unix operating system and is designed to run on a
wide range of devices, from servers to Raspberry Pi.
Apache HTTP Server: The Apache HTTP Server, commonly known as Apache, was created by a group of
developers in 1995 to provide a robust and reliable platform for hosting websites. Since its inception, it has become
one of the most popular web servers globally, commanding over half of the market share.
What Is Linux?
Linux is a free, open source operating system that is not only used on millions of desktops across the globe
but also serves as the backbone for the internet, the cloud, servers, supercomputers, smartphones, smart
devices and so much more.
At its foundation, the Linux operating system revolves around the Linux kernel initially created by Linus
Torvalds back in 1991. In contrast to closed systems such as Microsoft’s Windows, Linux allows anyone to
download and view the code of everything from the kernel all the way to user applications. This model has
helped to foster a thriving development community around access but also makes it possible for enterprise
businesses to use the code and contribute back to the efforts of open source teams everywhere.
The Evolution of Linux Operating System
Linux has transformed over time from a hobby project started by a Finnish student to a powerful system
that drives much of the internet’s backbone. Both individuals and companies such as the Free Software
Foundation, Red Hat, and Canonical have played roles in its development. This growth has resulted in the
emergence of distributions. Versions of the operating system customized for specific purposes, as well as
more general-purpose desktop distributions such as Ubuntu, Fedora and Debian.
Linux holds a role in the world of computing that cannot be emphasized enough. Its reputation for stability,
security and versatility has made it a top choice for systems and development environments. With support
for a wide range of hardware and network setups, Linux proves adaptable for a range of applications,
whether it’s running web servers, managing cloud environments, development or handling intricate
multicluster deployments.
The open source nature of Linux creates an environment for innovation. Developers have unrestricted
access to the kernel source code, enabling them to tune, optimize and fortify systems based on their
operational requirements and preferences. This level of accessibility has also cultivated an assortment of
software and tools that contribute to Linux’s standing as a cornerstone of computing.
The Linux kernel serves as part of the Linux operating system, overseeing resource management and
hardware operations such as memory allocation and device management. It acts as a connection point
between applications and the underlying hardware processes. In contrast to Windows NTs hybrid kernel
approach, Linux uses a kernel that consolidates all functions into a unified entity. This architecture
enhances performance. It provides access to hardware essential for efficient computing activities. With the
Linux kernel’s modular approach, users and administrators can add or remove modules as needed, to
expand capabilities and even strengthen security.
The Linux kernel and distributions are distributed via open source licenses, which means the source code is
available for developers to review, tweak and share. This openness encourages enhancements and
teamwork with contributions coming in from developers globally. The transparency of open source
principles boosts system security by inviting scrutiny and drives innovation by tapping into shared
knowledge and skills.
TRENDING STORIES
Linux is often compared to operating systems, such as UNIX and Windows. While Linux has roots in
UNIX, it stands out as a free option inspired by the GNU project led by Richard Stallman. Unlike
proprietary systems like Windows, Linux empowers developers and users with extensive customization
options and control essential for optimizing systems based on unique performance or security requirements.
Moreover, Linux’s efficient command line interface (CLI) serves as a resource for managing systems,
creating flexible scripts and granting users direct access to advanced functionalities.
Linux distributions, often simply called “distros,” are customized versions of the Linux operating system
tailored to meet diverse user requirements. From desktops to servers, each distro offers a unique
combination of the core Linux kernel with a set of applications, a package management system, and a user
interface. Some of the most widely used distributions include:
Ubuntu: Ubuntu is widely recognized for its ease of use and robust backing, from the community making it
a top choice among both novices and seasoned users in the realm of Linux distributions. Built upon Debian,
Ubuntu is versatile and ideal for settings such as desktops, servers and cloud platforms.
Debian: Debian is highly regarded for its stability and security, making it a popular choice as the base for
many distributions, like Ubuntu and Linux Mint. Developers and system administrators favor Debian for its
strong foundation..
Fedora: Fedora, well known for its speed and bleeding-edge approach, is a project led by a community that
acts as a platform to test out technologies, many of which eventually find their place in Red Hat Enterprise
Linux.
Red Hat Enterprise Linux (RHEL): RHEL provides top-notch support, thorough documentation and a
reliable, safe setting for business applications as part of its distribution.
Linux Mint: Mint is a choice for many users switching from Windows because of its user-friendly design and
seamless compatibility with various hardware.
Zoom
Data from 58,600 participants in Stack Overflow’s 2024 Developer Survey shows how many developers
use various operating systems (including Linux distros like Ubuntu and Debian) in both their professional
and private lives.
Ubuntu is well known for its user installation process and wide range of software options available. It is an
option for software development, with recent updates that offer strong support for container and cloud
technologies.
Debian provides a wide range of packages in its main repository but can be expanded with other third-party
“repos.” Debian emphasizes the importance of free software and high stability, which makes it well-suited
for servers and embedded systems.
Fedora explores possibilities by incorporating cutting-edge open source technologies catering to developers
keen on leveraging the innovations within the Linux environment.
In settings where reliability and robust support are essential, RHEL offers extended support periods and a
stable platform.
Linux Mint is frequently praised for its multimedia capabilities and user-friendly nature, making it a solid
choice for computing tasks.
There are types of Linux distributions each tailored to meet the needs of users whether they are casual
desktop users, hardcore developers, or large enterprises. Selecting a distribution can have an influence on
system administration, security measures, compatibility with software, and the range of available
applications all of which can directly influence productivity levels and the overall user experience.
Linux is highly valued in the world of software development for its strength, clarity and adaptability.
Developers appreciate Linux for its command line interface, which provides control over development
tools and system resources. The open source nature of Linux allows developers to tailor their operating
environment to meet project requirements, whether it involves tweaking the kernel or customizing software
stacks.
In Linux, you can find a variety of programming environments, integrated development environments
(IDEs), and compilers that are widely accessible and frequently updated, which makes Linux a dynamic
platform for creating applications in languages like Python, Java, Go, Rust, C++, and many
others. Furthermore, Linux’s compatibility with an array of software and hardware makes it an excellent
choice for platform development.
In the world of DevOps, Linux truly shines with containers and orchestration tools like Docker and
Kubernetes. These technologies play a role in enabling continuous integration and continuous deployment
(CI/CD) practices and empowering developers and operations teams to efficiently create, deploy, and
manage applications across various environments.
Moreover, Linux offers an array of automation tools, such as Ansible, Puppet and Chef that aid in
simplifying configuration management and operational tasks. These tools contribute to streamlining the
DevOps workflow. With its reliability and scalability, Linux remains a player in driving DevOps
advancements by supporting agile development methodologies and swift deployment processes.
Linux holds a robust presence in the realm of cloud computing. It is the choice for public cloud providers
such as Amazon Web Services (AWS), Google Cloud Platform (GCP), and Microsoft Azure, all of which
offer services and virtual machines based on Linux. The popularity of Linux in this field can be attributed
to its cost-effectiveness, performance, security, flexibility and scalability.
By using Linux-based cloud solutions, businesses can take advantage of virtualization and containerization
technologies to optimize resource utilization and minimize expenses. The robust security features of the
operating system, along with its capability to manage workloads efficiently, make it a suitable option for
running data applications, web services and large-scale enterprise systems.
Linux is well known for its security measures, which are essential for handling multiple users and
safeguarding sensitive information. One standout feature is Security Enhanced Linux (SELinux), originally
created by the National Security Administration (NSA), which offers support for access-control security
policies, including Mandatory Access Controls (MAC) that surpass traditional Discretionary Access
Controls (DAC) systems.
Moreover, Linux’s permission and user-role system add a layer of security by enabling system
administrators to specify resource access permissions. These capabilities allow developers to customize
Linux for an operating environment capable of protecting applications from unauthorized entry and
potential vulnerabilities.
Customizing the Linux Kernel
Linux offers an advantage, with its kernel allowing developers to tailor it for better performance, improved
hardware compatibility or the addition of new features. This flexibility is crucial for crafting computing
setups or for embedded systems with minimal resources. Moreover, Linux’s open source design provides
documentation and community assistance, which enable modifications at the kernel level. These factors
position Linux as a choice for fostering innovation and exploration, in all types of computing scenarios.
Linux’s command line interface (CLI) is a tool that empowers users to handle software packages and
manage hardware resources effectively. Popular scripting languages such as Bash and Python are well
supported on Linux and make it possible to simplify automation and system management tasks. Developers
benefit from the CLI’s automation capabilities boosting efficiency and accuracy in software development
processes. Additionally, the CLI accommodates programming methods and procedures, serving as a tool
for contemporary DevOps practices.
In Linux, package managers play a role by automating tasks, like installing, updating and uninstalling
software. They manage dependencies to prevent conflicts during installation and keep track of all installed
software for system maintenance. Some popular package managers are:
APT (Advanced Package Tool): Used primarily by Debian and its derivatives like Ubuntu, APT simplifies the
process of managing software by automating the retrieval, configuration, dependency resolution, and
installation of software packages from repositories.
YUM (Yellowdog Updater, Modified) and DNF: These package managers are used by distributions such as
Fedora, AlmaLinux, and Red Hat Enterprise Linux (RHEL). One thing to keep in mind is that yum has been
deprecated in favor of dnf. Although yum is still usable, it is advised to make the switch to dnf.
Pacman: The package manager for Arch Linux, known for its speed and simplicity, which manages packages
more dynamically by tracking every installed file.
Linux package-management systems make it easy to install software directly from maintained repositories,
which contain thousands of pre-built packages. For example, using APT, a user can install software with
commands like sudo apt-get install [package-name], and similarly, with DNF, a user might use udo
dnf install [package-name].
These tools also allow for easy updates and maintenance. Regular commands such as sudo apt-get
update && sudo apt-get upgrade or sudo dnf update ensure that all software on the system is up to
date, reducing security risks and improving stability.
Differences in Package Management Across Distributions
The functions of package managers are similar in how they handle software management. However, they
differ notably in their command formats and the repositories they can access. For example, Debian-based
systems typically offer repositories with a selection of software options, whereas Arch Linux prioritizes
simplicity and user empowerment by providing cutting-edge software that requires more hands-on
involvement.
The selection of a package manager can impact aspects from system security with updates to accessing the
latest software functionalities. For developers, it is essential to grasp these distinctions to opt for a
distribution that aligns best with their development requirements, particularly when specific libraries or
software versions are necessary for their projects.
Linux provides a variety of graphical user interfaces that are particularly attractive, to users who are
familiar with visually driven operating systems such as Windows or macOS. Some of the more popular
desktop environments for Linux are:
GNOME: Known for its minimal approach, simplicity, and functionality, GNOME is the default desktop
environment for many major Linux distributions, such as Fedora and Ubuntu. It offers a streamlined and
modern user experience with a focus on usability and accessibility.
KDE Plasma: Celebrated for its configurability and robust feature set, KDE Plasma is for users who prefer a
highly customizable and beautiful desktop experience. It provides extensive visual customization options,
making it attractive for users who want to tailor every aspect of their interface.
XFCE and LXDE: These environments are favored for their light resource usage and high customization,
making them ideal for older hardware or systems where performance is a priority over visual flair.
Cinnamon: This desktop environment is ideal for anyone who prefers the traditional Windows-like desktop,
consisting of a desktop menu, panel, system tray and clickable desktop icons.
The command line interface is still a tool for Linux users, particularly developers and system
administrators. Unlike user interfaces (GUIs), the CLI allows access to system functions and scripts,
providing a more efficient method for executing intricate tasks.
Bash: The most common Linux shell, Bash is a powerful tool for managing files, running software, and
programming scripts. It is highly valued for its efficiency and the extensive control it offers over the system.
Zsh: Known for its improvements over Bash, such as better auto-completion and customizable
appearances, Zsh is favored by users who spend a lot of time in the terminal and seek an enhanced
interactive experience.
Customizing the User Environment
Linux is known for its customization options. Users have the flexibility to personalize their system
according to their preferences or productivity needs with tools/apps such as:
Conky: A tool that shows information on the screen, like CPU usage, disk status, email alerts and weather
updates.
Docky: A dock software that simplifies launching apps and handling Windows efficiently.
Compiz: A Windows manager that elevates the effects of the Linux interface, with animations and smooth
Windows transitions.
Linux’s versatility shines in networking, where it serves as a robust platform for configuring and managing
network operations. Linux systems can be tailored to function as network routers, firewalls, gateways and
servers. Key networking functions in Linux are managed through utilities like netstat, ss, ip, ufw and
firewall-cmd, which allow for detailed control over network interfaces, traffic routing, and firewall
settings.
Linux supports a wide range of networking protocols, from IP to SMB and NFS, making it capable of
interacting in diverse networked environments. Additionally, advanced tools like nftables provide newer,
more flexible ways to manage network packets and routes.
Linux comes equipped with powerful tools that help in monitoring and managing network traffic:
Wireshark: This GUI-based network protocol analyzer lets users capture and interactively browse the traffic
running on a computer network.
tcpdump: A command-line packet analyzer used for network diagnostics and troubleshooting.
iperf3: A tool for active measurements of the maximum achievable bandwidth on IP networks.
These tools enable developers and system administrators to diagnose network issues, monitor traffic flows
and ensure security protocols are being followed effectively.
Linux is highly effective, in settings where there is a need for multiple operating systems to work together.
Tools such as Samba make it easy for Linux servers to share files and printers with Windows computers
seamlessly. Similarly, NFS (Network File System) allows files to be shared among Linux machines,
promoting collaboration in environments with a mix of operating systems.
Moreover, Linux can seamlessly integrate into virtualized setups, often serving as a guest operating system
on hypervisors like VMware ESXi, Microsoft Hyper V and Oracle VirtualBox. This adaptability is key for
businesses that operate with a variety of operating systems and require a platform that can smoothly
interact with technologies.
The Linux community is vast and active, offering an array of support forums, discussion groups, and online
resources that foster collaboration and knowledge sharing. Key platforms include:
LinuxQuestions.org: A comprehensive forum where users of all skill levels can find answers to technical
issues, share experiences, and discuss Linux-related topics.
Stack Exchange: Sites like Ask Ubuntu, Unix & Linux Stack Exchange, and Server Fault provide platforms for
problem-solving, with community-driven Q&A tailored to specific needs and challenges.
Reddit: Subreddits like r/Linux, r/Ubuntu and r/Fedora are bustling with discussions, advice, and the latest
news, making them valuable for staying updated and connected with the community.
These forums are not just about troubleshooting; they are vibrant hubs where enthusiasts and professionals
alike share projects, tutorials and personal experiences, contributing to the collective knowledge pool.
Linux thrives on community contributions, which span from development to documentation. Individuals
and organizations can contribute to various aspects of Linux:
Kernel development: Contributors can help improve the core functionalities of the Linux kernel by adding
features, fixing bugs or optimizing performance.
Distribution development: Many distributions encourage users to contribute to packaging, testing and
artwork, among other areas.
Documentation: High-quality documentation is crucial for user support and software development.
Contributions help maintain and expand these resources, making Linux more accessible to newcomers.
Noteworthy platforms for development collaboration include GitHub and GitLab, where many Linux
projects are hosted. Contributors can fork projects, push updates, and collaborate with developers from
around the world.
Official documentation and tutorials: Most Linux distributions provide detailed manuals and tutorials that
are invaluable for both beginners and advanced users.
Online courses: Platforms like Coursera, edX, and Udemy offer courses on Linux fundamentals, system
administration, networking, and security.
Certifications: Certifications like the CompTIA Linux+ and the Red Hat Certified System Administrator
provide structured learning paths and are recognized credentials that can boost professional careers.
These resources not only help individuals improve their technical skills but also support career
advancement in IT fields where Linux proficiency is valued.
Linux continues to evolve, with regular updates and new features across various distributions:
Ubuntu: Recent releases have focused on enhancing support for cloud computing and containers, with
improved Kubernetes integration and updated toolchains for developers.
Fedora: Known for staying at the forefront of innovation, Fedora has recently introduced new security
features and enhanced system performance capabilities to cater to both developers and enterprise users.
Arch Linux: Continuously rolling out updates, Arch has improved its package management system and user
utilities, providing an even more streamlined and flexible user experience.
These updates are driven by feedback from the community and the ongoing need to support newer
hardware and software standards.
Linux on mobile: Projects like the Android Open Source Project (AOSP) and efforts by Purism, with its
Librem 5 smartphone, emphasize the potential for Linux in the mobile domain. These initiatives aim to
bring the robustness and flexibility of Linux to mobile devices, offering an open-source alternative to the
dominant iOS and Android platforms.
Increased focus on security: With cyber threats becoming more sophisticated, Linux is increasingly focusing
on enhancing security. Features like Kernel Lockdown and improvements to SELinux are making Linux more
secure for all users.
Containerization and microservices: The rise of Docker and Kubernetes has highlighted the importance of
Linux in the containerization ecosystem. Linux’s lightweight nature and strong isolation features make it
ideal for running containerized applications, a trend that continues to grow in software development and
deployment.
Looking ahead, Linux distributions are likely to focus on several key areas:
Sustainability and system stability: Efforts will be made to ensure that Linux distributions remain
sustainable and stable, supporting older hardware while also accommodating the needs of modern
systems.
Enhanced user experience: With the increasing adoption of Linux on desktops, distributions will likely
continue to polish user interfaces and improve usability to appeal to a broader audience.
Integration with emerging technologies: As technologies like artificial intelligence (AI) and the Internet of
Things (IoT) continue to evolve, Linux distributions will likely integrate more tools and features to support
developers and businesses in these areas.
Conclusion: The Enduring Importance of Linux in Modern Computing
Linux continues to be an element of contemporary computing, fueling progress in areas ranging from
software creation to cloud architecture. Its open source design, strong security capabilities and adaptable
settings position it as the pick for IT experts, developers and tech aficionados globally.
Versatility and power. Linux’s ability to run on a wide range of hardware, from supercomputers to mobile
devices, and its pivotal role in server management and cloud computing underscore its versatility and
power.
Community and collaboration. The vibrant Linux community continues to be a source of innovation and
support, offering resources, guidance, and a collaborative environment for users of all skill levels.
Continual evolution. Regular updates and the introduction of new features ensure that Linux remains
relevant and on the cutting edge of technological advancements.
Knowledge:
Complexity:
Linux, UNIX, BSD, Windows, and MacOS are all operating systems.
Abstract
An operating system is a set of programs that manages the available resources of a computer.
UNIX¶
1964 — 1968: MULTICS (MULTiplexed Information and Computing Service) is developed for
MIT, Bell Labs (AT&T) and General Electric.
1969 — 1971: After the withdrawal of Bell (1969) and then General Electric from the project, two
developers, Ken Thompson and Dennis Ritchie (joined later by Brian Kernighan), judging
MULTICS to be too complex, begin development of UNIX (UNiplexed Information and
Computing Service). While it was created in Assembly language, the creators of UNIX eventually
developed the B language and then the C language (1971) and completely rewrote UNIX. As it was
developed in 1970, the reference (epoch) date for the start of time of UNIX/Linux systems is set at
January 01, 1970.
C remains one of the most popular programming languages today. A low-level language close to the
hardware, it allows the operating system to be adapted to any machine architecture having a C compiler.
UNIX is an open and evolving operating system that has played a major role in the history of computing. It
forms the basis for many other systems, such as Linux, BSD, MacOS, etc.
GNU Project¶
1984: Richard Matthew Stallman launched the GNU (GNU's Not Unix) Project, which aims to
establish a free and open Unix system, in which the more important tools are: gcc compiler, bash
shell, Emacs editor and so on. GNU is a Unix-like operating system. The development of GNU,
started in January 1984, is known as the GNU Project. Many of the programs in GNU are released
under the auspices of the GNU Project; those we call GNU packages.
1990: GNU's own kernel, the GNU Hurd, was started in 1990 (before Linux was started).
MINIX¶
1987: Andrew S. Tanenbaum develops MINIX, a simplified UNIX, to teach operating systems in a simple
way. Mr. Tanenbaum makes the source code of his operating system available.
Linux¶
1991: A Finnish student, Linus Torvalds, creates an operating system that runs on his personal
computer and names it Linux. He publishes his first version, called 0.02, on the Usenet discussion
forum, and other developers help him improve his system. The term Linux is a play on words
between the founder's first name, Linus, and UNIX.
1993: The Debian distribution is created. Debian is a non-commercial, community-based
distribution. Originally developed for use on servers, it is well-suited for this role; however it is a
universal system, usable on a personal computer as well. Debian forms the basis for many other
distributions, such as Mint or Ubuntu.
1994: The commercial distribution Red Hat is created by the company Red Hat, which is today the
leading distributor of the GNU/Linux operating system. Red Hat supports the community version
Fedora and until recently the free distribution CentOS.
1997: The KDE desktop environment is created. It is based on the Qt component library and the C+
+ development language.
1999: The GNOME desktop environment is created. It is based on the GTK+ component library.
2002: The Arch distribution is created. Its distinctive is that it offers rolling release (continuous
update).
2004: Ubuntu is created by the Canonical company (Mark Shuttleworth). It is based on Debian but
includes free and proprietary software.
2021: Rocky Linux is created, based on Red Hat distribution.
Info
Dispute over the name: although people are accustomed to calling the Linux operating system verbally,
Linux is strictly a kernel. We must not forget the development and contribution of the GNU project to the
open source cause, so! I prefer to call it the GNU/Linux operating system.
Market share¶
Despite its prevalence, Linux remains relatively unknown to the general public. Linux is hidden within
smartphones, televisions, internet boxes, etc. Almost 70% of the websites in the world are hosted on a
Linux or UNIX server!
Linux equips about 3% of personal computers but more than 82% of smartphones. The Android
operating system, for example, uses a Linux kernel.
Linux equips 100% of the top 500 supercomputers since 2018. A supercomputer is a computer designed to
achieve the highest possible performance with the techniques known during its design, especially
concerning computing speed.
Architectural design¶
The shell is a utility that interprets user commands and ensures their execution.
o Main shells: Bourne shell, C shell, Korn shell, and Bourne-Again shell (bash).
Multi-task¶
Linux belongs to the family of time-sharing operating systems. It divides processing time between several
programs, transparently switching from one to another for the user. This implies:
Multi-user¶
MULTICS's purpose was to allow users to work from several terminals (screen and keyboard) on a single
computer (which was very expensive at the time). Inspired by this operating system, Linux kept this ability
to work with several users simultaneously and independently, each with its user account with memory
space and access rights to files and software.
Multi-processor¶
Multi-platform¶
Linux is written in a high-level language that can be adapted to different platforms during compilation.
This allows it to run on:
Open¶
Linux is based on recognized standards such as POSIX, TCP/IP, NFS, and Samba, which allow it to share
data and services with other application systems.
A Linux distribution is a consistent set of software assembled around the Linux kernel, ready to be
installed along with the necessary components to manage itself (installation, removal, configuration). There
are associative or community distributions (Debian, Rocky) and commercial distributions (Red Hat,
Ubuntu).
Each distribution offers one or more desktop environments, and provides a set of pre-installed software
and a library of additional software. Configuration options (kernel or services options for example) are
specific to each distribution.
This principle allows distributions to be geared to beginners (Ubuntu, Linux Mint...) or fully customizable
for advanced users (Gentoo, Arch); distributions can also be more adept with servers (Debian, Red Hat)
or workstations (Fedora).
Desktop environments¶
There are many graphic environments such as GNOME, KDE, LXDE, XFCE, etc. There is something for
everyone, and their ergonomics hold their own against Microsoft or Apple systems.
So why is there so little enthusiasm for Linux, when this system is practically virus free? Could it be
because so many editors (Adobe) and manufacturers (Nvidia) do not play the free game and do not provide
a version of their software or drivers for GNU/Linux? Perhaps it's fear of change, or the difficulty of
finding where to buy a Linux computer, or too few games distributed under Linux. That last excuse, at
least, shouldn't be true for long, with the advent of the game engine Steam for Linux.
The GNOME 3 desktop environment no longer uses the concept of desktop but that of GNOME Shell (not
to be confused with the command line shell). It serves as a desktop, a dashboard, a notification area and a
window selector. The GNOME desktop environment is based on the GTK+ component library.
The KDE desktop environment is based on the Qt component library. It is traditionally recommended for
users familiar with a Windows environment.
Free / Open Source¶
A Microsoft or Mac operating system user must purchase a license to use the system. This license has a
cost, although it is usually transparent (the price of the license is included in the price of the computer).
The Free Software movement provides mostly free distributions in the GNU/Linux world.
Open Source: the source code is available, so consulting and modifying it under certain conditions is
possible.
A free software is necessarily open-source, but the opposite is not true since open-source software is
distinct from the freedom offered by the GPL license.
The GPL guarantees the author of a software its intellectual property, but allows modification,
redistribution or resale of software by third parties, provided that the source code is included with the
software. The GPL is the license that came out of the GNU (GNU is Not UNIX) project, which was
instrumental in creating Linux.
It implies:
On the other hand, even products licensed under the GPL can have a cost. This is not for the product itself,
but the guarantee that a team of developers will continue to work on it to make it evolve and
troubleshoot errors, or even provide support to users.
Areas of use¶
Linux is a suitable choice for hosting databases or websites, or as a mail server, DNS or firewall. In short,
Linux can do just about anything, which explains the quantity of specific distributions.
Shell¶
Generalities¶
The shell, known as command interface, allows users to send commands to the operating system. It is less
visible today since the implementation of graphical interfaces, but remains a privileged means on Linux
systems which do not all have graphical interfaces and whose services do not always have a setting
interface.
It offers a real programming language including classical structures (loops, alternatives) and common
constituents (variables, passing of parameters, and sub-programs). It allows the creation of scripts to
automate certain actions (backups, creation of users, system monitoring, etc.).
There are several types of shells available and configurable on a platform or according to the user's
preference. A few examples include:
Functionalities¶
Introduction
Linux is a popular UNIX-based operating system. It is an open-source whose freely available code can be
used freely. Numerous versions of Linux are already available on the internet for use. Since Linux is an
open-source operating system, with enough knowledge one can customize it as per their needs rather
easily.
In this article, we will be reading the features and the architecture of a linux operating system in complete
detail.
2. Multiuser Capacity
3. Multitasking
4. Security
6. File System
7. Application Support
9. Portability
10. Performance
15. Interoperability
16. Shell
This Linux code is freely available to all, and it's a community-based development project which helps the
user to modify the code or analyze the codes and redistribution of codes.
2. Multiuser Capacity
In Multiuser Capacity, Linux allows its users to share the system resources such as RAM, hard drive, and
application program at same time. But they have to use it in different terminals.
3. Multitasking
Multitasking allows multiple applications to run simultaneously by effectively dividing the CPU time.
Examples of multitasking are UNIX, IBM’s OS/390, Windows Vista, Windows XP, and Windows 7, 8, 9,
and 10.
4. Security
It is a command line that can be converted into a Graphical User Interface by installing some packages.
The most popular method for getting a GUI in a Linux system is logging into a Ubuntu server and
installing its desktop environment.
6. File System
It provides a hierarchical system to arrange files and directories. The folders are classified into binary
directories, configuration directories, data directories, and memory directories based on the type of files
they contain.
7. Application Support
It consists of a software repository for users to download and install several applications, which gives the
user a vast field of applications to download the application of their choice.
Linux operating systems receive frequent updates, including security patches, bug fixes, and new features,
ensuring ongoing improvements and staying up-to-date with evolving technology.
9. Portability
Here the term portability doesn't mean the smaller size. Here, it means that Linux supports different kinds
of hardware, which means that the software can perform similarly on different types of hardware.
10. Performance
Linux operating systems are known for their performance, offering high stability, efficiency, and
scalability, making them widely used in various applications.
Linux can be configured into a lightweight infrastructure by using minimal resources, streamlined services,
and optimized kernel settings, ideal for resource-constrained environments or embedded systems.
12. Live CD/USB
Live CD/USB of a Linux operating system allows booting and running the OS from external media without
installation, useful for testing, recovery, or portable computing.
Linux offers support for customized keyboard layouts, allowing users to define and use specific key
mappings for various languages and preferences.
Linux is highly compatible with cloud computing platforms, offering a wide range of distributions and
tools for cloud deployment, scaling, and management. It's a popular choice for cloud servers and
containers.
15. Interoperability
Linux demonstrates strong interoperability, allowing it to work seamlessly with various software, file
formats, and systems, promoting open standards and collaboration across diverse environments.
16. Shell
The Shell command-line interpreter on Linux acts as a line between the user and the kernel, which runs the
program known as commands. It is efficient and takes less time and space while executing the tasks.
1. Kernel
The Kernel is the backbone of Linux. It is in control of the operating system's primary functions. It is made
up of various modules that interact directly with the hardware. The Kernel provides the needed abstraction
for system or application programs to mask low-level hardware specifics.
2. System Library
System libraries are unique functions or programs that allow application software or system utilities to use
the functionality of the Kernel. These libraries implement most of the operating system's features without
requiring kernel module code access.
3. System Utility
System Utility is a special type of program that is specialized to perform individual-level tasks.
4. Bootloader
Booting is the process through which computers startup. Your operating system controls this boot
procedure, which depends on what program opens on the boot. When you switch on your computer, the
bootloader for your operating system runs first.
5. Application Software
There are too many application requirements for the desktop environment which helps your Linux OS or
that you choose to install to satisfy them all. Individual applications are so installed. Most Linux
distributions, such as Ubuntu, provide dedicated app stores where users may search for and install
applications.
It is an Open Source Operating System that allows programmers to create their own bespoke operating
systems.
It provides various applications with various characteristics from which to pick based on your requirements.
A worldwide development community is looking at numerous methods to improve its security, which is
very safe and resilient. You don't need to scan it routinely with an anti-virus program. Linux is used by
companies such as Google, Amazon, and Facebook to defend their servers since it is very trustworthy and
stable.
Above all, Linux is completely free, and you may install it on as many computers as you wish.
An entirely trouble-free operating system does not cause viruses, spyware, or computer slowdown.
The Kernel, Hardware layer, System library, Shell, and System utility are the main components of the
Linux Operating System's architecture. The Kernel is the most crucial operating system component since it
is responsible for all of LINUX's essential functions.
Fedora, SuSE, and Mandrake are the most popular and most accessible. Linspire has grown in popularity as
a pre-installed operating system for consumers who wish to buy a low-cost computer but don't have any
technical knowledge. It's a matter of personal choice as to which distribution is best.
Linux operating system performs several functions such as multitasking, security, networking, file
management, open-source development and many more. It serves as a powerful open-source operating
system that
is secure for performing various computing tasks.
There are various advantages of using the Linux operating system. Some advantages include open source,
security, free of cost, development, flexibility, security, and many more.
14
Minutes
Linux is one of the most commonly used operating systems, serving as the backbone for a diverse array of
devices, from personal computers to servers and mobile phones. Linux has been around since the 1990s,
and it has since established a global presence, seamlessly integrating into virtually every imaginable
application and industry.
While the versatility of the Linux operating system is well known, many remain unaware of how
ubiquitous it is. In addition to supporting much of the internet, Linux OS is powering anything from TV
sticks to large appliances like refrigerators and elevators.
In this comprehensive introductory guide, we collected everything you need to know to learn if Linux is the
right fit for you, including its most common uses, basic terminology, a quick installation guide, and
answers to some of the most commonly asked questions about Linux OS. Whether you’re a complete
newbie or seeking to deepen your understanding, read on to learn more about the versatility and potential
of the Linux operating system.
Key Takeaways
Linux is an open-source operating system known for its security, reliability, and flexibility. Its core kernel
manages hardware resources, process multitasking, and invites collaborative development under the
General Public License (GPL).
There is a vast array of customizable Linux distributions and desktop environments, serving different needs
and preferences, from desktop computing and servers to gaming and embedded devices.
Linux offers a secure and customizable alternative to other operating systems; it features an active
community for support and development; and it´s widely employed in various devices and enterprise
solutions.
Serving as a liaison between software and hardware, an OS manages the intricacies of a computer’s
components. In the realm of operating systems like Linux or Windows, key components work in unison to
facilitate seamless functionality:
Bootloader. Your computer needs to go through a startup sequence called booting. This boot process
needs guidance, and your OS is the software in control throughout the boot process. When you start your
computer the bootloader for your operating system kickstarts the process.
OS Kernel. You can call the kernel the part of the operating system which is the “closest” to your computing
hardware as it is the part which controls the CPU, access to memory and any peripheral devices. It is the
“lowest” level at which your operating system works.
Background services. Called “daemons” in Linux, these small applications act as servants in the
background, ensuring that key functions such as scheduling, printing and multimedia function correctly.
They load after you have booted up, or when you have logged into your computer.
OS Shell. You need to be able to tell our operating system what to do, and this is the goal of the shell. Also
known as the command line, it is a facility which lets you instruct your OS using text. However few people
nowadays are familiar with command line code, and it once used to put people off using Linux. This
changed because a modern distribution of Linux will use a desktop shell just like Windows.
Graphics server. This provides a graphical sub-system that renders images and shapes on your computer
monitor. Linux uses a graphical server called “X” or “X-server”.
Desktop environment. You can’t interact with the graphical server directly. Instead you need software that
can drive the server. This is called a desktop environment in Linux and there are plenty of options including
KDE, Unity and Cinnamon. A desktop environment is usually bundled with a number of applications
including file and web browsers plus a couple of games.
Applications. Obviously, the desktop environment which is bundled with your Linux OS or which you
choose to install cannot cater for every application need, there are too many. Individual applications,
however, can and there are thousands for Linux just like Windows and Apple’s OS X has thousands of
applications. Most Linux distros have app stores which help you find and install apps, for example Ubuntu
Software which comes with Ubuntu.
It’s worth noting that Ubuntu’s application repository, the Ubuntu software centre, is a great place to look
around for Linux applications, both free to use and paid to use.
The desktop operating systems most of us use are typically bundled with our computers and we rarely
question why we need to change operating systems. Few people are interested in learning a new operating
system and rarely ask what Linux is simply because they feel their existing operating system does the job
just fine.
However, it is not always obvious how much time is lost in the process of battling common OS problems,
including malicious software such as viruses, frequent OS crashes, and the resulting costly repairs. Don’t
forget that most operating systems charge a license fee too.
Perhaps your existing choice of OS is not doing the job just fine. If you are tired of paying for an operating
system and hate the frequent, costly maintenance you have to do on your existing OS you might just want
to think about Linux and whether it offers a better, free alternative. There is no charge for trying Linux, and
many people will consider Linux to be the most reliable operating system for a desktop computer.
Because of the open-source, collaborative nature of Linux, there is no charge whatsoever to try Linux. You
can install the OS free of charge on an unlimited number of computers without paying anything towards
licensing. This is the case for both the server editions and the desktop editions of many Linux distributions.
In contrast with commercial solutions, many Linux distributions are completely free and include open-
source server software, so you can get started serving web pages without paying any fees for licensing.
Getting a fully functional Linux web server up and running is, in fact, just a few clicks away. To
learn more on Linux server distributions, read this post.
It’s also easy to argue that Linux software makes life easy for system administrators because Linux is more
reliable. It means you don’t need to closely watch your server every day; you can rely on it running without
a problem. Also, because of the way Linux is built, you can often restart individual services without
impacting the entire Linux OS.
By definition, an operating system is a tool that you need to be able to rely on. If cost is not the biggest
factor for you, the reliability that Linux brings can be game-changing. Wondering what the Linux operating
system’s biggest advantage is? Well, its inherent reliability and its general immunity to viruses, malicious
software, and other random operating system issues are perhaps the biggest reasons to adopt Linux.
Server reboots are a particular problem for sysadmins, and Linux, due to its stability, allows sysadmins to
largely avoid reboots unless the kernel is updated. Many Linux servers can run for years without seeing a
reboot, and sysadmins would often consider this a sign of the reliability of Linux.
Open source: Linux equals freedom
We mentioned open source earlier in this article. But what is open source? Simply put, any software that is
open source follows a set of principles, which include:
Full freedom to run the software, no matter your reason for running it or your goals
Permission to examine and disassemble the software, to study it, and to make any changes you want to
make to it
There are no restrictions on distributing the software
There are no restrictions on distributing any copies of the software modified by you
It is also important to understand that open-source software is a community. It´s a community that built
Linux and a community that maintains Linux. Arguably, it´s this open-source philosophy that has made
Linux so popular.
The different editions, or distributions, of Linux can be vary varied. They can be aimed at desktop use, or
designed to be used as server software. Some distributions of Linux focus on expert users, other Linux
distributions are easy enough to use for beginners. Also known as distros, most Linux editions can be
downloaded free of charge and burned to an optical disk or USB drive for installation.
There are an almost endless number of Linux distributions. For desktop Linux users the default choice is
often Ubuntu, but Fedora, Arch, Linux Mint, Debian and openSUSE are also popular choices. Ubuntu is
one of the most modern thanks to Ubuntu Unity while KDE, included with openSUSE, has a more
traditional Linux look.
Looking for the server Linux OS list? It’s a long list, but some of the most well know distros include Red
Hat, Ubuntu Server, SUSE Enterprise and CentOS. However some Linux server distros are not free, Red
Hat requires licensing but bear in mind that you do get support in return for your license fee, which can be
important for businesses.
A number of Linux distributions have gained a following for their unique features and specialized
offerings. For example, Ubuntu is highly regarded for its user-friendly interface and various versions
tailored to desktop, server, and IoT needs. It also boasts the support of Canonical as well as a customized
GNOME desktop environment.
On the other hand, Fedora remains closely aligned with traditional GNOME experiences while providing
software management through dnf, rpm, and Flatpak.
Red Hat Enterprise Linux which focuses on long-term commercial stability backed by default use of
GNOME.
MX Linx which merges MEPIS Linux and antiX to create an intermediate distribution offering both
reliability from its stable foundation along with current applications made possible thanks in partto MX-
tools;
openSUSE caters towards diverse users via Leap (a more consistent edition) or Tumbleweed’s rolling
release system. Each differs according to preferences/needs.
Linux stands out from Windows and macOS in its flexible graphical user interfaces, offering a wide range
of customization options. Popular choices include GNOME with personalized touches on Fedora and
Ubuntu, Cinnamon, MATE, and Xfce for Linux Mint users who prioritize lightweight alternatives like
LXQt or Xfce as seen in Lubuntu and Linux Lite.
Major versions of various distributions also present diverse desktop environments such as KDE, Pantheon,
Mate among others to cater to different preferences for visual appeal or functionality. Lightweight
solutions are preferred by some users, resulting in the popularity of Openbox, SliTaz, IceWM along with
Trinity Desktop which can be found on Q4OS,Tiny Core Linux, Absolute Linux etc.
When compared to other operating systems, Linux stands out for its strong security measures and
flexibility. The architecture of Linux has various built-in features that make it more secure than many
competing operating systems. There have been instances of specific malware targeting Linux, such as
Symbiote. Despite this potential vulnerability, the proactive community consistently tests and addresses
any security flaws in the system.
While the growing popularity of Linux has made it a target for malicious attacks like malware, overall it
remains reliable and cost-effective due to its lower number of targeted vulnerabilities compared to
Windows – largely because Windows still dominates with approximately 75% market share on desktops
while only about 3% use Linux instead.
Nevertheless, a significant portion continues to choose Linux over others due to their prioritization of
safeguarding computer data and reducing risk exposure by using less attractive operating systems which
are likely already hacked or at least regularly tested for safety glitches. Even though its user base is
relatively small compared to those who prefer Windows, Linux retains an appeal among security-conscious
users which amounts up not more than three percent of those who opt for a safer computing experience
foregoing the popular yet potentially risky Microsoft operating system (Linux uses a steadyitie between
upgrades).
Security Features
Linux is known for its robust security features, which include file permissions and the use of Security-
Enhanced Linux (SELinux) to enforce additional application and network-level protections. These
measures contribute to its reputation as a more secure operating system than others.
When it comes to file permissions in Linux, they are based on Unix’s model that allows owners, groups,
and other users different levels of access such as read, write, and execute. This highly configurable nature
adds another layer of security. Additionally, UX-based systems rely on optional packages like ClamAV for
antivirus protection instead of having an integrated one like Windows 10 or built-in technologies similar to
macOS’ Gatekeeper.
Although all three major operating systems – Linux,Windows & macOS – utilize codesigning, to ensure
trustworthiness, it’s worth noting that there have been some instances where unsigned code has slipped
through with Tux at a slightly higher frequency compared with the other two OSs’.
The open-source nature of Linux allows for extensive customization, from the kernel to the desktop
environment. This enables users to create a personalized computing experience tailored to their specific
needs.
Linux’s file system and directory structure can be extensively configured, giving users even more control
over their systems. Scripting and automation are deeply integrated in Linux, providing power users with
command-line tools that allow them to automate tasks and adjust system behavior according to their
preferences. The flexibility and level of personalization of these features make Linux a powerful tool for
those who know how to use it effectively.
Linux is widely utilized in a diverse range of devices, including smartphones, cars, supercomputers, home
appliances, desktops, servers, and embedded systems. Its strengths lie in providing dependable and secure
desktop environments as well as graphical server solutions that require minimal maintenance.
The versatility of Linux allows it to cater to the needs of various users, such as business owners looking for
robust web server platforms or developers creating applications for mobile devices. Even hobbyists
experimenting with their own home automation systems can benefit from its tailored solutions. This
demonstrates the expansive capabilities and reach of this operating system, making it an excellent choice
for any Linux system.
One key feature that makes Linux stand out is its reliability on different types of machines, ranging from
small-scale personal computers to large enterprise-grade servers. It also offers flexible options like
graphical interfaces, which are user-friendly yet low-maintenance compared to other traditional operating
systems, allowing businesses more convenience with their data handling methods without compromising
security measures due to constant monitoring requirements.
It’s no wonder why so many individuals turn towards utilizing a Linux-based environment, whether they’re
trying to optimize resource management throughput levels or ensuring foolproof access restrictions
throughout all organization layers used. Regardless of whether you use your computer on a work, home, or
mobile platform, there will always be something new to explore while learning after switching over
completely painlessly.
Web Servers and Enterprise Solutions
In the realm of web servers and corporate environments, a Linux server provides customizable solutions for
hosting and balancing loads to meet diverse data usage requirements. With over 41.8% of known operating
systems using Linux, this technology is widely adopted by various websites worldwide.
Some of the popularly used distributions include Ubuntu, Debian, CentOS, Red Hat Gentoo, and
AlmaLinux.
Leading cloud service providers such as AWS, Google Cloud, and Microsoft Azure have successfully
integrated Linux into their services, catering to a wide array of computing and hosting demands. Different
enterprise needs are met with specific versions and distributions offered within Linux; Fedora requires
updates every nine months, followed by reboots, whereas Ubuntu Server has gained recognition for its
ability to support cloud-native applications.”
Linux serves as the base for a variety of popular consumer electronics, including smartphones, smart TVs,
in-vehicle infotainment systems, and routers. Android is one prime example where Linux dominates the
mobile operating system market.
Embedded Linux can be found at work in various devices, such as Sharp Zaurus PDAs or TomTom GPS
units, along with Linksys routers and Motorola smartphones, which run on the Sailfish OS. This
widespread use of Linux reflects its versatility and reliability.
Whether you’re using your smartphone to surf the internet or relying on your GPS device to navigate
through unfamiliar streets, chances are high that you’re utilizing a product powered by Linux technology.
Your Linux distro of choice is going to depend on your personal needs. Foremost, you should consider
your computer skills. If you have never used the command line or Linux, you will be more restricted in
your choices. Another obvious point to consider is: are you going to use your Linux OS on a desktop or to
serve applications? And, if desktop, do you prefer one of the modern-looking distros or a more classic
Linux look?
With basic computer skills, you should look at a distribution that caters to inexperienced Linux users.
Linux Mint and Deepin are good choices. Are you a pro Linux user? You might prefer to use Fedora or
Debian, while the most experienced users could choose Gentoo.
As for servers, consider the need for a GUI. Some servers are best managed via the command line, which
means your server won’t be slowed down by the graphics server. Some server distros won’t come with a
GUI, some will, and some, like Ubuntu, will allow you to add a GUI any time after you’ve installed the
server.
Some server distros are good for specific applications, including a lot of pre-bundled services. CentOS is a
good example, as it offers a lot of what you need to run a comprehensive server out of the box. You can
even start with a desktop distribution and add Linux operating system components as and when you need
them. Consider Debian or Ubuntu if that’s the case.
Transitioning to Linux involves creating a bootable installation media, partitioning the hard drive, and
using package managers for software management. It is possible to have multiple operating systems on one
computer by dividing the hard drive and utilizing a boot manager like GRUB during system startup.
Don’t be intimidated! Live distributions in Linux make it easier to install without permanently saving it on
your device. The key element of managing and installing software in Linux is leveraging its specific
distribution package manager(s).
Installation Process
The process of installing Linux is fairly straightforward. To install a Linux distribution, you will need to
create bootable installation media with at least 4GB of space on a USB thumb drive or external hard drive
using Rufus. The recommended minimum amount of dedicated space for the operating system is 20GB
when partitioning your PC’s hard drive.
2. Go through an installation wizard where you must choose language, time zone, and keyboard layout.
You´ll then be prompted by GNU GRUB to select which operating system they would like to use. There
may be options available during this process, such as downloading updates or third-party software, if
desired by Linux users.
As you embark on your journey with Linux installation, we understand that it’s essential to start with the
basics. For those new to Linux, we’ve prepared a beginner-friendly guide on setting up a Linux mail server,
empowering you to expand your server’s functionalities while gaining valuable hands-on experience.
Additionally, discover our tutorial on finding files in Linux from the command line, providing you with
fundamental skills to navigate your system efficiently. Let’s dive in and make your Linux journey a smooth
and enjoyable one!
Linux uses a package manager to effectively manage software installation by adding, updating, or
removing components from trusted repositories. On Debian-based systems, users can utilize the command
line with ‘sudo apt install packagename’ while on DNF-based systems like Fedora, it would be ‘sudo dnf
install packagename’. Before initiating any installations, keeping an updated list of available software is
important, and this can be done through commands such as ‘sudo apt update’ for Debian-based systems
and sudo dnf makecache for DNF-based systems.
To find specific packages that meet their needs, users have options depending on the type of system they
are using. For instance, on Debian-based systems, keyword searching is possible via “apt-cache search”
followed by desired keywords, whereas, for those on DNF-based ones, the equivalent command would
involve typing in’sudo dnf search softwarename‘.
Some people may opt for graphical user interfaces instead, particularly if they prefer visual representations
when working; GNOME Software, KDE Discover, and Synaptic are examples of tools offering easy-to-use
graphical interfaces for managing software. In addition to these traditional packaging methods, some
universal solutions, including Flatpak and Snap, also exist that provide cross-distribution compatibility thus
easing sharing between different Linux distributions.
In addition to general software installation, users may also install web hosting control panels like Plesk on
Linux servers. If that is your case, here´s how to install Plesk on Linux.
The community of Linux users offers various resources, such as online forums, documentation, and local
gatherings known as Linux User Groups (LUGs), to promote collaboration, provide support, and facilitate
learning. LUGs that are successful typically maintain a user-friendly website with easily accessible URLs,
share event details, including maps and directions, welcome all members of the public at their meetings
without charging any fees, and regularly update their online content with relevant information along with
functional hyperlinks.
Individuals who acquire proficiency in commonly used open-source tools like the Linux kernel can take
advantage of increased employment opportunities by actively contributing to them. Embedded systems also
have thriving communities involved in developing Linux-based devices through initiatives such as
Embeddable Linux Kernel Subset, Yocto, OpenEmbedded, and OpenWrt. All these groups play a vital role
in enhancing the development process for embedded systems using Linux technology.
Linux User Groups (LUGs) are instrumental in promoting preferred Linux distributions and providing
various forms of support, such as demonstrations, technical assistance, training sessions, and guidance for
newcomers on how to install the operating system. These groups gather physically and virtually through
online platforms to organize social events, deliver technical talks, and facilitate informal discussions
among members with a shared interest in using Linux.
For LUGs to maintain an active community that operates effectively over time, it is recommended they
establish consistent meeting times and locations while being mindful of avoiding scheduling conflicts.
Furthermore, having a dedicated core group comprising enthusiastic users can greatly contribute to their
success. LUG websites serve as valuable tools for showcasing past activities, enabling effective
communication among members, and offering practical learning resources related to internet technologies
specific to the use of Linux.
The availability of online platforms such as Ubuntu Forums plays a crucial role in facilitating knowledge
sharing, troubleshooting discussions, and highlighting the strong support system present for Linux users.
The forum is structured to include both a resolution center and a council agenda dedicated to governing the
community and resolving conflicts within it.
These internet resources are vital to fostering collaboration among members of the Linux community by
providing an inclusive platform where they can exchange ideas, seek assistance with problems, and find
solutions. They effectively showcase the power behind open-source principles by promoting a culture that
values sharing information and continuous learning. Thus driving forward progress and innovation within
Linux.
Linux offers a vast range of possibilities for individuals and businesses alike due to its open-source nature,
backed by a thriving community.
In this article, we covered the fundamentals of the Linux Operating System and listed the most common
use cases where Linux is employed. We also covered the unique security features of Linux and the
advantages of its versatility, as well as how it can adapt to users with different proficiency levels.
Linux is an open-source operating system kernel. It differs from proprietary operating systems like
Windows and macOS in that it is freely distributable, customizable, and supported by a vast community of
developers worldwide.
Linux is renowned for its security features, including user permissions, robust access controls, and regular
security updates. However, no system is entirely immune to vulnerabilities, so it’s essential to practice
good security hygiene, such as installing updates promptly and using reputable software sources.
Linux offers numerous advantages, including stability, security, flexibility, and cost-effectiveness. It can
breathe new life into older hardware, provide robust server capabilities, and offer a wide range of software
options tailored to various needs.
Is Linux suitable for enterprise environments?
Linux is widely used in enterprise environments for servers, cloud computing, networking, and embedded
systems. Its stability, scalability, and cost-effectiveness make it a compelling choice for businesses seeking
reliable solutions without vendor lock-in.
While Linux may have a learning curve for beginners accustomed to other operating systems, many
distributions (distros) offer user-friendly interfaces and extensive documentation. With some patience and
practice, users can quickly adapt to Linux and its ecosystem.
Yes, you can dual-boot Linux alongside Windows or macOS, allowing you to choose which operating
system to use at startup. Alternatively, you can run Linux within a virtual machine for experimentation or
as a secondary environment.
Linux offers various package management systems, such as apt (Advanced Package Tool), yum
(Yellowdog Updater, Modified), and Pacman, depending on the distribution. Users can install software
using these package managers, which simplify the process of downloading, installing, and updating
applications.
Linus Torvalds, a Finnish software engineer, created Linux in 1991. He developed the Linux kernel as an
open-source project, originally intending it to be a Unix-like operating system kernel for personal
computers. Torvalds released the first version of the Linux kernel to the public on September 17, 1991.
Since then, Linux operating system has evolved with contributions from thousands of developers
worldwide.
The Linux kernel serves as the core element of the operating system and is responsible for managing
hardware resources. It plays a crucial role in controlling how different components interact with each other.
A Linux distribution, commonly referred to as a distro, is a specific version of Linux tailored to fulfill
particular purposes. It´s a complete operating system based on the Linux kernel, along with various
software applications and utilities. While the Linux kernel itself is the core component responsible for
managing hardware resources and providing basic system functionalities, a Linux distribution packages the
kernel with additional software, libraries, and configuration files to create a usable operating system with
specific objectives or intended
Linux File System and Directory Structure
By Neil Mitra
9 mins read
819 views
Topics Covered
Overview
A file system is a method for storing and organizing data on a computer. In Linux, the file system is a
crucial component of the operating system, responsible for managing data stored on disk and other storage
devices. The Linux file system is designed to provide a hierarchical and organized structure for files and
directories, making it easy to navigate and manage data on the system.
The Linux file system is the structure that the Linux operating system uses to organize and store files and
data on a computer. It is a hierarchical, tree-like structure that starts with the root directory, which contains
all other directories and files. The Linux file system is based on the Unix file system, which was designed
to be scalable, flexible, and easy to use.
One of the key features of the Linux Operating System is that everything in Linux is a file, including
devices, programs, and system information. Even the most basic commands such as ls and cat are also files,
which lies inside the /bin directory, which itself is also a file. You can try to catch the cat command by
getting inside the /bin directory and see what happens.
The Linux Operating System is designed to support multiple file systems, each with its own characteristics
and features. This allows users to choose the best file system for their needs, depending on the specific
requirements of their system. We will discuss more about different file systems further in this article.
The Linux file system structure is a complex hierarchy that includes multiple layers, each with its own role
in managing and organizing data on storage devices. At the top of the hierarchy is the root directory (/),
which contains all other directories and files. Below the root directory are several subdirectories, each with
its own purpose and set of files.
The file system is designed to manage non-volatile storage data by organizing it into files and directories,
which can be accessed and modified by users and applications. Linux uses different file systems such as
ext4, XFS, Btrfs, JFS, and ZFS to manage and store data on storage devices. It also provides a namespace,
which is a way of organizing files and directories into a logical hierarchy. The namespace allows multiple
users and applications to access the same storage device without conflicts.
The Linux file system structure also provides an API (Application Programming Interface) that allows
applications to interact with the file system. The API provides a set of functions and commands that allow
applications to create, modify, and delete files and directories, as well as to read and write data to and from
storage devices.
There are several characteristics of a file system that make it an essential component of any operating
system. Some of the most important characteristics are discussed below:
File Organization:
The file system is responsible for organizing the files and directories on a disk. It provides a logical structure
to the data stored on the disk, making it easy for users and applications to locate and access files.
File Naming Conventions:
The file system provides a set of rules for naming files and directories, ensuring that they can be easily
identified and managed. It also allows for the creation of long filenames, which can be more descriptive
and meaningful than traditional filenames.
File Metadata:
The file system stores metadata about each file and directory, such as its name, size, creation and
modification dates, permissions, and ownership. This information is used by the operating system to
manage the file system and by applications to locate and manipulate files.
File Access Control:
The file system provides a way to control access to files and directories, allowing users and applications to
specify who can read, write, or execute files. This is important for maintaining the security and integrity of
the data stored on the disk.
File Compression:
The file system can compress files to reduce their size and save disk space. This can be useful for archiving
files or for transferring them over the internet.
File Fragmentation:
As files are added, deleted, and modified on the disk, the file system may become fragmented, meaning
that the data for a file is stored in multiple locations on the disk. Fragmentation can slow down file access
and reduce overall system performance.
File Recovery:
The file system provides a way to recover data from damaged or corrupted files or disks. This can be done
using file system utilities or specialized data recovery software.
File System Type:
There are different types of file systems available, each with its own set of features and limitations. Some
of the most common file systems include ZFS, JFS, and ext4.
File System Performance:
The performance of the file system can have a significant impact on overall system performance. Factors
such as disk speed, file system type, and disk fragmentation can all affect file system performance.
File System Scalability:
The file system must be able to scale to accommodate the growing storage needs of users and applications.
This can be achieved through the use of larger disks or by implementing file system features such as disk
quotas or file compression.
Quiz Pop
Quiz Type
SCQ
100
Success Rate:35%
The storage of data from one file in multiple locations on the disk.
Submit
There is a wide range of file systems available in Linux, each of which is designed to meet specific needs.
We will learn about some of the most common file systems in Linux below :
ext2:
The ext2 (extended file system 2) is one of the oldest and most widely used Linux file systems. It has been
the default file system for most Linux distributions for many years. The ext2 file system does not have a
journal, which means that file recovery can be more difficult after a system crash. However, it is a stable
and reliable file system that is ideal for small to medium-sized partitions.
ext3:
The ext3 (extended file system 3) is an extension of the ext2 file system that adds journaling support.
Journaling keeps track of changes to the file system, which helps to reduce the risk of data loss after a
system crash. The ext3 file system is widely used and is a good choice for most Linux installations. It is
stable, reliable, and has good performance.
ext4:
The ext4 (extended file system 4) is the successor to the ext3 file system and was designed to address some
of the limitations of ext3. It supports larger file sizes, faster file system checks, and better performance on
large disks. It also includes journal checksumming, which improves data integrity. The ext4 file system is the
default file system used by many modern Linux distributions.
XFS:
The XFS(X File System) is a high-performance file system designed for large-scale storage systems. It
supports file systems up to 16 exabytes in size, making it suitable for use in large data centers. The XFS file
system is known for its scalability, performance, and reliability. It supports advanced features such as
journaling, file-level encryption, and online defragmentation.
Btrfs:
The Btrfs (B-tree file system) is a modern file system designed for use on Linux systems. It supports features
such as copy-on-write, snapshots, and subvolumes, which allow users to create separate file systems within
a single partition. It also includes built-in support for RAID and compression. Btrfs is still under active
development and is not yet as widely used as some of the other file systems.
ZFS:
The ZFS (Zettabyte File System) is a powerful and feature-rich file system that was originally developed for
Solaris. It supports advanced features such as snapshots, data compression, deduplication, and built-in
RAID. ZFS is known for its reliability and data integrity features, including checksumming and self-healing
capabilities. It is not included in most Linux distributions by default due to licensing issues but can be
installed separately.
JFS:
The JFS(Journaled File System) is a high-performance file system that was originally developed by IBM. It
includes advanced features such as journaling, file-level compression, and online resizing. The JFS file
system is known for its speed and reliability and is a good choice for high-performance computing systems.
ReiserFS:
The ReiserFS is a file system that was designed for high-performance computing systems. It includes
advanced features such as journaling, file-level encryption, and support for large files and directories. The
ReiserFS file system is known for its speed and reliability but is not as widely used as some of the other file
systems on this list.
Directories, also known as folders, are an integral part of the Linux file system. They serve as containers
for files and other directories and are used to organize and structure the file system. A directory can be
thought of as a virtual container that holds files and other directories within it.
One of the primary functions of directories in Linux is to provide a hierarchical structure for organizing
files. This structure starts at the root directory (/), and branches out into subdirectories as needed. With the
help of this hierarchical structure, complicated systems can be structured logically and organized. This
makes it simpler to locate and handle files on the system.
Each directory in Linux file system has a specific function, Some of a few key directories are listed below :
/ (root directory):
The root directory is the top-level directory in the Linux file system. All other directories and files are
contained within the root directory.
/bin:
The /bin stands for binaries. This directory contains essential command-line tools and programs that are
required for basic system administration tasks.
/boot:
The /boot directory contains the boot loader files and kernel images needed to start the system.
/dev:
The /dev directory contains device files that represent hardware devices and virtual devices such as
terminals, printers, and disks.
/etc:
The /etc directory contains system configuration files that are used by various applications and services on
the system.
/home:
The /home directory contains the home directories of users on the system. Each user has their own
subdirectory within /home where they can store their personal files and settings.
/lib:
The /lib directory contains shared library files that are needed by various programs on the system.
/media:
The /media directory is used to mount removable media such as CDs, DVDs, and USB drives.
/mnt:
The /mnt directory is used to mount file systems temporarily, such as network file systems or disk images.
/opt:
The /opt directory is used to store additional software packages that are not part of the core system.
/proc:
The /proc directory is a virtual file system that provides information about running processes and system
resources.
/run:
The /run directory contains temporary files that are created by system services and daemons.
/sbin:
The /sbin directory contains system binaries and administrative tools that are required for system
maintenance.
/srv:
The /srv directory is used to store data for services provided by the system.
/sys:
The /sys directory is a virtual file system that provides information about the system's hardware and
devices.
/tmp:
The /tmp directory contains temporary files that are created by applications and services running on the
system.
/usr:
The /usr directory contains user-level programs, libraries, documentation, and shared data files.
/var:
The /var directory contains variable data files that change frequently, such as log files and system
databases.
Quiz Pop
Quiz Type
SCQ
100
Success Rate:35%
Which directory in Linux is specifically used for mounting file systems temporarily?
/media
/mnt
/opt
/tmp
Submit
Conclusion
The Linux file system and directory structure are fundamental components of the operating system,
enabling users to effectively manage and organize their data while maintaining system integrity and
security.
Linux uses a hierarchical file system that is organized in a tree-like structure.
The topmost directory in any Linux file system is the root directory.
There are different types of file systems available in Linux, and each type has its own advantages and
disadvantages, depending on the use case.
The Linux directory structure contains various directories for system binaries, boot files, device files,
configuration files, user files, libraries, and system-generated files.
Linux operating system
By
What is Linux?
Linux is a Unix-like, open source and community-developed operating system (OS) for computers, servers,
mainframes, mobile devices and embedded devices. It is supported on almost every major computer
platform, including x86, ARM and SPARC, making it one of the most widely supported operating systems.
Every version of the Linux OS manages hardware resources, launches and handles applications, and
provides some form of user interface. The enormous community for developers and wide range of
distributions means that a Linux version is available for almost any task, and Linux has penetrated many
areas of computing.
For example, Linux has emerged as a popular OS for web servers such as Apache, as well as for network
operations, scientific computing tasks that require huge compute clusters, running databases, desktop and
endpoint computing, and running mobile devices with OS versions like Android.
The Linux OS can be found in many different settings, supporting many different use cases. Linux is used
in the following ways:
Server OS for web servers, database servers, file servers, email servers and any other type of shared server.
Designed to support high-volume and multithreading applications, Linux is well-suited for all types of server
applications.
Desktop OS for personal productivity computing. Linux is an open source and freely available desktop
environment for users who prefer it to commercial OSes.
Headless server OS for systems that do not require a graphical user interface (GUI) or directly connected
terminal and keyboard. Headless systems are often used for remotely managed networking server and
other devices.
Embedded device or appliance OS for systems that require limited computing function. Linux is used as an
embedded OS for a variety of applications, including household appliances, automotive entertainment
systems and network file system appliances.
Network OS for routers, switches, domain name system servers, home networking devices and more. For
example, Cisco offers a version of the Cisco Internetwork Operating System (IOS) that uses the Linux kernel.
Software development OS for enterprise software development. Although many development tools have
been ported to Windows or other OSes, Linux is home to some of the most widely used open source
software development tools. For example, git for distributed source control; vim and emacs for source code
editing; and compilers and interpreters for almost every programming language.
Cloud OS for cloud instances. Major cloud computing providers offer access to cloud computing instances
running Linux for cloud servers, desktops and other services.
Linux is highly configurable and depends on a modular design that enables users to customize their own
versions of Linux. Depending on the application, Linux can be optimized for different purposes such as:
networking performance;
computation performance;
deployment on specific hardware platforms; and
deployment on systems with limited memory, storage or computing resources.
Users can choose different Linux distributions for specific applications or adapt a specific distribution to
incorporate custom kernel configurations.
Linux distributions
Since its initial development, Linux has adopted the copyleft stipulations of the Free Software Foundation
which originated the GNU GPL. The GPL says that anything taken for free and modified must be
distributed for free. In practice, if Linux or other GNU-licensed components are developed or modified to
create a new version of Linux, that new version must be distributed for free. This prevents a developer or
other groups from profiting unfairly from the freely available work of others.
Linux
Hundreds of different Linux versions, also known as distributions or distros, are available. Distributions
usually distinguish themselves from the pack by addressing a specific goal, philosophy, function or target
market.
There are distributions tailored for specific target functions, such as servers, desktops, gaming, security, or
embedded devices including Raspberry Pi systems. Most modern distributions are precompiled and ready
to use, while others like Gentoo Linux consist of source code that a user can compile locally during initial
installation to optimize their system configuration. Knoppix Linux is one of many distros used to recover
damaged hard drives and perform other technical support tasks. Information security professionals use Kali
Linux for penetration testing and other security-related tasks.
Linux has become an important component of automaker entertainment systems. Many automakers have
joined Automotive Grade Linux (AGL), an open source project hosted by the Linux Foundation. For
example, Toyota and Lexus vehicles use AGL for their infotainment systems.
Linux distributions may be community-developed, like Debian, Slackware and Gentoo. Other distributions
are commercial and intended for enterprise use, including Red Hat Enterprise Linux and SUSE Linux
Enterprise Server. Many distributions use a combination of community- and corporate-supported
development, such as Red Hat's Fedora, openSUSE from SUSE and Ubuntu from Canonical.
This chart highlights
nine popular Linux distributions.
The GNU GPL does not prohibit intellectual ownership, and it is commonplace for creators of Linux
components to hold copyrights on the various components. The GNU GPL ensures that those components
remain free and freely distributed. While the software remains free, it is common for some commercial
distributions to charge for value-added services, such as support or custom development services.
Bootloader. A bootloader is responsible for managing the boot process of the computer and for starting
the Linux kernel. It can also be used to manage systems that boot more than one OS.
Kernel. The core of the Linux system, the kernel handles network access, schedules processes or
applications, manages basic peripheral devices and oversees all file system services. The Linux kernel is the
software that interfaces directly with the computer hardware.
Init system. The first process to run once the kernel is loaded. A process is an instance of a program
running on a computer, and the init system initializes the system to enable it to run other processes. Init is
a daemon program that acts as the parent process to all other processes running on the system. Init can be
configured to start specific processes at system initialization. For example, when the system will be running
a web server the init system can be configured to load all necessary web server software.
Daemons. This is a program that runs in the background, handling requests for a service. A web server
running on a Linux server depends on a daemon, usually named httpd, to listen for web server requests.
Graphical server. This is the software that controls how graphics are displayed on a computer. Without a
graphical server, users can only interact with the Linux system through a command-line interface. The X
Window System, also known as X11 or X, is the most common graphical server for Linux, though not the
only one. X runs as a server daemon on the system and is called upon by applications when graphical
output is required.
Desktop environment. This is the collection of applications and user interface controls with which users
interact when using Linux as a desktop platform. Access to the desktop environment is usually controlled
through the X Window System or another graphical system. Each desktop environment defines its own
look, including the way graphical elements like windows, pull-down menus and files are displayed and
manipulated. The desktop environment will also include a set of default applications for managing files and
folders, text editing, running a command-line session and other common tasks.
Applications. This is the software that is installed during and after the initial Linux installation. Most Linux
distributions include thousands of different applications, including both for a networked server and for
desktop use.
While these components are included in most Linux distributions, they are not necessarily part of every
deployed Linux system. For example, a Linux-based server may not require a graphical server, desktop
environment or applications.
But it is really the many outside developers and GNU projects that offer high-level functions to the Linux
kernel to provide a fully realized OS. For example, there are modules to provide a command-line interface,
implement a GUI, manage security, and offer video input or audio services -- each of which can be
modified and optimized to form unique distributions for specific tasks.
Desktop environments can also vary widely with different approaches to GUI design and default
applications. The two most popular desktop environments are:
The GNOME desktop environment, which is included in most popular Linux distributions, is the default
desktop for many. Designed to be easy to use and reliable, GNOME spawned other desktop environment
projects, including MATE, Cinnamon and Unity.
The KDE desktop environment is the primary alternative to GNOME. KDE is also designed to be easy to use
and reliable. It has spawned other projects, including the Trinity Desktop Environment.
Package manager software typically adds, updates or removes software components under the Linux OS.
Package managers enable users to install additional software not included with their distributions.
Examples of software package managers include RPM Package Manager, dpkg, OpenPKG and Zero
Install.
The Linux OS follows a modular design that is the key to its many variations and distributions. All Linux
distributions are based on the Linux kernel, but they can differ depending on factors such as:
Kernel version. Distributions can be configured with more recent versions to incorporate newer features or
with older versions to be more stable.
Kernel modules. This is software that can be loaded and unloaded into the kernel to extend functionality
without rebooting. Kernel modules are often used to support:
o device drivers, which use code that controls how attached devices operate;
o file system drivers, which use code that controls how the kernel works with different file systems;
and
o system calls, which use code that controls how programs request services from the kernel.
Configuration options. Linux kernels compiled with configuration options set to include only device or file
system drivers are used for some specialized distributions; for example, compiling a kernel for a wireless
device without any wired network device drivers.
The Linux kernel is the one thing that all systems running Linux have in common. Linux works by:
While the kernel may be almost identical -- with some divergence for configuration and compilation
differences -- the user experience can vary widely, depending on how the Linux system is being used. For
example, some Linux use cases with widely different user experiences include:
Desktop productivity systems, such as those used by software developers or other professionals. Software
development workstations may be optimized for performance, while desktops for administrative
professionals may be optimized for use of desktop productivity tools.
Network servers may not even include a terminal for direct access. These headless servers are managed
remotely through network terminal or Windows sessions. Servers may be used by many but should be
directly accessed only by authorized system admins.
Thin clients enable users to access a rich desktop environment from a lightweight device. This includes
Raspberry Pi single-card computers and Google Chromebooks.
When using Linux with a desktop environment as a GUI, Linux works much the same as any GUI-based
OS. Applications and other resources can be opened by clicking on icons, and files can be moved, copied
or deleted using a mouse or trackpad.
Likewise, using the Linux command line is similar to any modern OS command line:
userID@DESKTOP-2KD3FT2:/mnt/c/WINDOWS/system32$
This example shows the default command prompt in the Windows Subsystem for Linux. The prompt
displays, from left to right, userID@hostname, and the full path of the current directory followed by the "$"
symbol.
Experience Linux on
Windows 10 (or later) desktop by running the Windows Subsystem for Linux.
Open source software. The Linux kernel is released under the GNU GPL open source software license. Most
distros include hundreds of applications, with many options in almost every category. Many distributions
also include proprietary software, such as device drivers provided by manufacturers, to support their
hardware.
Licensing costs. Unlike Microsoft Windows or Apple macOS, Linux has no explicit licensing fees. While
system support is available for a fee from many Linux vendors, the OS itself is free to copy and use. Some IT
organizations have increased their savings by switching their server software from a commercial OS to
Linux.
Reliability. Linux is considered a reliable OS and is well-supported with security patches. Linux is also
considered to be stable, meaning it can run in most circumstances. Linux also copes with errors when
running software and unexpected input.
Backward compatibility. Linux and other open source software tend to be updated frequently for security
and functional patches, while retaining core functionality. Configurations and shell scripts are likely to work
unchanged even when software updates are applied. Unlike commercial software vendors that roll out new
versions of their OSes along with new ways to work, Linux and open source applications generally don't
change their modes of operation with new releases.
Many choices. Between the hundreds of available distributions, thousands of applications and almost
infinite options for configuring, compiling and running Linux on almost any hardware platform, it is possible
to optimize Linux for almost any application.
Lack of established standard. There is no standard version of Linux, which may be good for optimizing
Linux for particular applications, but less so for deploying standardized server or desktop images. The wide
range of options can complicate support as a result.
Support costs. While an organization can acquire Linux freely without licensing fees, support is not free.
Most enterprise Linux distributors like SUSE and Red Hat offer support contracts. Depending on the
circumstances, these license fees can reduce savings significantly.
Proprietary software. Desktop productivity software like Microsoft Office cannot be used on Linux
desktops, and other proprietary software may be unavailable for Linux platforms.
Unsupported hardware. While many hardware manufacturers make Linux device drivers available for their
products, many do not.
Steep learning curve. Many users struggle to learn to use the Linux desktop or Linux-based applications.
In some cases, the same Linux attribute can be either an advantage or disadvantage. For example, having
many options for customizing the Linux OS is advantageous for manufacturers looking for an embedded
OS, but it is a disadvantage for enterprises that want a desktop OS that can be used by a wide range of end
users.
History of Linux
Linus Torvalds started working on Linux as a replacement to the Minix OS while at the University of
Helsinki in Finland. Torvalds recognized the work done on the GNU Project in 1983, which intended to
create a complete, Unix-compatible OS comprised entirely of free software, and noted the GNU as a model
for distribution. However, the work on GNU had not been finished by the time Torvalds sought a Minix
replacement, prompting him to develop an alternate OS kernel dubbed Linux -- a contraction of Linus'
Unix -- and adopt the GNU GPL.
Torvalds released the Linux kernel in September 1991. A community of developers worked to integrate
GNU components with Torvalds' kernel to create a complete, free OS known collectively as Linux.
Torvalds continues to develop the Linux kernel and a vast developer community continues to create and
integrate a wide range of components.
While Linux still lags Windows and macOS on the desktop, it continues to challenge the proprietary OS
vendors on servers and embedded systems.
Kernel
• The kernel (orange box in the diagram) is the core part of the operating system.
• It manages:
• The kernel acts like a bridge between hardware and software. Programs don’t talk directly to the
hardware; they request services from the kernel.
User Space
This is where user-level programs run (blue boxes in the diagram). It sits on top of the kernel, meaning it
interacts with the kernel to get things done.
Terminal
• A terminal is basically a program (emulator) that provides a text-based interface for user
interaction.
• In the old days, the terminal was a physical device (a monitor + keyboard connected to a
mainframe).
• Today, it’s a software window (like GNOME Terminal, Konsole, or xterm) where you type
commands.
Shell
• The shell is the command-line interpreter.
• When you type a command in the terminal, the shell takes it, interprets it, and passes it to the
operating system/kernel.
• Examples: Bash, Zsh, Fish.
• In the diagram, the shell sits between the terminal and the command you want to run.
Command (‘tree’)
• 'tree' is just an example of a user program/command.
• The shell launches it, and the kernel ensures it runs on the hardware properly.
Console
• A console is a physical or virtual text input/output environment.
• In early computers: it meant the physical keyboard + screen directly connected to the machine.
• In modern Linux:
A console can be a virtual console (like pressing Ctrl+Alt+F1 to Ctrl+Alt+F6 to switch to different TTYs).
It’s a low-level environment where you interact with the system without a graphical interface.
Let’s look at the 60 most commonly used Linux commands and their examples for system administration.
Pro Tip
If you want to check a command’s usage, syntax, and options, use the –help flag. For example, enter ls –
help to display the ls utility guide.
1. ls command
The ls command lists the content of a folder, including files and directories. Here’s the syntax:
ls [options] [directory_or_path]
If you omit the path, the ls command will check the content of your current directory. To list items inside
subfolders, add the -R option. Meanwhile, use -a to show hidden content.
2. pwd command
To check the full path of your current working directory, use the pwd command. Its syntax is as follows:
pwd [options]
The pwd command has only two options. The -L option prints environment variable content, like shortcuts,
instead of the actual path of your current location. Meanwhile, -P outputs the exact location.
For example, /shortcut/folder is a shortcut for /actual/path, and you are currently in /actual/path/dir. If
you use the -L option, the output will be:
/shortcut/folder/dir
/actual/path/dir
3. cd command
Use cd to navigate between directories in your Linux VPS. It doesn’t have any option, and the syntax is
simple:
cd [path_or_directory]
Depending on your location, you might only need to specify the parent directory. For example, omit path
from path/to/directory if you are already inside one. The cd command has several shortcuts:
4. mkdir command
The mkdir command lets you create one or multiple directories. The syntax looks like this:
To create a folder in another location, specify the full path. Otherwise, this command will make the new
item in your current working directory.
mkdir path/to/target_folder/new_folder
By default, mkdir allows the current user to read, write, and execute files in the new folder. You can set
custom privileges during the creation by adding the -m option. To learn more about permission
management, read the chmod section below.
5. rmdir command
Run rmdir to delete empty directories in your Linux system. The command syntax looks like this:
rmdir [options] directory_name
The rmdir command won’t work if the directory contains subfolders. To force the deletion, add the –p
option. Note that you must own the item you want to remove or use sudo instead.
6. rm command
The rm command deletes files from a directory. You must have the write permission for the folder or use
sudo. Here’s the syntax:
You can add the -r option to remove a folder and its contents, including subdirectories. Use the -i flag to
display a confirmation message before the removal or -f to deactivate it completely.
Warning! Avoid using -r and -f unless necessary. Instead, add -i option to prevent accidental deletion.
7. cp command
Use the cp command to copy files from your current directory to another folder. The syntax looks like this:
You can also use cp to duplicate the content of one file to another using this syntax. If the target is in
another location, specify the full path like so:
cp source_file /path/to/target_file
Additionally, cp lets you duplicate a directory and its content to another folder using the -R option:
cp -R /path/to/folder /target/path/to/folder_copy
8. mv command
The main usage of the mv command is to move a file or folder to another location. Here’s the syntax:
mv file_or_directory [target_directory]
For example, we will move file1.txt from another location to the /new/file/directory path using this
command:
mv /original/path/file1.txt the/target/path
You can also use the mv command to rename files in your Linux system. Here’s an example:
mv old_name.txt new_name.txt
If you specify the full path, you can simultaneously rename files and move them to a new location like this
example:
mv old/location/of/old_name.txt new/path/for/new_name.txt
9. touch command
Run the touch command to create a new empty file in a specific directory. The syntax is as follows:
If you omit the path, the touch command will create a new file in your current working directory. Here’s
an example:
touch file.txt
The file command checks a file type, such as TXT, PDF, or other. The syntax is as follows:
file [file_name]
If you use this command on a symbolic link, it will output the actual file connected to the shortcut. You can
add the -k option to print more detailed information about the item.
The zip command compresses one or multiple files into a ZIP archive, reducing their size. Here’s the
syntax:
To extract a compressed file into your current working directory, use the unzip command like so:
The tar command bundles multiple files or directories into an archive without compression. The syntax
looks as follows:
To create a new TAR file, you must add the -c option. Then, use the -f flag to specify the archive’s name.
If you want to enable compression, add a specific option based on your preferred method. For example, the
following will bundle file1.txt and file2.txt with the gzip compression:
Remember that the archive’s file format will differ depending on the compression method. Regardless of
the extension, you can unpack a TAR file using this syntax:
nano, vi, and jed commands let you edit files. They have the same syntax, except at the beginning, where
you specify the name of the tool:
nano/vi/jed file_name
If the target file doesn’t exist, these commands will create a new one. Since your system might not have
these text processing utilities pre-installed, configure them using your package manager.
We will explain the command in the apt and dnf command section.
The concatenate or cat command has various usages. The most basic one is printing the content of a file.
Here’s the syntax:
cat file_name
To print the content in reverse order, use tac instead. If you add the standard output operator symbol (>),
the cat command will create a new file. For example, the following will make file.txt:
You can also use cat with the operator to combine the content of multiple files into a new item. In this
command, file1.txt and file2.txt will merge into target.txt:
Global regular expression print or grep lets you search specific lines from a file using keywords. It is
useful for filtering large data like logs. The syntax looks as follows:
You can also filter data from another utility by piping it to the grep command. For example, the following
searches file.txt from the ls command’s output:
ls | grep "file.txt"
Use the sed command to search and replace patterns in files quickly. The basic syntax looks like this:
You can replace a string in multiple files simultaneously by listing them. Here’s an example of a sed
command that changes red in colors.txt and hue.txt with blue:
Use the head command to print the first few entries of a file. The basic syntax is as follows:
You can also print the first few lines of another command’s output by piping it like so:
By default, head will show the first ten lines. However, you can change this setting using the -n option
followed by your desired number.
Meanwhile, use -c to print the first few entries based on the byte size instead of the line.
The tail command is the opposite of head, allowing you to print the last few lines from files or another
utility’s output. Here are the syntaxes:
The tail utility also has the same option as head. For example, we will extract the last five lines from the
ping command’s output:
The awk command searches and manipulates regular expression patterns in a file. Here’s the basic syntax:
Although similar to sed, awk offers more operations beyond substitution, including printing, mathematical
calculation, and deletion. It also lets you run a complex task with an if statement.
You can run multiple actions by listing them according to their execution order, separated by a semicolon
(;). For example, this awk command calculates the average student score and print names that are above
that threshold:
Ask Kodee, Hostinger’s AI assistant, to break down and explain complex commands.
Use the sort command to rearrange a file’s content in a specific order. Its syntax looks as follows:
By default, the sort command uses the alphabetical order from A to Z, but you can add the -r option to
reverse the order. You can also sort files numerically using the -n flag.
The cut command selects specific sections from a file and prints them as a Terminal output. The syntax
looks like this:
Unlike other Linux utilities, the cut command’s options are mandatory for file sectioning. Here are some of
the flags:
You can combine multiple options for a more specific output. For example, this command extracts the
third to fifth field from a comma-separated list:
The diff command compares two files and prints their differences. Here’s the syntax:
By default, the diff command only shows the differences between the two files. To print all the content and
highlight the discrepancies, enable the context format using the -c option. You can also ignore case
sensitivity by adding -i.
For example, run the following to show only the differences between 1.txt and 2.txt:
The tee command outputs another command’s results to both the Terminal and a file. It’s helpful if you
want to use the data for further processing or backups. Here’s the syntax:
If the specified file doesn’t exist, tee will create it. Be careful when using this command since it will
overwrite the existing content. To preserve and append existing data, add the -a option.
For example, we will save the ping command’s output as new entries in the test_network.txt file:
The locate command searches for a file and prints its location path. Here’s the syntax:
If you use the -r option to search files using regular expressions, omit the [keyword] argument. The locate
command is case-sensitive by default, but you can turn off this behavior using the -i flag.
Note that locate will look for files from its database. While this behavior speeds up the search process, you
must wait for the list to refresh before finding newly created items.
updatedb
The find command searches for a file within a specific directory. Here’s the syntax:
If you don’t specify the path, the find command will search your current working directory. To find files
using their name, add the -name option followed by the keyword.
You can specify the type of item you are looking for using the -type flag. The –type f option will search
files only, while -type d will find directories. For example, we will check file.txt in path/to/folder:
superuser do or sudo enables non-root users who are part of the sudo group to execute administrative
commands. Simply add it at the beginning of another utility like so:
For example, enter the following to open a file using nano as an administrator:
The Terminal will prompt you to enter the user’s password before executing the command. By default, you
must reenter it after five minutes of inactivity.
Typically, you don’t add any option to sudo, but you can check them by entering:
sudo --help
Warning! Since users with sudo privileges can change various settings of your system, use this command
with caution.
The su command lets you switch to another user in the Terminal session. The syntax looks as follows:
su [options] [username]
If you don’t specify any option or username, this command will switch you to the root user. In this case,
you must enter the password before changing the account.
You can check the currently logged-in user from the Linux command-line shell. Alternatively, use the
whoami command:
whoami
Chmod lets you change the permissions of files or directories. The basic syntax looks as follows:
The spot after the first hyphen (–) specifies the permission for the owner of file1.txt. In the previous
example, we grant them the rwx privilege.
The next spot is for groups. Since we won’t grant them any privilege, we put three hyphens to indicate
emptiness. The last slot is for other users who only have read or r permission.
The chown command lets you change the ownership of files, directories, or symbolic links. Here’s the
syntax:
If you want to assign a user as the new owner of an item, leave the group name empty. For example, we
will make admin-vps the owner of file1.txt:
Conversely, omit the username to make all group members the owner. Remember to write the colons (:)
like so:
Use the useradd command to create a new account in your Linux system. The syntax is as follows:
By default, the useradd command doesn’t prompt you to give the new user a password. You can add or
change it manually later with the passwd command:
passwd new_username
To remove a user, use the userdel command followed by the account name like the syntax in the example:
userdel new_username
Since managing other users requires a superuser privilege, run these commands as root or with the sudo
prefix.
Pro Tip
To set up a password and other details during the account creation process, use the adduser command
instead.
31. df command
The df command checks your Linux system’s disk usage, displaying the used space in percentage and
kilobyte (KB). The syntax looks like this:
Note that the df command operates at the file system level. If you don’t specify one, the utility will display
all the active file systems.
32. du command
To check the size of a directory and its content, use the du command. Here’s the syntax:
du [directory]
The command will check your working directory if you don’t specify a path or folder. By default, it breaks
down each subfolder’s disk usage, but you can add the -s option to summarize the total usage in one output.
You can also use the -M option to change the information from KB to MB.
The top command displays all running processes in your system and their hardware consumption. The
syntax looks like this:
top [options]
The top command has various options. For example, -p lets you check a specific process by specifying its
ID. Meanwhile, add the -d flag to change the delay between screen updates.
Like top, the htop command lets you display and manage processes in your Linux server. It also shares the
same syntax:
htop [options]
htop has options similar to top, but you can add additional ones. For example, -C enables the monochrome
mode, while –-tree shows processes in a hierarchical view.
35. ps command
The ps command summarizes the status of all running processes in your Linux system at a specific time.
Unlike top and htop, it doesn’t update the information automatically. Here’s the syntax:
ps [options]
You can print a more detailed report by adding other options. For example, use -A to list all processes in
your system, -r to check only the running ones, or -u username to query those associated with a particular
account.
The unix name or uname command displays detailed information about your Linux machine, including
hardware, name, and operating system kernel. Its basic syntax looks as follows:
uname [options]
Without any option, the command will print your system’s kernel name. To check all information about
your machine, add the -a option.
Use the hostname command to check your VPS hostname and other related information. Here is the
syntax:
hostname [options]
If you leave the option empty, the command will print your hostname. Add -i to check your server’s IP
address, -a to print the hostname alias, and -A to output the system’s fully qualified domain name (FQDN).
The time command measures the execution time of commands or scripts to gain insights into your system
performance. The basic syntax looks as follows:
time command_or_script
You can measure a series of commands by separating them using double ampersands (&&) or semicolons
(;) like so:
The systemctl command is used to manage services in your Linux system. Here’s the basic syntax:
systemctl subcommand [service_name][options]
The subcommands represent your task, like listing, restarting, terminating, or enabling the services. For
example, we will list Linux services using this:
Note that this command might not work with older distributions since they use another service manager.
The watch command lets you continuously run a utility at a specific interval to monitor changes in the
output. Here’s the basic syntax:
By default, watch will run your command every two seconds, but you can change the interval using the -n
option followed by the delay. If you want to highlight changes in the output, add the -d flag.
41. jobs command
Jobs are tasks or commands that are running in your current shell. To check them, use the jobs command
with the following syntax:
Running this command without any argument will show all jobs running in the Terminal’s foreground and
background. If you don’t have any ongoing tasks, it will return an empty output.
You can display more detailed information about each job by adding the -l option. Meanwhile, use -n to
show only tasks whose status has changed since the last notification.
Use the kill command to terminate a process using its ID. Here’s the basic syntax:
ps ux
The kill command has 64 termination signals. By default, it uses the SIGTERM method that lets the
program save its progress before closing.
The shutdown command lets you turn off or restart your Linux system at a specific time. Here’s the
syntax:
If you run the command without any arguments, your system will shut down immediately. You can specify
the schedule using a 24-hour format or a relative one. For example, enter +5 to shut down the system after
five minutes. To restart the machine, add the -r option.
The message argument specifies the notification other users in your system will receive before the server
shuts down.
The ping command sends packets to a target server and fetches the responses. It is helpful for network
diagnostics. The basic syntax looks like the following:
However, you can specify a custom number using the -c option. You can also change the interval between
transfers by adding -i.
For instance, let’s send 15 packets every two seconds to Google’s server:
ping -c 15 -i 2 google.com
The wget command lets you download files from the internet via HTTP, HTTPS, or FTP protocols. Here’s
the syntax:
By default, the wget command will download an item to your current working directory. For example, run
this command to retrieve the latest WordPress installer:
wget https://wordpress.org/latest.zip
Use the cURL command to transfer data from or to a server by specifying its URL. The basic syntax looks
as follows:
Running cURL without an option will print the website’s HTML content in your Terminal. If you add the -
O or -o option, the command will download files from the specified link.
The cURL command is also helpful for testing API or server endpoints. You can do so by adding the –X
option followed by an HTTP method, depending on whether you want to fetch or upload data.
For example, the following command will retrieve data from a specific API endpoint:
The scp command lets you securely copy files and directories between systems over a network. The syntax
looks as follows:
If you are copying items to or from your local machine, omit the IP and path. When transferring a file or
folder from a local machine, specify its name after options.
For example, we will run the following to copy file1.txt to our VPS’ path/to/folder directory as root:
You can change the default SCP port by specifying its number after the -P option. Meanwhile, use the -l
flag to limit the transfer bandwidth and add –C to enable compression.
The rsync command syncs files or folders between two destinations to ensure they have the same content.
The syntax looks as follows:
The source and destination can be a folder within the same system, a local machine, or a remote server. If
you are syncing content with a VPS, specify the username and IP address like so:
You can add the -a option to sync the file or folder’s attributes as well, including their symbolic links.
Meanwhile, use the -z flag to enable compression during the transfer.
49. ip command
The ip utility lets you list and manage your system’s network parameters, similar to the ifconfig command
in older Linux distros. Here’s the syntax:
Running this command without any argument will print the manual, including an explanation about
acceptable options and objects.
To manage a network parameter, specify the action in the command argument. For example, run this to
show your system’s IP address:
ip address show
Pro tip
You can also use Kodee to display network details on your VPS. Simply type a prompt like “Can you show
my server’s network details?”, and it will respond accordingly.
The netstat command displays information about your system’s network configuration. The syntax is
simple:
netstat [options]
Add an option to query specific network information. Here are several flags to use:
The traceroute command tracks a packet’s path when traveling between hosts, providing information like
the transfer time and involved routers. Here’s the syntax:
You can use a hostname, domain name, or IP address as the destination. If you don’t specify an option,
traceroute will run the test using the default settings.
Change the maximum packet hops using the -m option. To prevent traceroute from resolving IP addresses,
add -n.
You can also enable a timeout in seconds using the -w flag followed by the duration.
The nslookup command requests a domain name system (DNS) server to check a domain linked to an IP
address or vice versa. Here’s the syntax:
If you don’t specify a DNS server, nslookup will use your internet service provider’s default resolver. You
can add other options to change how this command queries an IP address or a domain.
For example, use the -type= option to specify the information you want to check, such as the DNS records.
You can also set up automatic retry with the -retry= flag and add -port= to use a specific port.
Since some Linux distros don’t have this utility pre-installed, you might encounter the “command not
found” error. You can configure it by downloading bind-utils or dnsutils via your package manager.
53. dig command
The domain information groper or dig command displays information about a domain. It is similar to
nslookup but more comprehensive. The syntax looks as follows:
Running dig without an argument will check A records of the specified domain using the operating
system’s default resolver. You can query a particular record by specifying it in the [type] argument like the
following example:
dig MX domain.com
To run a reverse DNS lookup, add the –x option and use an IP address as the target.
Run the history command to check previously run utilities. Here’s its syntax:
history [options]
Add the -r option if you want to clear the Terminal history. To rerun a specific utility from the list, enter an
exclamation mark followed by its ID.
!145
The man or manual command displays a comprehensive guide of another utility. The syntax looks like the
following:
If you specify only the command name, man will display the entire manual. Alternatively, you can select
one of the nine sections using their IDs to print more specific information.
For example, run the following to check the library call section of the ls command’s manual:
man 3 ls
56. echo command
Use echo to print text in your command as a Terminal output. Here’s the syntax:
You can also add the redirection symbol (>) to print the text in a file instead of Terminal. If you use two
symbols (>>), it will append the existing content. The command syntax looks like this:
If your text contains an environment or shell variable like $var, echo will display the actual value. This
command is commonly used for testing and bash scripting.
57. ln command
The ln command links files or directories with a shortcut. The syntax looks as follows:
This command will automatically create the shortcut, meaning you don’t need to make one manually. For
example, the following will enable you to open file.txt using shortcut.txt:
ln target.txt shortcut.txt
By default, ln creates a hard link, meaning changes in the source will be reflected in the linked item and
vice versa. To set up a soft or symbolic link, add the -s option.
The alias command lets you set another name for a string that belongs to a file, text, program, or command
name. Here’s the syntax:
alias name='string'
For example, the following will assign k as the alias for the kill command, allowing you to use the letter
instead of the full name.
alias k='kill'
To check a command’s alias, run alias followed by an alternative name. For example, we will check the
previous snippet:
alias k
You can remove an alias by running this syntax:
unalias [name]
The cal command displays a calendar in your Linux command-line interface. Here’s the syntax:
If you don’t add any argument, the command will show the current date. Alternatively, you can enter a
specific month and year in a numerical format.
You can also add the -3 option to show the current, previous, and next month.
The apt command lets you manage advanced package tool (APT) libraries in Debian-based operating
systems such as Ubuntu and Kali Linux. The syntax looks like this:
The subcommands define the action, like updating the library, upgrading software, installing an
application, or removing a package. For example, we will install the Vim text editor:
In Linux, package management commands differ across distributions. For example, Red Hat Enterprise
Linux-based distros like CentOS and AlmaLinux use dnf. It has the same syntax and options as apt.
Running both apt and dnf requires superuser privileges, which you can only obtain with sudo or via root.
Since we do not recommend you buy a Linux book here are some very good references and free access
online books - see resources.
If you need help on the meaning or syntax of any unix shell command you can use the manual (man) pages
or the web unix commands.
Goals
The shell
The file system
The shell
The shell is the Linux command line interpreter. It provides an interface between the user and the kernel
and executes programs called commands. For example, if a user enters ls then the shell executes the ls
command. The shell can also execute other programs such as applications, scripts, and user programs (e.g.,
written in c or the shell programming language).
You will get by in the course by becoming familiar with a subset of the Linux commands.
Linux has often been criticized for being very terse (it’s rumored that its designers were bad typists). Many
commands have short, cryptic names - vowels are a rarity:
awk, cat, cp, cd, chmod, grep, find, ls, mv, ps, rm
Linux command output is also very terse - the default action on success is silence. Only errors are reported.
Linux strongly supports the philosophy of one and only one way to perform each task. Linux commands
are often termed tools or utilities - familiarity with the “top 20” will be a great help.
Instructions entered in response to the shell prompt have the following syntax:
The brackets [] indicate that the arguments are optional. Many commands can be executed with or without
arguments. Others require arguments (e.g., cp sort.c anothersort.c) to work correctly, if not, they will
provide some error status message in return. But to avoid an explosion in the number of commands most
commands support switches (i.e., options) to modify the actions of the commands.
For example, lets use the ls command and the -l option switch to list in long format the file c.tex. Switches
are often single characters preceded by a hyphen (e.g., -l). Most commands accept switches in any order
though they must appear before all “true” arguments (usually filenames). In the case of the ls example
below the command arguments represent [options] filenames[s], as shown below. Options therefore modify
the operation of the command and are operated on by the program called by the shell and not the shell
itself.
In fact, the command is argument 0 (ls), -l switch or option is argument 1 and the filename is argument 2.
Some commands also accept their switches grouped together. For example, the following switches to ls are
identical:
The shell parses the words or tokens (commandname , options, filesnames[s]) and gets the kernel to
execute the commands assuming the syntax is good.
Typically, the shell processes the complete line after a carriage return (cr) (carriage return) is entered and
finds the program that the command line wants executing. The shell looks for the command to execute
either in the specified directory if given (./mycommnd) or it searches through a list of directories depending
on your $PATH variable.
Path environment
You will need to look at your $PATH variable and update it from time to time to make sure the path to the
code you want to execute is there. Typically, your login files that execute when you log in (.bash˙profile)
or each time to execute a command (.bashrc) are the place to set up these environment variables such as
$PATH.
echo $PATH
/sw/bin:/sw/sbin:/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/texbin:/sw/bin:/usr/X11R6/bin
So where does the ls command executed above reside in the Linux directory hierarchy. Let’s use another
command to find out.
The whereis or which commands are a useful sanity check if you want to know for sure which ls command
is executed. For example, I could have written a program called ls and placed it in my working directory -
probably not a good idea but could happen. In that case, if I entered ls - which command would the shell
execute?
So we can see from the $PATH variable that /bin is in the path. Hence the shell can track down the ls
binary to execute. The fact that the ls command is in /bin assumes that the filename /bin/ls has the correct
permission set to be an executable by all. We can check this.
[atc@Macintosh-7 atc]$ ls -l /bin/ls
-r-xr-xr-x 1 root wheel 60972 Oct 17 2006 /bin/ls
If I set my $PATH variable to “.” only the current working directory and execute ls (this would be akin to
not having my path name set up correctly) then the shell would not be able to find the correct program to
execute (assuming I don’t have an ls binary with the correct permissions in my current directory). Here is
what would happen.
You may come across this error (“-bash: ls: command not found” or variants thereof) when you install code
or try and execute new programs you want to use or have written.
There are a number of shells available to a Linux user - so which one do you select? These are:
We will be using the bash shell in this course since it is standard fare with Linux machines. Let’s check
what shell has been configured for your login account. If it’s not the bash shell then let’s change the shell
using the change shell chsh command. To find out what shell is running, log in and look at the $SHELL
environment variable. We will use the echo command which is akin to the print (e.g., printf in C) statement
in many languages.
The tcsh shell is set. So we need to change the shell to bash. Note, that galehead is the only machine that
you can change user configuration information on such as passwords and shells. So we ssh to galehead and
list all permissible shells supported by our local Linux machines using the “chsh -l” switch.
Now, let’s change the shell to bash. Note, that our first attempt fails because the shell wants the full path
name. We will discuss full and relative path names later in this lecture. Our second attempt using the full
path name from above is successful.
[campbell@galehead ~]$ ps
PID TTY TIME CMD
22271 pts/1 00:00:00 bash
22345 pts/1 00:00:00 ps
Another way to see what shell is running is to use the process status (ps) command. We can see that the
bash shell is running. The process ID and other status information of the process is displayed.
Note, that most commands executed by the shell starts a new “process”. (There is an exception to for what
are called builtins). We will discuss processes in a future lecture.
The basic shell operation is as follows. The shell parses the command line and finds the program to
execute. It passes any options and arguments to the program as part of a new process for the command such
as ps above. While the process is running ps above the shell waits for the process to complete. The shell is
in a sleep state. When the program completes it passes its exit status back to the shell. At this point the
shell wakes up and prompts the user for another command. Note, that it is the program that is executed, for
example, ps in this case, that checks to see if the arguments passed to it by the shell are correct or not. It is
not the job of the shell to do that level of parsing or error checking. If there are problems with the syntax
(e.g., wrong switch) then it is the program itself that informs the user via an error message.
The term command and utility are used synonymous in these notes. The shell has a number of utilities built
into the shell called builtins. When a builtin runs the shell does not fork a process; that is, it does not create
a process specifically to execute the command. Therefore, the builtins run more efficiently in the context of
the existing process rather than having the cost of creating new processes to run the command. Typically,
users are not aware if a command runs as a builtin or a standard forked command. The echo command
exists as a builtin to the shell and as a separate utility in /bin/echo. As a rule the shell will always execute a
builtin before trying to find a command of the same name to fork. Bash supports a number of builtins
including bg, fg, cd, kill, pwd, read among others.
The Linux file system is a hierarchical file system. The file system consists of a very small number of
different file types. These include text files, directories, character special files (e.g., terminals) and block
special files (e.g., disks and tapes).
A directory is just a special type of file. A directory (akin to a Macintosh folder) contains the names and
locations of all files and directories below it. A directory always contains two special files ’.’ (dot) and ’..’
(dot dot). Every file has a filename of up to 1024 characters typically from ’A-Z a-z 0-9 ˙ .’ and an inode
which uniquely identifies the file in the file system.
/usr/bin/emacs
/etc/passwd
Home directory
Each account has a home directory. After you have logged in your shell will be executing in your home
directory. So let’s log in and use the pwd command to find out where we are - we will be in the home
directory of course.
[atc@Macintosh-7 l2]$ ssh -Y -l campbell spruce.cs.dartmouth.edu
campbell@spruce.cs.dartmouth.edu’s password:
Last login: Mon Dec 24 11:37:01 2007 from c-75-69-130-98.hsd1.nh.comcast.net
[campbell@spruce ~]$ ls -l
total 434
drwx------ 2 campbell faculty 48 Dec 22 15:29 bin
drwxr--r-- 5 campbell faculty 128 Dec 24 14:33 cs23
drwx------ 2 campbell faculty 48 Dec 22 15:30 lib
drwx------ 3 campbell faculty 1368 Dec 24 11:25 mail
drwx------ 3 campbell faculty 104 Nov 6 12:01 papers
drwxr-xr-x 4 campbell ug 728 Oct 26 2006 public_html
-rw------- 1 campbell faculty 435438 Dec 14 2006 Sent
-rw------- 1 campbell faculty 1017 Mar 22 2007 Sent Messages
drwx------ 3 campbell faculty 72 Dec 11 15:14 teaching
Recall the “d” in “drwx——” indicates that this file is in fact a directory. So we can move to that directory
assuming we have the relevant permission - which we do in all cases. So lets move around.
All files and directories have certain access permissions which constrain access to only those users having
the correct permission. Let’s consider a couple of typical examples from above:
The first character of the access permissions indicate what “type of file” is being displayed.
Following the first type of file character the next 3 triples (i.e., groups of three characters) from left to right
represent file permissions: the read, write, and execute permissions, for (respectively) the owner (campbell
in this case), the files’s group (faculty in this case), and the “rest-of-the-world”. To determine that group
particular files are in enter the “ls -lg” command.
What do these permissions mean?
Read permission means that the file may be displayed on the screen, copied to another file or printed on
the printer - any operation that requires reading the contents of the file. Having read permission on a
directory means that its contents can be read; for example, ls could read the names and attributes of the
files in the directory. Could the contents of those files be read?
Write permission means that the file of directory may be modified, changed or overwritten. Most
importantly, write permission means that a file may be deleted. Write permission on a directory gives
permission to delete a file from within a directory.
Execute permission on a file indicates that the file may be executed (i.e., run). Since a directory cannot be
executed its meaning is redefined. Execute permission on a directory means that the user can change into
that directory (e.g., using cd) and examine files (e.g., using ls) that it has permission to read. If it does not
have permission to read then permission will be denied when using the ls command.
After the file permission comes the number of links to the file (e.g., 5), followed b the owner (campbell),
group (faculty), size (e.g., 128) which represents the size of the file in bytes, date and time of modification
(e.g., Dec 24 14:33), and the filename (e.g., cs50).
Note, that shellscripts (which we will discuss in a future lecture) must have both read and execute
permission - bash or any of the shells must both be able to read the shellscript and execute it. Program
binaries on the other hand do not need to be read and only need execution permission since they are not
read but executed (recall when we tried to more a.out we could not view it because it was an executable in
machine code).
Changing permission
The permissions on files and directories may be changed with the chmod (change mode) command. When
you own a file or directory you can use chmod to change the access permissions to the file or directory.
Only the three permission triplets may be changed - a directory cannot be changed into a plain file nor
vice-versa.
Each triplet is the sum of the octal digits 4, 2, 1, and read from left to right. For example rwx is represented
by 7, rw- by 6, and r– by 4 and so on. The absolute octal values used with chmod are as follows:
The complete permission on any file is the sum of the values. For example, home directories are typically
700 which provides the owner with read, write, and execute permission but denies all access to others.
If you wish others to read your files set - in this case a file funny - then the command would be:
Because the file is only to be read, not written too, and the fact that it’s a file with no execution (not a
binary or shellscript) 644 makes good sense.
Use the manual pages to read how chmod can be used in a relative or symbolic mode; for example, what
would
These symbolic arguments need to be used carefully. Here “o” means others and “u” means owner or user.
The change directory command (cd) allows us to move around the Linux directory hierarchy. Let’s
combine pwd, ls, and cd to move around the my local directories that are rooted at /net/nusers/campbell
[campbell@spruce cs50]$ ls
assignments code lectures
[campbell@spruce lectures]$ ls
bash-programming.tex design.tex se.tex start.tex
c.tex linux-advance.tex shell.tex
There are also a number of special characters that can be used with cd for short hand.
[campbell@spruce lectures]$ cd ..
[campbell@spruce cs50]$ cd -
/net/nusers/campbell/cs50/lectures
[campbell@spruce lectures]$ cd ~
and back:
[campbell@spruce ~]$ cd -
/net/nusers/campbell/cs50/lectures
Here are a popular set of switches you can use with ls:
We can use a number of special characters to look at the files in a smart and efficient manner:
[campbell@spruce lectures]$ ls -l s*
-rw-r--r-- 1 campbell faculty 0 Dec 24 12:23 se.tex
-rw-r--r-- 1 campbell faculty 0 Dec 24 12:21 shell.tex
-rw-r--r-- 1 campbell faculty 0 Dec 24 12:21 start.tex
[campbell@spruce lectures]$
If I wanted to list just the directories or just plain files (i.e., non directory files) in a directory how would I
do that? Use ls, right. Sorry, ls does not have an option to list only directories or just plain files. But we can
use a combination of commands to do this!
We can write our own commands to do this job - we can use a combination of ls and grep to list directory
names only or plain file names only.
First let’s just list all the files in the home directory - it includes one plain file and the rest are directories.
[campbell@bear ~]$ cd ~
[campbell@bear ~]$ ls -l
total 40
drwx------ 2 campbell faculty 4096 2007-12-22 15:29 bin
drwx------ 15 campbell faculty 4096 2010-01-03 21:16 cs50
drwx------ 5 campbell faculty 4096 2008-05-11 15:18 cs60
drwx------ 2 campbell faculty 4096 2007-12-22 15:30 lib
drwx------ 3 campbell faculty 4096 2009-12-29 08:12 mail
drwx------ 2 campbell faculty 4096 2009-06-23 02:43 Mail
drwx------ 3 campbell faculty 4096 2007-12-24 23:23 papers
-rw-r--r-- 1 campbell faculty 0 2010-01-04 21:43 plainfile
drwxr-xr-x 6 campbell ug 4096 2009-12-02 12:41 public_html
drwx------ 3 campbell faculty 4096 2007-12-11 15:14 teaching
drwxr-xr-x 2 campbell faculty 4096 2009-04-08 09:48 trash
Now we use a combination of ls and grep and the pipe command. More on this is in a later lecture but now
we begin to see the power of the shell.
If you don’t know any of the above swicthes then use the man command. We can also use the -F switch to
show which file is a directory or not. Check it out.
It is handy to be able to list just the directories when moving around the file system. So we’ll add these
commands to our bash files in the next lecture - we’ll create aliases of these commands so we can use them
any time.
We’ll there is a ls option to list directories and indeed there are many ways to do this; for example:
[campbell@bear ~]$ ls -d */
bin/ cs50/ cs60/ lib/ mail/ Mail/ papers/ public_html/ teaching/ trash/
In the following sequence we will create a new directory, create two new files (using touch), move one file
to another directory, delete the other file and remove the directory.
[campbell@spruce project]$ ls
socket.c transport.c
[campbell@spruce project]$ ls
[campbell@spruce project]$ cd ..
[campbell@spruce cs50]$ ls
assignments code lectures project
In the sequence above we reset the alias for rm which is set up in .bashrc. When you use the “rm -i” option
the shell will ask you to confirm if you really want to delete files. This is worth doing by setting up the
alias in your .bashrc file. It is easy to type “rm” and accidently delete files. Therefore, the “-i” (interactive)
option is a life saver. For example,
In the home directory there are a number of interesting “hidden” files. Using the “-a” lists all files
including those that begin with a dot (aka the hidden files).
[campbell@spruce ~]$ ls
bin cs50 lib mail papers public_html Sent Sent Messages teaching
UNIX/Linux
Linux Commands
Linux Basics
Sr Technical Writer
Introduction
Unlock the full potential of your Linux system with this comprehensive guide to essential Linux
commands. Whether you’re a seasoned administrator or just starting out, mastering these commands is
crucial for efficient server management, script writing, and troubleshooting. In this tutorial, you will learn
the most frequently used and powerful commands for file management, process control, user access,
network configuration, and system debugging.
You will learn over 50+ must-know Linux commands that will transform you into a Linux power user.
From basic to advanced, these commands will become your go-to tools for tackling any task that comes
your way.
Prerequisites
We will be running these commands on a Ubuntu server, but you can follow along on any modern Linux
distribution. You can set up a Ubuntu server for this tutorial by following our guide to Initial Server Setup
on Ubuntu.
Deploy your frontend applications from GitHub using DigitalOcean App Platform. Let DigitalOcean focus
on scaling your app.
tar Archive and compress files. tar -cvf archive.tar file1.txt file2.txt
Networking Commands
Command Description Sample Usage
ifconfig / ip
Display network interfaces. ifconfig or ip a
a
traceroute Trace the path packets take to a network host. traceroute example.com
cut Remove sections from lines of text. cut -d':' -f1 /etc/passwd
join Join lines of two files on a common field. join file1.txt file2.txt
Now let’s dive a little deeper into each of these commands and understand them in more detail. We already
have a lot of existing articles for each of those individual commands. For your convenience, we’ll add links
to all the existing articles, and continue to update the article as new topics are covered.
The ls command is used to list files and directories in the current working directory. This is going to be
one of the most frequently used Linux commands you must know of.
As you can see in the above image, using the command by itself without any arguments will give us an
output with all the files and directories in the directory. The command offers a lot of flexibility in terms of
displaying the data in the output.
The pwd command allows you to print the current working directory on your terminal. It’s a very basic
command and solves its purpose very well.
Now, your terminal prompt should usually have the complete directory anyway. But in case it doesn’t, this
can be a quick command to see the directory that you’re in. Another application of this command is when
creating scripts where this command can allow us to find the directory where the script has been saved.
While working within the terminal, moving around within directories is pretty much a necessity. The cd
command is one of the important Linux commands you must know, and it will help you navigate through
directories. Just type cd followed by directory, as shown below.
As you can see in the above command, I simply typed cd /etc/ to get into the /etc directory. We used the
pwd command to print the current working directory.
While working within the terminal, moving around within directories is pretty much a necessity. The cd
command is one of the important Linux commands you must know, and it will help you navigate through
directories. Just type cd followed by directory, as shown below.
root@ubuntu:~# cd <directory path>
As you can see in the above command, I simply typed cd /etc/ to get into the /etc directory. We used the
pwd command to print the current working directory.
The mkdir command allows you to create directories from within the terminal.
As you can see in the above screenshot, we created the JournalDev directory with just this simple
command.
The mkdir command allows you to create directories from within the terminal.
The cp and mv commands are equivalent to the copy-paste and cut-paste commands in Windows. But since
Linux doesn’t really have a command for renaming files, we also use the mv command to rename files and
folders.
In the above command, we created a copy of the file named Sample. Let’s see how what happens if we use
the mv command in the same manner.
The cp and mv commands are equivalent to the copy-paste and cut-paste commands in Windows. But since
Linux doesn’t really have a command for renaming files, we also use the mv command to rename files and
folders.
In the above command, we created a copy of the file named Sample. Let’s see how what happens if we use
the mv command in the same manner.
In the above case, since we were moving the file within the same directory, it acted as a rename. The file
name is now changed.
The cp and mv commands are equivalent to the copy-paste and cut-paste commands in Windows. But since
Linux doesn’t really have a command for renaming files, we also use the mv command to rename files and
folders.
In the above command, we created a copy of the file named Sample. Let’s see how what happens if we use
the mv command in the same manner.
In the above case, since we were moving the file within the same directory, it acted as a rename. The file
name is now changed.
In the previous section, we deleted the Sample-Copy file. The rm command is used to delete files and
folders and is one of the important Linux commands you must know.
The -r flag in the rm command in Linux stands for “recursive”. When used with the rm command, it will
remove not only the specified file but also all of its subdirectories and the files within those subdirectories
recursively.
Note: It’s important to be careful when using the rm command with the -r flag, as it can quickly and
permanently delete a large number of files and directories. It’s a good idea to use the -i flag in conjunction
with the -r flag, which will prompt you for confirmation before deleting each file and directory.
For example, to remove the mydir directory and its contents with confirmation, you can use this command:
This will prompt you for confirmation before deleting each file and directory within the mydir directory.
In the previous section, we deleted the Sample-Copy file. The rm command is used to delete files and
folders and is one of the important Linux commands you must know.
To delete a directory, you must add the -r argument to it. Without the -r argument, the rm command won’t
delete directories.
root@ubuntu:~# rm -r <folder/directory name>
The -r flag in the rm command in Linux stands for “recursive”. When used with the rm command, it will
remove not only the specified file but also all of its subdirectories and the files within those subdirectories
recursively.
Note: It’s important to be careful when using the rm command with the -r flag, as it can quickly and
permanently delete a large number of files and directories. It’s a good idea to use the -i flag in conjunction
with the -r flag, which will prompt you for confirmation before deleting each file and directory.
For example, to remove the mydir directory and its contents with confirmation, you can use this command:
This will prompt you for confirmation before deleting each file and directory within the mydir directory.
In the previous section, we deleted the Sample-Copy file. The rm command is used to delete files and
folders and is one of the important Linux commands you must know.
To delete a directory, you must add the -r argument to it. Without the -r argument, the rm command won’t
delete directories.
The -r flag in the rm command in Linux stands for “recursive”. When used with the rm command, it will
remove not only the specified file but also all of its subdirectories and the files within those subdirectories
recursively.
Note: It’s important to be careful when using the rm command with the -r flag, as it can quickly and
permanently delete a large number of files and directories. It’s a good idea to use the -i flag in conjunction
with the -r flag, which will prompt you for confirmation before deleting each file and directory.
For example, to remove the mydir directory and its contents with confirmation, you can use this command:
This will prompt you for confirmation before deleting each file and directory within the mydir directory.
In the previous section, we deleted the Sample-Copy file. The rm command is used to delete files and
folders and is one of the important Linux commands you must know.
To delete a directory, you must add the -r argument to it. Without the -r argument, the rm command won’t
delete directories.
The -r flag in the rm command in Linux stands for “recursive”. When used with the rm command, it will
remove not only the specified file but also all of its subdirectories and the files within those subdirectories
recursively.
Note: It’s important to be careful when using the rm command with the -r flag, as it can quickly and
permanently delete a large number of files and directories. It’s a good idea to use the -i flag in conjunction
with the -r flag, which will prompt you for confirmation before deleting each file and directory.
For example, to remove the mydir directory and its contents with confirmation, you can use this command:
This will prompt you for confirmation before deleting each file and directory within the mydir directory.
The touch command in Linux creates an empty file or updates the timestamp of an existing file.
The touch command in Linux creates an empty file or updates the timestamp of an existing file.
To create a link to another file, we use the ln command. This is one of the most important Linux
commands that you should know if you’re planning to work as a Linux administrator.
By default, the ln command will make hard links instead of symbolic or soft links.
Note: Say you have a text file. If you make a symbolic link to that file, the link is only a pointer to the
original file. If you delete the original file, the link will be broken, as it no longer has anything to point to.
A hard link is a mirror copy of an original file with the exact same contents. Like symbolic links, if you
edit the contents of the original file, those changes will be reflected in the hard link. If you delete the
original file, though, the hard link will still work, and you can view and edit it as you would a normal copy
of the original file.
To create a link to another file, we use the ln command. This is one of the most important Linux
commands that you should know if you’re planning to work as a Linux administrator.
By default, the ln command will make hard links instead of symbolic or soft links.
Note: Say you have a text file. If you make a symbolic link to that file, the link is only a pointer to the
original file. If you delete the original file, the link will be broken, as it no longer has anything to point to.
A hard link is a mirror copy of an original file with the exact same contents. Like symbolic links, if you
edit the contents of the original file, those changes will be reflected in the hard link. If you delete the
original file, though, the hard link will still work, and you can view and edit it as you would a normal copy
of the original file.
The clear command in Linux clears the terminal screen. It removes all the text and output currently
displayed on the terminal and gives you a clean slate to work with.
root@ubuntu:~# clear
This will clear the terminal screen and move the cursor to the top-left corner of the screen.
You can also use the clear command in combination with other commands, like this:
This will list the files and directories in the current directory, and then clear the terminal screen.
Note: The clear command does not delete any files or data from your system. It only affects the display of
the terminal.
The clear command in Linux clears the terminal screen. It removes all the text and output currently
displayed on the terminal and gives you a clean slate to work with.
root@ubuntu:~# clear
This will clear the terminal screen and move the cursor to the top-left corner of the screen.
You can also use the clear command in combination with other commands, like this:
This will list the files and directories in the current directory, and then clear the terminal screen.
Note: The clear command does not delete any files or data from your system. It only affects the display of
the terminal.
The clear command in Linux clears the terminal screen. It removes all the text and output currently
displayed on the terminal and gives you a clean slate to work with.
root@ubuntu:~# clear
This will clear the terminal screen and move the cursor to the top-left corner of the screen.
You can also use the clear command in combination with other commands, like this:
This will list the files and directories in the current directory, and then clear the terminal screen.
Note: The clear command does not delete any files or data from your system. It only affects the display of
the terminal.
The cat, echo, and less commands
When you want to output the contents of a file or print anything to the terminal output, we use the cat or
echo commands. Let’s see their basic usage.
As you can see in the above example, the cat command, when used on our New-File, prints the contents
of the file. At the same time, when we use echo command, it simply prints whatever follows after the
command.
The less command is used when the output printed by any command is larger than the screen space and
needs scrolling. The less command allows the user to break down the output and scroll through it with the
use of the enter or space keys.
The simple way to do this is with the use of the pipe operator (|).
Note: Use the -S flag with less to enable line wrapping. This will allow you to view long lines of text
without scrolling horizontally.
Use the -N flag with less to display line numbers. This can be useful when you need to know the line
number of a specific piece of text.
Using less with the pipe operator can be useful in many different situations. Here are a few examples:
When you want to output the contents of a file or print anything to the terminal output, we use the cat or
echo commands. Let’s see their basic usage.
As you can see in the above example, the cat command, when used on our New-File, prints the contents
of the file. At the same time, when we use echo command, it simply prints whatever follows after the
command.
The less command is used when the output printed by any command is larger than the screen space and
needs scrolling. The less command allows the user to break down the output and scroll through it with the
use of the enter or space keys.
The simple way to do this is with the use of the pipe operator (|).
Note: Use the -S flag with less to enable line wrapping. This will allow you to view long lines of text
without scrolling horizontally.
Use the -N flag with less to display line numbers. This can be useful when you need to know the line
number of a specific piece of text.
Using less with the pipe operator can be useful in many different situations. Here are a few examples:
When you want to output the contents of a file or print anything to the terminal output, we use the cat or
echo commands. Let’s see their basic usage.
As you can see in the above example, the cat command, when used on our New-File, prints the contents
of the file. At the same time, when we use echo command, it simply prints whatever follows after the
command.
The less command is used when the output printed by any command is larger than the screen space and
needs scrolling. The less command allows the user to break down the output and scroll through it with the
use of the enter or space keys.
The simple way to do this is with the use of the pipe operator (|).
Note: Use the -S flag with less to enable line wrapping. This will allow you to view long lines of text
without scrolling horizontally.
Use the -N flag with less to display line numbers. This can be useful when you need to know the line
number of a specific piece of text.
Using less with the pipe operator can be useful in many different situations. Here are a few examples:
When you want to output the contents of a file or print anything to the terminal output, we use the cat or
echo commands. Let’s see their basic usage.
As you can see in the above example, the cat command, when used on our New-File, prints the contents
of the file. At the same time, when we use echo command, it simply prints whatever follows after the
command.
The less command is used when the output printed by any command is larger than the screen space and
needs scrolling. The less command allows the user to break down the output and scroll through it with the
use of the enter or space keys.
The simple way to do this is with the use of the pipe operator (|).
Note: Use the -S flag with less to enable line wrapping. This will allow you to view long lines of text
without scrolling horizontally.
Use the -N flag with less to display line numbers. This can be useful when you need to know the line
number of a specific piece of text.
Using less with the pipe operator can be useful in many different situations. Here are a few examples:
The man command is a very useful Linux command one must know. When working with Linux, the
packages that we download can have a lot of functionality. Knowing it all is impossible.
The man command in Linux is used to display the manual page for a specific command. It provides detailed
information about the command, including its syntax, options, and examples.
1. Open a terminal and type man ls to display the manual page for the ls command.
root@ubuntu:~# man ls
Output
LS(1) User Commands LS(1)
NAME
ls - list directory contents
SYNOPSIS
ls [OPTION]... [FILE]...
DESCRIPTION
List information about the FILEs (the current directory by default).
Mandatory arguments to long options are mandatory for short options too.
-a, --all
do not ignore entries starting with .
-A, --almost-all
do not list implied . and ..
-c with -lt: sort by, and show, ctime (time of last modification
of file status information) with -l: show ctime and sort
by name;
...
The man command is a very useful Linux command one must know. When working with Linux, the
packages that we download can have a lot of functionality. Knowing it all is impossible.
The man command in Linux is used to display the manual page for a specific command. It provides detailed
information about the command, including its syntax, options, and examples.
1. Open a terminal and type man ls to display the manual page for the ls command.
root@ubuntu:~# man ls
Output
LS(1) User Commands LS(1)
NAME
ls - list directory contents
SYNOPSIS
ls [OPTION]... [FILE]...
DESCRIPTION
List information about the FILEs (the current directory by default).
Mandatory arguments to long options are mandatory for short options too.
-a, --all
do not ignore entries starting with .
-A, --almost-all
do not list implied . and ..
-c with -lt: sort by, and show, ctime (time of last modification
of file status information) with -l: show ctime and sort
by name;
...
The man command is a very useful Linux command one must know. When working with Linux, the
packages that we download can have a lot of functionality. Knowing it all is impossible.
The man command in Linux is used to display the manual page for a specific command. It provides detailed
information about the command, including its syntax, options, and examples.
1. Open a terminal and type man ls to display the manual page for the ls command.
root@ubuntu:~# man ls
Output
LS(1) User Commands LS(1)
NAME
ls - list directory contents
SYNOPSIS
ls [OPTION]... [FILE]...
DESCRIPTION
List information about the FILEs (the current directory by default).
Mandatory arguments to long options are mandatory for short options too.
-a, --all
do not ignore entries starting with .
-A, --almost-all
do not list implied . and ..
-c with -lt: sort by, and show, ctime (time of last modification
of file status information) with -l: show ctime and sort
by name;
...
The uname and whoami commands allow you to access some basic information that comes in handy when
you work on multiple systems.
The uname command in Linux displays information about the system’s kernel, including the kernel name,
hostname, kernel release, kernel version, and machine hardware name.
The whoami command in Linux returns the current user’s username. It stands for “who am I?” and it’s
often used to determine the current user’s identity in shell scripts or the terminal.
Let’s see the output of both the commands and the way we can use these.
root@ubuntu:~# uname -a
The parameter -a with uname command stands for “all”. This prints out the complete information. If the
parameter is not added, all you will get as the output is “Linux”.
Note: Some important flags you can use with the uname command.
The uname and whoami commands allow you to access some basic information that comes in handy when
you work on multiple systems.
The uname command in Linux displays information about the system’s kernel, including the kernel name,
hostname, kernel release, kernel version, and machine hardware name.
The whoami command in Linux returns the current user’s username. It stands for “who am I?” and it’s
often used to determine the current user’s identity in shell scripts or the terminal.
Let’s see the output of both the commands and the way we can use these.
root@ubuntu:~# uname -a
The parameter -a with uname command stands for “all”. This prints out the complete information. If the
parameter is not added, all you will get as the output is “Linux”.
Note: Some important flags you can use with the uname command.
The tar command in Linux is used to create and extract archived files. We can extract multiple different
archive files using the tar command.
To create an archive, we use the -c parameter, and to extract an archive, we use the -x parameter. Let’s see
how it works.
#Compress
root@ubuntu:~# tar -cvf <archive name> <files separated by space>
#Extract
root@ubuntu:~# tar -xvf <archive name>
In the first line, we created an archive named Compress.tar with the New-File and New-File-Link. In the
next command, we have extracted those files from the archive.
Let’s discuss the zip and unzip commands. Both are very straightforward. You can use them without any
parameters, and they’ll work as intended. Let’s see an example below.
The tar command in Linux is used to create and extract archived files. We can extract multiple different
archive files using the tar command.
To create an archive, we use the -c parameter, and to extract an archive, we use the -x parameter. Let’s see
how it works.
#Compress
root@ubuntu:~# tar -cvf <archive name> <files separated by space>
#Extract
root@ubuntu:~# tar -xvf <archive name>
In the first line, we created an archive named Compress.tar with the New-File and New-File-Link. In the
next command, we have extracted those files from the archive.
Let’s discuss the zip and unzip commands. Both are very straightforward. You can use them without any
parameters, and they’ll work as intended. Let’s see an example below.
root@ubuntu:~# zip <archive name> <file names separated by space>
root@ubuntu:~# unzip <archive name>
Since we already have those files in the same directory, the unzip command prompts us before overwriting
those files.
The tar command in Linux is used to create and extract archived files. We can extract multiple different
archive files using the tar command.
To create an archive, we use the -c parameter, and to extract an archive, we use the -x parameter. Let’s see
how it works.
#Compress
root@ubuntu:~# tar -cvf <archive name> <files separated by space>
#Extract
root@ubuntu:~# tar -xvf <archive name>
In the first line, we created an archive named Compress.tar with the New-File and New-File-Link. In the
next command, we have extracted those files from the archive.
Let’s discuss the zip and unzip commands. Both are very straightforward. You can use them without any
parameters, and they’ll work as intended. Let’s see an example below.
Since we already have those files in the same directory, the unzip command prompts us before overwriting
those files.
The grep command is a powerful and versatile text search tool in Linux and Unix-based operating systems.
It can search for specific patterns or strings in one or more files and filter the output of other commands.
The grep command stands for “global regular expression print,” which reflects its ability to search for
regular expressions across multiple lines and files.
This was a simple demonstration of the command. Learn more about the grep command.
The grep command is a powerful and versatile text search tool in Linux and Unix-based operating systems.
It can search for specific patterns or strings in one or more files and filter the output of other commands.
The grep command stands for “global regular expression print,” which reflects its ability to search for
regular expressions across multiple lines and files.
This was a simple demonstration of the command. Learn more about the grep command.
When outputting large files, the head and tail commands come in handy. These commands display the
beginning or end of a file, respectively. They are commonly used to quickly view the contents of a file
without having to open it in a text editor.
The head and tail commands display the first 10 lines of a file by default. To display a different number
of lines, you can use the -n option, followed by the number of lines you want to display.
The tail command outputted the bottom 10 lines from the file.
These commands can be used to quickly view a file’s contents, monitor real-time updates for
troubleshooting issues, filter output from other commands, and perform log analysis.
When outputting large files, the head and tail commands come in handy. These commands display the
beginning or end of a file, respectively. They are commonly used to quickly view the contents of a file
without having to open it in a text editor.
The head and tail commands display the first 10 lines of a file by default. To display a different number
of lines, you can use the -n option, followed by the number of lines you want to display.
As you can see, the head command showed 10 lines from the top of the file.
The tail command outputted the bottom 10 lines from the file.
These commands can be used to quickly view a file’s contents, monitor real-time updates for
troubleshooting issues, filter output from other commands, and perform log analysis.
The diff, comm, and cmp commands are all used to compare files in Linux and Unix-based operating
systems. These commands can be used to identify differences between two files, merge changes, and
perform other file comparison tasks.
As you can see above, we have added a small piece of text saying, “This line is edited” to the New-File-
Edited file.
The cmp command is used to compare two files and display the first byte that is different between them. It
can be used to identify differences between binary files or to check for corruption in files.
root@ubuntu:~# cmp <file 1> <file 2>
The cmp command only tells us the line number, which is different. Not the actual text.
The comm command is used to compare two sorted files and display the lines that are unique to each file, as
well as the lines that are common to both files.
The text that’s aligned to the left is only present in file 1. The centre-aligned text is present only in file
2. And the right-aligned text is present in both files.
By the looks of it, comm command makes the most sense when we’re trying to compare larger files and
would like to see everything arranged together.
All three of these commands are essential tools for working with files in Linux and Unix-based operating
systems. By understanding how to use the diff, comm, and cmp commands effectively, you can identify
differences between files, merge changes, and perform other file comparison tasks.
These commands can help you to identify and resolve issues with files, as well as to track changes and
maintain version control. Whether you’re a developer or a system administrator, these commands are an
essential part of your toolkit.
The diff, comm, and cmp commands are all used to compare files in Linux and Unix-based operating
systems. These commands can be used to identify differences between two files, merge changes, and
perform other file comparison tasks.
As you can see above, we have added a small piece of text saying, “This line is edited” to the New-File-
Edited file.
The cmp command is used to compare two files and display the first byte that is different between them. It
can be used to identify differences between binary files or to check for corruption in files.
The cmp command only tells us the line number, which is different. Not the actual text.
The comm command is used to compare two sorted files and display the lines that are unique to each file, as
well as the lines that are common to both files.
By the looks of it, comm command makes the most sense when we’re trying to compare larger files and
would like to see everything arranged together.
All three of these commands are essential tools for working with files in Linux and Unix-based operating
systems. By understanding how to use the diff, comm, and cmp commands effectively, you can identify
differences between files, merge changes, and perform other file comparison tasks.
These commands can help you to identify and resolve issues with files, as well as to track changes and
maintain version control. Whether you’re a developer or a system administrator, these commands are an
essential part of your toolkit.
The diff, comm, and cmp commands are all used to compare files in Linux and Unix-based operating
systems. These commands can be used to identify differences between two files, merge changes, and
perform other file comparison tasks.
The cmp command is used to compare two files and display the first byte that is different between them. It
can be used to identify differences between binary files or to check for corruption in files.
The cmp command only tells us the line number, which is different. Not the actual text.
The comm command is used to compare two sorted files and display the lines that are unique to each file, as
well as the lines that are common to both files.
The text that’s aligned to the left is only present in file 1. The centre-aligned text is present only in file
2. And the right-aligned text is present in both files.
By the looks of it, comm command makes the most sense when we’re trying to compare larger files and
would like to see everything arranged together.
All three of these commands are essential tools for working with files in Linux and Unix-based operating
systems. By understanding how to use the diff, comm, and cmp commands effectively, you can identify
differences between files, merge changes, and perform other file comparison tasks.
These commands can help you to identify and resolve issues with files, as well as to track changes and
maintain version control. Whether you’re a developer or a system administrator, these commands are an
essential part of your toolkit.
Jump back to commands list ↑
The diff, comm, and cmp commands are all used to compare files in Linux and Unix-based operating
systems. These commands can be used to identify differences between two files, merge changes, and
perform other file comparison tasks.
As you can see above, we have added a small piece of text saying, “This line is edited” to the New-File-
Edited file.
The cmp command is used to compare two files and display the first byte that is different between them. It
can be used to identify differences between binary files or to check for corruption in files.
The cmp command only tells us the line number, which is different. Not the actual text.
The comm command is used to compare two sorted files and display the lines that are unique to each file, as
well as the lines that are common to both files.
By the looks of it, comm command makes the most sense when we’re trying to compare larger files and
would like to see everything arranged together.
All three of these commands are essential tools for working with files in Linux and Unix-based operating
systems. By understanding how to use the diff, comm, and cmp commands effectively, you can identify
differences between files, merge changes, and perform other file comparison tasks.
These commands can help you to identify and resolve issues with files, as well as to track changes and
maintain version control. Whether you’re a developer or a system administrator, these commands are an
essential part of your toolkit.
The sort command is used to sort lines in a text file or standard input in Linux and Unix-based operating
systems. It can be used to sort lines in ascending or descending order and to perform other sorting
operations, such as sorting by fields or using a custom sorting order.
The above command will sort the lines in file.txt in numerical order.
The sort command can also be used to sort lines based on specific fields using the -k option.
This command will sort the lines in file.txt based on the second field.
The sort command is a powerful and flexible tool for working with text files in Linux and Unix-based
operating systems. By understanding how to use the sort command effectively, you can sort lines in text
files, sort lines based on specific fields, and perform other sorting operations.
These commands can help you organize and analyze data and perform other file manipulation tasks.
Whether you’re a developer or a system administrator, the sort command is an essential part of your
toolkit.
The sort command is used to sort lines in a text file or standard input in Linux and Unix-based operating
systems. It can be used to sort lines in ascending or descending order and to perform other sorting
operations, such as sorting by fields or using a custom sorting order.
The basic syntax of the sort command is:
By default, the sort command sorts lines in ASCII collating sequence, which can lead to unexpected
results when sorting numbers or special characters. To sort numbers in numerical order, you can use the -n
option.
The above command will sort the lines in file.txt in numerical order.
The sort command can also be used to sort lines based on specific fields using the -k option.
This command will sort the lines in file.txt based on the second field.
The sort command is a powerful and flexible tool for working with text files in Linux and Unix-based
operating systems. By understanding how to use the sort command effectively, you can sort lines in text
files, sort lines based on specific fields, and perform other sorting operations.
These commands can help you organize and analyze data and perform other file manipulation tasks.
Whether you’re a developer or a system administrator, the sort command is an essential part of your
toolkit.
The sort command is used to sort lines in a text file or standard input in Linux and Unix-based operating
systems. It can be used to sort lines in ascending or descending order and to perform other sorting
operations, such as sorting by fields or using a custom sorting order.
By default, the sort command sorts lines in ASCII collating sequence, which can lead to unexpected
results when sorting numbers or special characters. To sort numbers in numerical order, you can use the -n
option.
The above command will sort the lines in file.txt in numerical order.
The sort command can also be used to sort lines based on specific fields using the -k option.
This command will sort the lines in file.txt based on the second field.
The sort command is a powerful and flexible tool for working with text files in Linux and Unix-based
operating systems. By understanding how to use the sort command effectively, you can sort lines in text
files, sort lines based on specific fields, and perform other sorting operations.
These commands can help you organize and analyze data and perform other file manipulation tasks.
Whether you’re a developer or a system administrator, the sort command is an essential part of your
toolkit.
The sort command is used to sort lines in a text file or standard input in Linux and Unix-based operating
systems. It can be used to sort lines in ascending or descending order and to perform other sorting
operations, such as sorting by fields or using a custom sorting order.
By default, the sort command sorts lines in ASCII collating sequence, which can lead to unexpected
results when sorting numbers or special characters. To sort numbers in numerical order, you can use the -n
option.
The above command will sort the lines in file.txt in numerical order.
The sort command can also be used to sort lines based on specific fields using the -k option.
This command will sort the lines in file.txt based on the second field.
The sort command is a powerful and flexible tool for working with text files in Linux and Unix-based
operating systems. By understanding how to use the sort command effectively, you can sort lines in text
files, sort lines based on specific fields, and perform other sorting operations.
These commands can help you organize and analyze data and perform other file manipulation tasks.
Whether you’re a developer or a system administrator, the sort command is an essential part of your
toolkit.
The export command in Linux and Unix-based operating systems is used to set environment variables.
Environment variables are used to store information that can be used by processes or commands.
Once an environment variable is set, it can be accessed by any process or command that is running in the
same shell.
Environment variables can be used to store a wide range of information, such as configuration settings,
user preferences, or system information.
The export command in Linux and Unix-based operating systems is used to set environment variables.
Environment variables are used to store information that can be used by processes or commands.
Once an environment variable is set, it can be accessed by any process or command that is running in the
same shell.
Environment variables can be used to store a wide range of information, such as configuration settings,
user preferences, or system information.
The ssh command in Linux and Unix-based operating systems establishes a secure shell connection to a
remote server. The command provides a secure encrypted connection between the local and remote servers,
allowing users to run commands and transfer files securely.
This command establishes an ssh connection to the remote-server using the username account.
The ssh command supports a wide range of options and configurations, including:
The ssh command in Linux and Unix-based operating systems establishes a secure shell connection to a
remote server. The command provides a secure encrypted connection between the local and remote servers,
allowing users to run commands and transfer files securely.
This command establishes an ssh connection to the remote-server using the username account.
The ssh command supports a wide range of options and configurations, including:
The service command in Linux is used to manage system services, which are long-running processes that
are started at boot time and run in the background. These services are responsible for providing various
system functionalities, such as networking, database management, and user authentication.
The service command is used to start, stop, restart, and check the status of these services. It is a front-end
to the systemctl command, which is used to manage the systemd service manager.
As you can see in the image, the ssh server is running on our system.
The service command in Linux is used to manage system services, which are long-running processes that
are started at boot time and run in the background. These services are responsible for providing various
system functionalities, such as networking, database management, and user authentication.
The service command is used to start, stop, restart, and check the status of these services. It is a front-end
to the systemctl command, which is used to manage the systemd service manager.
As you can see in the image, the ssh server is running on our system.
The ps, kill, and killall commands are all used to manage processes in Linux.
The ps command is used to display information about the current running processes on the system. Here
are some examples of using the ps command:
root@ubuntu:~ ps -p PID
root@ubuntu:~ ps
root@ubuntu:~ kill <process ID>
root@ubuntu:~ killall <process name>
For demonstration purposes, we will create a shell script with an infinite loop and will run it in the
background.
With the use of the & symbol, we can pass a process into the background. As you can see, a new bash
process with PID 14490 is created.
Now, to kill a process with the kill command, you can type kill followed by the PID(Process Id) of the
process.
But if you do not know the process ID and just want to kill the process with the name, you can make use of
the killall command.
You will notice that PID 14490 stayed active. That is because, both times, we killed the sleep process.
The ps, kill, and killall commands are all used to manage processes in Linux.
The ps command is used to display information about the current running processes on the system. Here
are some examples of using the ps command:
root@ubuntu:~ ps -ef
Display a list of all processes for a specific process ID (PID):
root@ubuntu:~ ps -p PID
root@ubuntu:~ ps
root@ubuntu:~ kill <process ID>
root@ubuntu:~ killall <process name>
For demonstration purposes, we will create a shell script with an infinite loop and will run it in the
background.
With the use of the & symbol, we can pass a process into the background. As you can see, a new bash
process with PID 14490 is created.
Now, to kill a process with the kill command, you can type kill followed by the PID(Process Id) of the
process.
But if you do not know the process ID and just want to kill the process with the name, you can make use of
the killall command.
You will notice that PID 14490 stayed active. That is because, both times, we killed the sleep process.
The ps, kill, and killall commands are all used to manage processes in Linux.
The ps command is used to display information about the current running processes on the system. Here
are some examples of using the ps command:
root@ubuntu:~ ps -ef
Display a list of all processes for a specific process ID (PID):
root@ubuntu:~ ps -p PID
root@ubuntu:~ ps
root@ubuntu:~ kill <process ID>
root@ubuntu:~ killall <process name>
For demonstration purposes, we will create a shell script with an infinite loop and will run it in the
background.
With the use of the & symbol, we can pass a process into the background. As you can see, a new bash
process with PID 14490 is created.
Now, to kill a process with the kill command, you can type kill followed by the PID(Process Id) of the
process.
But if you do not know the process ID and just want to kill the process with the name, you can make use of
the killall command.
You will notice that PID 14490 stayed active. That is because, both times, we killed the sleep process.
The ps, kill, and killall commands are all used to manage processes in Linux.
The ps command is used to display information about the current running processes on the system. Here
are some examples of using the ps command:
root@ubuntu:~ ps -ef
Display a list of all processes for a specific process ID (PID):
root@ubuntu:~ ps -p PID
root@ubuntu:~ ps
root@ubuntu:~ kill <process ID>
root@ubuntu:~ killall <process name>
For demonstration purposes, we will create a shell script with an infinite loop and will run it in the
background.
With the use of the & symbol, we can pass a process into the background. As you can see, a new bash
process with PID 14490 is created.
Now, to kill a process with the kill command, you can type kill followed by the PID(Process Id) of the
process.
But if you do not know the process ID and just want to kill the process with the name, you can make use of
the killall command.
You will notice that PID 14490 stayed active. That is because, both times, we killed the sleep process.
When working with Linux, the df and mount commands are very efficient utilities to mount filesystems
and get details of the file system.
The df command is used to display the amount of disk space used and available on the file systems, and
the mount command is used to mount a file system or device to a specific directory.
When we say mount, it means that we’ll connect the device to a folder so we can access the files from our
filesystem. The default syntax to mount a filesystem is below:
root@ubuntu:~ mount /dev/cdrom /mnt
root@ubuntu:~ df -h
In the above case, /dev/cdrom is the device that needs to be mounted. Usually, a mountable device is
found inside the /dev folder. mnt is the destination folder to which to mount the device. You can change it
to any folder you want, but we have used /mnt as it’s the system’s default folder for mounting devices.
To see the mounted devices and get more information about them, we use the df command. Just typing df
will give us the data in bytes, which is not readable. So, we’ll use the -h parameter to make the data
human-readable.
When working with Linux, the df and mount commands are very efficient utilities to mount filesystems
and get details of the file system.
The df command is used to display the amount of disk space used and available on the file systems, and
the mount command is used to mount a file system or device to a specific directory.
When we say mount, it means that we’ll connect the device to a folder so we can access the files from our
filesystem. The default syntax to mount a filesystem is below:
In the above case, /dev/cdrom is the device that needs to be mounted. Usually, a mountable device is
found inside the /dev folder. mnt is the destination folder to which to mount the device. You can change it
to any folder you want, but we have used /mnt as it’s the system’s default folder for mounting devices.
To see the mounted devices and get more information about them, we use the df command. Just typing df
will give us the data in bytes, which is not readable. So, we’ll use the -h parameter to make the data
human-readable.
The chmod and chown commands are used to modify file permissions and ownership in Linux.
The chmod command is used to change the permissions of a file or directory, and the chown command is
used to change the ownership of a file or directory
The default syntax for both the commands is chmod <parameter> filename and chown <user:group>
filename
As you will notice, the root root part is now changed to www-data which is the new user who has full file
ownership.
Learn more about the Linux file Permissions and using the chmod command.
The chmod and chown commands are used to modify file permissions and ownership in Linux.
The chmod command is used to change the permissions of a file or directory, and the chown command is
used to change the ownership of a file or directory
The default syntax for both the commands is chmod <parameter> filename and chown <user:group>
filename
In the above example, we’re adding executable permissions to the loop.sh file with the chmod command.
In addition, with the chown command, we’ve made it accessible only to the root user and users within the
root group.
As you will notice, the root root part is now changed to www-data which is the new user who has full file
ownership.
Learn more about the Linux file Permissions and using the chmod command.
The ifconfig and traceroute commands manage network interfaces and trace the route of network
packets in Linux.
The ifconfig command will give you the list of all the network interfaces along with the IP addresses,
MAC addresses and other information about the interface.
root@ubuntu:~ ifconfig
There are multiple parameters that can be used, but we’ll work with the basic command here.
The traceroute command is used to trace the route of network packets and determine the path they take
to reach a specific destination.
When working with traceroute, you can simply specify the IP address, hostname, or domain name of the
endpoint.
root@ubuntu:~ traceroute <destination address>
Now, obviously, localhost is just one hop (the network interface itself). You can try this same command
with any other domain name or IP address to see all the routers your data packets pass through to reach the
destination.
The ifconfig and traceroute commands manage network interfaces and trace the route of network
packets in Linux.
The ifconfig command will give you the list of all the network interfaces along with the IP addresses,
MAC addresses and other information about the interface.
root@ubuntu:~ ifconfig
There are multiple parameters that can be used, but we’ll work with the basic command here.
The traceroute command is used to trace the route of network packets and determine the path they take
to reach a specific destination.
When working with traceroute, you can simply specify the IP address, hostname, or domain name of the
endpoint.
The ifconfig and traceroute commands manage network interfaces and trace the route of network
packets in Linux.
The ifconfig command will give you the list of all the network interfaces along with the IP addresses,
MAC addresses and other information about the interface.
root@ubuntu:~ ifconfig
There are multiple parameters that can be used, but we’ll work with the basic command here.
The traceroute command is used to trace the route of network packets and determine the path they take
to reach a specific destination.
When working with traceroute, you can simply specify the IP address, hostname, or domain name of the
endpoint.
If you want to download a file from within the terminal, the wget command is one of the handiest
command-line utilities available. It is one of the important Linux commands you should know when
working with source files.
When you specify the link for download, it has to directly be a link to the file. If the file cannot be accessed
by the wget command, it will simply download the webpage in HTML format instead of the actual file that
you wanted.
Or,
If you want to download a file from within the terminal, the wget command is one of the handiest
command-line utilities available. It is one of the important Linux commands you should know when
working with source files.
When you specify the link for download, it has to directly be a link to the file. If the file cannot be accessed
by the wget command, it will simply download the webpage in HTML format instead of the actual file that
you wanted.
Let’s try an example. The basic syntax of the wget command is :
Or,
If you want to download a file from within the terminal, the wget command is one of the handiest
command-line utilities available. It is one of the important Linux commands you should know when
working with source files.
When you specify the link for download, it has to directly be a link to the file. If the file cannot be accessed
by the wget command, it will simply download the webpage in HTML format instead of the actual file that
you wanted.
Or,
The ufw and iptables commands are used to manage firewalls in Linux.
UFW and IPTables are firewall interfaces for the Linux Kernel’s netfilter firewall. IPTables directly passes
firewall rules to Netfilter while UFW configures the rules in IPTables, which then sends those rules to
Netfilter.
Why do we need UFW when we have IPTables? Because IPTables is pretty difficult for a newbie. UFW
makes things extremely easy. See the below example where we are trying to allow port 80 for our
webserver.
root@ubuntu:~# iptables -A INPUT -p tcp -m tcp --dport 80 -j ACCEPT
root@ubuntu:~# ufw allow 80
I’m sure you now know why UFW was created! Look at how easy the syntax becomes. Both these
firewalls are very comprehensive and can allow you to create any kind of configuration required for your
network. Learn at least the basics of UFW or IPTables firewall, as these are the Linux commands you must
know.
Learn more about managing firewall with ufw and managing firewall with iptables.
The ufw and iptables commands are used to manage firewalls in Linux.
UFW and IPTables are firewall interfaces for the Linux Kernel’s netfilter firewall. IPTables directly passes
firewall rules to Netfilter while UFW configures the rules in IPTables, which then sends those rules to
Netfilter.
Why do we need UFW when we have IPTables? Because IPTables is pretty difficult for a newbie. UFW
makes things extremely easy. See the below example where we are trying to allow port 80 for our
webserver.
I’m sure you now know why UFW was created! Look at how easy the syntax becomes. Both these
firewalls are very comprehensive and can allow you to create any kind of configuration required for your
network. Learn at least the basics of UFW or IPTables firewall, as these are the Linux commands you must
know.
Learn more about managing firewall with ufw and managing firewall with iptables.
Different Linux distributions use different package managers. Since we’re working on a Ubuntu server, we
have the apt package manager. But for someone working on a Fedora, Red Hat, Arch, or Centos machine,
the package manager will be different.
Below are the commands on how to use these package managers on different Linux distributions.
Getting yourself well versed with the package manager of your distribution will make things much easier
for you in the long run. So even if you have a GUI based package management tool installed, try an make
use of the CLI based tool before you move on to the GUI utility. Add these to your list of Linux commands
you must know.
This is the quote displayed when a sudo-enabled user(sudoer) first uses the sudo command to escalate
privileges. This command is equivalent to logging in as root (based on what permissions you have as a
sudoer).
Just add the command sudo before any command that you need to run with escalated privileges, and that’s
it. It’s very simple to use, but can also be an added security risk if a malicious user gains access to a sudoer.
Learn more about how to create a new sudo user and how to edit sudoers file.
This is the quote displayed when a sudo-enabled user(sudoer) first uses the sudo command to escalate
privileges. This command is equivalent to logging in as root (based on what permissions you have as a
sudoer).
Just add the command sudo before any command that you need to run with escalated privileges, and that’s
it. It’s very simple to use, but can also be an added security risk if a malicious user gains access to a sudoer.
Learn more about how to create a new sudo user and how to edit sudoers file.
Have you ever wanted to view the calendar in the terminal? There apparently are people who want it to
happen, and well, here it is.
The cal command displays a well-presented calendar on the terminal. Just enter the command cal on your
terminal prompt.
root@ubuntu:~# cal
root@ubuntu:~# cal March 2024
Although we don’t need it often, it’s a great addition! It’s an amazing option for terminal fans.
Have you ever wanted to view the calendar in the terminal? There apparently are people who want it to
happen, and well, here it is.
The cal command displays a well-presented calendar on the terminal. Just enter the command cal on your
terminal prompt.
root@ubuntu:~# cal
root@ubuntu:~# cal March 2024
Although we don’t need it often, it’s a great addition! It’s an amazing option for terminal fans.
Do you have some commands that you run very frequently while using the terminal? It could be rm -r or
ls -l, or it could be something longer like tar -xvzf.
If you know a command that you run very often, it’s time to create an alias.
What’s an alias? In simple terms, it’s another name for a command that you’ve defined.
root@ubuntu:~# alias lsl="ls -l"
OR
root@ubuntu:~# alias rmd="rm -r"
Now, every time you enter lsl or rmd in the terminal, you’ll receive the output that you’d have received if
you had used the full commands.
The examples here are for really small commands that you can still type by hand every time. But in some
situations where a command has too many arguments that you need to type, it’s best to create a shorthand
version of the same.
Do you have some commands that you run very frequently while using the terminal? It could be rm -r or
ls -l, or it could be something longer like tar -xvzf.
If you know a command that you run very often, it’s time to create an alias.
What’s an alias? In simple terms, it’s another name for a command that you’ve defined.
Now, every time you enter lsl or rmd in the terminal, you’ll receive the output that you’d have received if
you had used the full commands.
The examples here are for really small commands that you can still type by hand every time. But in some
situations where a command has too many arguments that you need to type, it’s best to create a shorthand
version of the same.
This command was created to convert and copy files from multiple file system formats. In the current day,
the command is simply used to create bootable USB for Linux but there still are some things important you
can do with the command.
The dd command in Linux is a versatile command used for low-level copying and conversion of data. It
stands for “data-description” or “data definition,” and it can be used to copy and convert data between
different file formats and storage devices.
For example, if we wanted to back up the entire hard drive as is to another drive, we would use the dd
command.
The if and of arguments stand for input file and output file.
It’s a powerful and flexible tool, but it can also be dangerous if not used carefully. Always double-check
your syntax and make sure you know what the command will do before executing it.
This command was created to convert and copy files from multiple file system formats. In the current day,
the command is simply used to create bootable USB for Linux but there still are some things important you
can do with the command.
The dd command in Linux is a versatile command used for low-level copying and conversion of data. It
stands for “data-description” or “data definition,” and it can be used to copy and convert data between
different file formats and storage devices.
For example, if we wanted to back up the entire hard drive as is to another drive, we would use the dd
command.
The if and of arguments stand for input file and output file.
It’s a powerful and flexible tool, but it can also be dangerous if not used carefully. Always double-check
your syntax and make sure you know what the command will do before executing it.
The whereis and whatis commands are used in Linux to search for information about programs and files.
The whereis command locates the binary, source, and manual pages for a specific command or program,
and the whatis command displays a short description of a command or program.
The whereis and whatis commands are used in Linux to search for information about programs and files.
The whereis command locates the binary, source, and manual pages for a specific command or program,
and the whatis command displays a short description of a command or program.
The whereis and whatis commands are used in Linux to search for information about programs and files.
The whereis command locates the binary, source, and manual pages for a specific command or program,
and the whatis command displays a short description of a command or program.
A few sections earlier, we talked about the ps command. You observed that the ps command will output
the active processes and end itself.
The top command is like a CLI version of the task manager in Windows.
The top command in Linux is a system monitoring tool that displays real-time information about system
processes and resource usage. It provides a dynamic, real-time view of system activity, including CPU
usage, memory usage, and process information.
This will sort the process list by memory usage, with the most memory-intensive processes at the top.
root@ubuntu:~# top -n 1
It’s a powerful and flexible tool for monitoring system activity and troubleshooting performance issues.
Jump back to commands list ↑
This will sort the process list by memory usage, with the most memory-intensive processes at the top.
root@ubuntu:~# top -n 1
A few sections earlier, we talked about the ps command. You observed that the ps command will output
the active processes and end itself.
The top command is like a CLI version of the task manager in Windows.
The top command in Linux is a system monitoring tool that displays real-time information about system
processes and resource usage. It provides a dynamic, real-time view of system activity, including CPU
usage, memory usage, and process information.
This will sort the process list by memory usage, with the most memory-intensive processes at the top.
root@ubuntu:~# top -n 1
It’s a powerful and flexible tool for monitoring system activity and troubleshooting performance issues.
The useradd and usermod commands are used in Linux to manage user accounts.
The useradd or adduser commands are the exact same commands where adduser is just a symbolic link
to the useradd command. This command allows us to create a new user in Linux.
The above command will create a new user named JournalDev with the home directory as /home/JD.
The usermod command, on the other hand, is used to modify existing users. You can modify any value of
the user including the groups, the permissions, etc.
For example, if you want to add more groups to the user, you can type in:
The useradd and usermod commands are used in Linux to manage user accounts.
The useradd or adduser commands are the exact same commands where adduser is just a symbolic link
to the useradd command. This command allows us to create a new user in Linux.
The above command will create a new user named JournalDev with the home directory as /home/JD.
The usermod command, on the other hand, is used to modify existing users. You can modify any value of
the user including the groups, the permissions, etc.
For example, if you want to add more groups to the user, you can type in:
The useradd and usermod commands are used in Linux to manage user accounts.
The useradd or adduser commands are the exact same commands where adduser is just a symbolic link
to the useradd command. This command allows us to create a new user in Linux.
The above command will create a new user named JournalDev with the home directory as /home/JD.
The usermod command, on the other hand, is used to modify existing users. You can modify any value of
the user including the groups, the permissions, etc.
For example, if you want to add more groups to the user, you can type in:
Now that you know how to create new users, let’s also set the password for them. The passwd command
lets you set the password for your own account, or if you have the permissions, set the password for other
accounts.
Replace username with the name of the user whose password you want to change.
Replace days with the number of days before the password expires and warm days with the number of
days before the password expires that the user will be warned.
These are just a few examples of using the passwd command in Linux. By understanding how to use this
command effectively, you can manage user accounts and ensure that your system is secure.
Now that you know how to create new users, let’s also set the password for them. The passwd command
lets you set the password for your own account, or if you have the permissions, set the password for other
accounts.
This will prompt you to enter a new password for the current user.
Replace username with the name of the user whose password you want to change.
Force a user to change their password at the next login:
root@ubuntu:~# passwd -f username
Replace days with the number of days before the password expires and warm days with the number of
days before the password expires that the user will be warned.
These are just a few examples of using the passwd command in Linux. By understanding how to use this
command effectively, you can manage user accounts and ensure that your system is secure.
Now that you know how to create new users, let’s also set the password for them. The passwd command
lets you set the password for your own account, or if you have the permissions, set the password for other
accounts.
This will prompt you to enter a new password for the current user.
Replace username with the name of the user whose password you want to change.
These are just a few examples of using the passwd command in Linux. By understanding how to use this
command effectively, you can manage user accounts and ensure that your system is secure.
Now that you know how to create new users, let’s also set the password for them. The passwd command
lets you set the password for your own account, or if you have the permissions, set the password for other
accounts.
This will prompt you to enter a new password for the current user.
Replace username with the name of the user whose password you want to change.
Replace days with the number of days before the password expires and warm days with the number of
days before the password expires that the user will be warned.
These are just a few examples of using the passwd command in Linux. By understanding how to use this
command effectively, you can manage user accounts and ensure that your system is secure.
When working with Linux commands, you may encounter various errors and issues. Here are some
common problems and how to resolve them:
If you receive a command not found error, it means that the command you are trying to run is not
recognized by the system. This can happen for several reasons:
1. The command is not installed on your system. You can install it using your package manager (e.g.,
apt, yum, dnf).
If you see:
Use:
which xyz
2. The command is not in your system’s PATH. You can add the directory containing the command to
your PATH.
3. export PATH=$PATH:/path/to/command
4. You made a typo. Double-check the command for any spelling errors.
2. Resolving permission issues with sudo
If you encounter permission issues, you can use sudo to run the command with superuser privileges. For
example:
sudo command_name
File conflicts can occur when multiple users or processes attempt to modify the same file simultaneously.
Here are some ways to handle file conflicts:
1. Use Version Control Systems (VCS): Tools like Git can help manage file conflicts by allowing
users to merge changes and resolve conflicts manually.
2. git merge <branch_name>
If there are conflicts, Git will prompt you to resolve them. Open the conflicting files, make the
necessary changes, and then commit the resolved files.
3. Locking Mechanisms: Implement file locking to prevent multiple processes from writing to the
same file simultaneously. Use flock in Linux to create a lock on a file.
4. flock -x <file> -c "<command>"
5. Atomic Operations: Use atomic operations to ensure that file writes are completed in a single step,
reducing the risk of conflicts.
6. mv temp_file target_file
mv -f file1 file2
cp -i file1 file2
Performance bottlenecks can significantly impact the efficiency of your system. Here are some steps to
debug and resolve them:
1. Identify the Bottleneck: Use tools like top, htop, vmstat, and iostat to monitor system
performance and identify the resource causing the bottleneck (CPU, memory, disk I/O, etc.).
2. top
3. Analyze Logs: Check system and application logs for any errors or warnings that might indicate
performance issues.
4. tail -f /var/log/syslog
5. Optimize Code: Review and optimize your code to improve performance. Look for inefficient
algorithms, unnecessary computations, and memory leaks.
6. Profile Your Application: Use profiling tools like gprof, perf, or valgrind to analyze your
application’s performance and identify slow functions or memory issues.
7. gprof <executable> gmon.out
8. Scale Resources: If the bottleneck is due to resource limitations, consider scaling up your hardware
or using load balancing to distribute the load across multiple servers.
Use:
which xyz
The command is not in your system’s PATH. You can add the directory containing the command to
your PATH.
export PATH=$PATH:/path/to/command
When working with Linux commands, you may encounter various errors and issues. Here are some
common problems and how to resolve them:
If you receive a command not found error, it means that the command you are trying to run is not
recognized by the system. This can happen for several reasons:
1. The command is not installed on your system. You can install it using your package manager (e.g.,
apt, yum, dnf).
If you see:
Use:
which xyz
4. You made a typo. Double-check the command for any spelling errors.
If you encounter permission issues, you can use sudo to run the command with superuser privileges. For
example:
sudo command_name
File conflicts can occur when multiple users or processes attempt to modify the same file simultaneously.
Here are some ways to handle file conflicts:
1. Use Version Control Systems (VCS): Tools like Git can help manage file conflicts by allowing
users to merge changes and resolve conflicts manually.
2. git merge <branch_name>
If there are conflicts, Git will prompt you to resolve them. Open the conflicting files, make the
necessary changes, and then commit the resolved files.
3. Locking Mechanisms: Implement file locking to prevent multiple processes from writing to the
same file simultaneously. Use flock in Linux to create a lock on a file.
4. flock -x <file> -c "<command>"
5. Atomic Operations: Use atomic operations to ensure that file writes are completed in a single step,
reducing the risk of conflicts.
6. mv temp_file target_file
mv -f file1 file2
cp -i file1 file2
Performance bottlenecks can significantly impact the efficiency of your system. Here are some steps to
debug and resolve them:
1. Identify the Bottleneck: Use tools like top, htop, vmstat, and iostat to monitor system
performance and identify the resource causing the bottleneck (CPU, memory, disk I/O, etc.).
2. top
3. Analyze Logs: Check system and application logs for any errors or warnings that might indicate
performance issues.
4. tail -f /var/log/syslog
5. Optimize Code: Review and optimize your code to improve performance. Look for inefficient
algorithms, unnecessary computations, and memory leaks.
6. Profile Your Application: Use profiling tools like gprof, perf, or valgrind to analyze your
application’s performance and identify slow functions or memory issues.
7. gprof <executable> gmon.out
8. Scale Resources: If the bottleneck is due to resource limitations, consider scaling up your hardware
or using load balancing to distribute the load across multiple servers.
When working with Linux commands, you may encounter various errors and issues. Here are some
common problems and how to resolve them:
If you receive a command not found error, it means that the command you are trying to run is not
recognized by the system. This can happen for several reasons:
1. The command is not installed on your system. You can install it using your package manager (e.g.,
apt, yum, dnf).
If you see:
Use:
which xyz
2. The command is not in your system’s PATH. You can add the directory containing the command to
your PATH.
3. export PATH=$PATH:/path/to/command
4. You made a typo. Double-check the command for any spelling errors.
2. Resolving permission issues with sudo
If you encounter permission issues, you can use sudo to run the command with superuser privileges. For
example:
sudo command_name
File conflicts can occur when multiple users or processes attempt to modify the same file simultaneously.
Here are some ways to handle file conflicts:
1. Use Version Control Systems (VCS): Tools like Git can help manage file conflicts by allowing
users to merge changes and resolve conflicts manually.
2. git merge <branch_name>
If there are conflicts, Git will prompt you to resolve them. Open the conflicting files, make the
necessary changes, and then commit the resolved files.
3. Locking Mechanisms: Implement file locking to prevent multiple processes from writing to the
same file simultaneously. Use flock in Linux to create a lock on a file.
4. flock -x <file> -c "<command>"
5. Atomic Operations: Use atomic operations to ensure that file writes are completed in a single step,
reducing the risk of conflicts.
6. mv temp_file target_file
mv -f file1 file2
cp -i file1 file2
Performance bottlenecks can significantly impact the efficiency of your system. Here are some steps to
debug and resolve them:
1. Identify the Bottleneck: Use tools like top, htop, vmstat, and iostat to monitor system
performance and identify the resource causing the bottleneck (CPU, memory, disk I/O, etc.).
2. top
3. Analyze Logs: Check system and application logs for any errors or warnings that might indicate
performance issues.
4. tail -f /var/log/syslog
5. Optimize Code: Review and optimize your code to improve performance. Look for inefficient
algorithms, unnecessary computations, and memory leaks.
6. Profile Your Application: Use profiling tools like gprof, perf, or valgrind to analyze your
application’s performance and identify slow functions or memory issues.
7. gprof <executable> gmon.out
8. Scale Resources: If the bottleneck is due to resource limitations, consider scaling up your hardware
or using load balancing to distribute the load across multiple servers.
If there are conflicts, Git will prompt you to resolve them. Open the conflicting files, make the necessary
changes, and then commit the resolved files.
Locking Mechanisms: Implement file locking to prevent multiple processes from writing to the same
file simultaneously. Use flock in Linux to create a lock on a file.
Atomic Operations: Use atomic operations to ensure that file writes are completed in a single step,
reducing the risk of conflicts.
mv temp_file target_file
mv -f file1 file2
cp -i file1 file2
When working with Linux commands, you may encounter various errors and issues. Here are some
common problems and how to resolve them:
If you receive a command not found error, it means that the command you are trying to run is not
recognized by the system. This can happen for several reasons:
1. The command is not installed on your system. You can install it using your package manager (e.g.,
apt, yum, dnf).
If you see:
which xyz
2. The command is not in your system’s PATH. You can add the directory containing the command to
your PATH.
3. export PATH=$PATH:/path/to/command
4. You made a typo. Double-check the command for any spelling errors.
If you encounter permission issues, you can use sudo to run the command with superuser privileges. For
example:
sudo command_name
File conflicts can occur when multiple users or processes attempt to modify the same file simultaneously.
Here are some ways to handle file conflicts:
1. Use Version Control Systems (VCS): Tools like Git can help manage file conflicts by allowing
users to merge changes and resolve conflicts manually.
2. git merge <branch_name>
If there are conflicts, Git will prompt you to resolve them. Open the conflicting files, make the
necessary changes, and then commit the resolved files.
3. Locking Mechanisms: Implement file locking to prevent multiple processes from writing to the
same file simultaneously. Use flock in Linux to create a lock on a file.
4. flock -x <file> -c "<command>"
5. Atomic Operations: Use atomic operations to ensure that file writes are completed in a single step,
reducing the risk of conflicts.
6. mv temp_file target_file
mv -f file1 file2
cp -i file1 file2
4. Debugging Performance Bottlenecks
Performance bottlenecks can significantly impact the efficiency of your system. Here are some steps to
debug and resolve them:
1. Identify the Bottleneck: Use tools like top, htop, vmstat, and iostat to monitor system
performance and identify the resource causing the bottleneck (CPU, memory, disk I/O, etc.).
2. top
3. Analyze Logs: Check system and application logs for any errors or warnings that might indicate
performance issues.
4. tail -f /var/log/syslog
5. Optimize Code: Review and optimize your code to improve performance. Look for inefficient
algorithms, unnecessary computations, and memory leaks.
6. Profile Your Application: Use profiling tools like gprof, perf, or valgrind to analyze your
application’s performance and identify slow functions or memory issues.
7. gprof <executable> gmon.out
8. Scale Resources: If the bottleneck is due to resource limitations, consider scaling up your hardware
or using load balancing to distribute the load across multiple servers.
Analyze Logs: Check system and application logs for any errors or warnings that might indicate
performance issues.
tail -f /var/log/syslog
Optimize Code: Review and optimize your code to improve performance. Look for inefficient
algorithms, unnecessary computations, and memory leaks.
Profile Your Application: Use profiling tools like gprof, perf, or valgrind to analyze your
application’s performance and identify slow functions or memory issues.
When working with Linux commands, you may encounter various errors and issues. Here are some
common problems and how to resolve them:
If you receive a command not found error, it means that the command you are trying to run is not
recognized by the system. This can happen for several reasons:
1. The command is not installed on your system. You can install it using your package manager (e.g.,
apt, yum, dnf).
If you see:
Use:
which xyz
2. The command is not in your system’s PATH. You can add the directory containing the command to
your PATH.
3. export PATH=$PATH:/path/to/command
4. You made a typo. Double-check the command for any spelling errors.
If you encounter permission issues, you can use sudo to run the command with superuser privileges. For
example:
sudo command_name
File conflicts can occur when multiple users or processes attempt to modify the same file simultaneously.
Here are some ways to handle file conflicts:
1. Use Version Control Systems (VCS): Tools like Git can help manage file conflicts by allowing
users to merge changes and resolve conflicts manually.
2. git merge <branch_name>
If there are conflicts, Git will prompt you to resolve them. Open the conflicting files, make the
necessary changes, and then commit the resolved files.
3. Locking Mechanisms: Implement file locking to prevent multiple processes from writing to the
same file simultaneously. Use flock in Linux to create a lock on a file.
4. flock -x <file> -c "<command>"
5. Atomic Operations: Use atomic operations to ensure that file writes are completed in a single step,
reducing the risk of conflicts.
6. mv temp_file target_file
Or, Use mv with -f (force) or cp with -i (interactive):
mv -f file1 file2
cp -i file1 file2
Performance bottlenecks can significantly impact the efficiency of your system. Here are some steps to
debug and resolve them:
1. Identify the Bottleneck: Use tools like top, htop, vmstat, and iostat to monitor system
performance and identify the resource causing the bottleneck (CPU, memory, disk I/O, etc.).
2. top
3. Analyze Logs: Check system and application logs for any errors or warnings that might indicate
performance issues.
4. tail -f /var/log/syslog
5. Optimize Code: Review and optimize your code to improve performance. Look for inefficient
algorithms, unnecessary computations, and memory leaks.
6. Profile Your Application: Use profiling tools like gprof, perf, or valgrind to analyze your
application’s performance and identify slow functions or memory issues.
7. gprof <executable> gmon.out
8. Scale Resources: If the bottleneck is due to resource limitations, consider scaling up your hardware
or using load balancing to distribute the load across multiple servers.
FAQs
1. What are the most used Linux commands?
The most used Linux commands include cd, ls, mkdir, rm, cp, mv, echo, cat, grep, find, man, sudo, apt-
get, ssh, ping, df, du, free, top, ps, kill, killall, service, systemctl, reboot, shutdown, whoami,
uname, uptime, history, and clear.
You can list all available commands in Linux by using the compgen -c command. This will display a list
of all commands that are available on your system.
You can find a file in Linux using the find command. The basic syntax is find <path> -name
"<filename>", where <path> is the directory where you want to start searching, and <filename> is the
name of the file you’re looking for. For example, find /home/user -name "example.txt".
4. How do I kill a process in Linux?
You can kill a process in Linux using the kill command. First, you need to find the process ID (PID) of
the process you want to kill using the ps or top command. Then, use the kill command followed by the
PID. For example, kill 1234, where 1234 is the PID of the process.
The cp command is used to copy files or directories, while the mv command is used to move or rename
files or directories. cp creates a duplicate of the original file, leaving the original intact, whereas mv moves
the file to a new location, removing it from the original location.
You can check your Linux system’s memory usage using the free command. This command displays the
total amount of free and used physical and swap memory in the system. For example, free -h will display
the memory usage in a human-readable format.
Linux is a powerful and versatile operating system that powers everything from servers to smartphones. If
you’re a student new to Linux, understanding basic commands is the first step toward becoming proficient
in using this OS. In this blog, we’ll explore 50 essential Linux commands with simple explanations and
examples to help you get started.
1. pwd
$ pwd
/home/student
2. ls
$ ls
$ cd Downloads
4. mkdir
$ mkdir projects
5. rmdir
$ rmdir projects
6. touch
$ touch file.txt
7. rm
$ rm file.txt
$ rm -r folder_name
8. cp
$ cp file1.txt file2.txt
$ cp -r dir1 dir2
9. mv
$ mv file.txt new_name.txt
$ mv file.txt /home/student/Documents
10. cat
11. nano
$ nano file.txt
12. vi
$ vi file.txt
13. echo
14. clear
$ clear
15. history
$ history
16. find
17. grep
18. chmod
19. chown
20. man
$ man ls
21. uname
$ uname -a
22. df
$ df -h
23. du
$ du -sh folder_name
24. top
$ top
25. ps
$ ps
26. kill
$ kill 1234
27. ping
$ ping google.com
28. curl
$ curl https://example.com
29. wget
$ wget https://example.com/file.zip
30. zip
31. unzip
$ unzip archive.zip
32. tar
33. df
$ df -h
34. free
$ free -h
35. hostname
$ hostname
36. ifconfig
37. ssh
38. scp
39. passwd
$ passwd
40. whoami
$ whoami
41. uptime
System Uptime: Shows how long the system has been running.
$ uptime
42. shutdown
$ shutdown -h now
43. reboot
$ reboot
44. alias
45. unalias
$ unalias ll
46. wc
47. head
$ head file.txt
48. tail
$ tail file.txt
49. sort
$ sort file.txt
50. uniq
$ uniq file.txt
Antivirus is cybersecurity software that detects and removes malicious software (malware) from a
computer, device, or network. If your network has end users, antivirus software is essential in the
prevention of data breaches. Read on to learn more about how antivirus works, the risks of malware and
ransomware, and more.
About antivirus software
Also known as anti-malware, antivirus software protects devices and systems from various types of
harmful software, typically delivered via email. Antivirus aims to detect viruses, worms, trojan horses,
spyware, adware, and ransomware. The primary purpose of antivirus software is to protect computers and
data from being compromised or damaged by these malicious programs.
Virus Detection: Antivirus programs use various methods to identify known and emerging threats. This
includes signature-based detection, which involves matching the patterns or signatures of known malware,
and heuristic-based detection, which looks for behaviors or characteristics common to malware.
Malware Prevention: Antivirus software aims to prevent malware from infecting a device or system. This
may involve real-time scanning of files and programs, monitoring network traffic, and blocking suspicious
activities.
Destruction/Removal of Viruses: Antivirus software takes action to remove or quarantine the malicious
files if malware is detected. This process helps prevent further damage and protects the system and data.
Monitoring and Scanning: Antivirus programs often provide options for on-demand or scheduled scans of
the entire system, specific drives, or individual files and folders. Scans help identify and eliminate potential
threats that may have infiltrated the system.
Antivirus software detects, prevents, and removes malicious software, commonly known as malware, from
a computer or network. Here's a general overview of how antivirus software works:
New and sophisticated malware can sometimes evade detection, which is why it's essential to practice safe
computing habits, such as keeping software up to date, being cautious of email attachments and links, and
using strong, unique passwords.
Traditional antivirus software is deployed on a computer and keeps an eye out for signatures to protect
against known attacks. The software must be installed on-site, and it can take weeks or months to deploy.
Next-generation antivirus software, on the other hand, uses artificial intelligence (AI) and other
technologies to stop known and unknown attacks. The software identifies suspicious behaviors and threats,
even if they have not been previously used in cyberattacks. It can be deployed in hours and doesn't require
any updates.
The number one risk of forgoing antivirus software is that you open the door for malware to infect your
user devices and systems. Malware attacks pose significant risks to individuals, organizations, and even
nations.
Here are some of the key risks associated with viruses and malware attacks:
Data Loss or Theft: Malware can be designed to steal sensitive information such as personal details,
financial information, intellectual property, or login credentials. This can be used for identity theft, financial
fraud, or sold on the dark web.
Financial Loss: Malware can cause financial damage by disrupting business operations, leading to
downtime and loss of revenue. Ransomware can encrypt files or systems, demanding payment for their
release.
System Disruption and Downtime: Malware can disrupt normal system operations, leading to downtime
and loss of productivity. Disruption is particularly damaging for businesses that rely heavily on their IT
infrastructure.
Reputation Damage: A successful malware attack can tarnish an individual's or organization's reputation.
Customers and clients may lose trust if they perceive that their data is not secure.
Intellectual Property Theft: Malware can target and exfiltrate intellectual property (IP), trade secrets, and
other proprietary information. This can have long-term consequences for businesses that rely on
innovation and unique offerings.
Compromised Security: Some malware is designed to disable or bypass security measures, leaving systems
vulnerable to further attacks. This can lead to a perpetual cycle of security breaches.
Network Compromise: Malware can spread across networks, infecting multiple systems and devices. This
can result in a broader and more severe impact on an organization's infrastructure.
Botnet Formation: Malware can be used to create botnets, a type of cyber attack that involves controlling
computers controlled by a central server. These botnets can be used for various malicious activities, such as
distributed denial-of-service (DDoS) attacks.
Legal Risk: Organizations may face legal consequences for failing to protect sensitive data or for being the
source of a malware attack. Data protection laws and data privacy regulations impose strict requirements
on how personal and sensitive information is handled.
To mitigate these risks, individuals and organizations should implement robust cybersecurity measures,
including regularly updating software, using antivirus and anti-malware solutions, educating users about
phishing and social engineering tactics, and regularly backing up important data.
Choosing the right antivirus software is crucial for ensuring the security of your computer and data. Here
are a few factors to consider when selecting an antivirus program:
Detection Effectiveness Rate: Look for antivirus software that has a high detection rate for malware,
viruses, and other threats. Check independent test results from organizations like AV-Test or AV-
Comparatives for performance evaluations.
Modern Security Features: Consider the features offered by the antivirus software. Common features
include real-time scanning, firewall protection, email scanning, and automatic updates. Some may also
include additional features like password managers, VPNs, or parental controls.
Overall Network System Impact: Evaluate the impact of the antivirus software on your system's
performance. Some antivirus programs can be resource-intensive and slow down your computer. Look for a
balance between effective protection and minimal impact on system performance.
Ease of Use: Choose antivirus software that is user-friendly and easy to navigate. A complicated interface
can be frustrating and may lead to errors or missed security settings.
Compatibility With Business Systems and Applications: Your antivirus software should be compatible with
your operating system and other business applications. Some antivirus programs are designed specifically
for Windows, while others also support macOS, Linux, or mobile platforms.
Cost: Antivirus software comes in both free and paid versions. While free options can provide basic
protection, paid versions often offer more advanced features and better customer support. Consider your
budget and the level of protection you need.
Customer Support: Check the level of customer support your vendor provides. In case you encounter issues
or have questions, responsive customer support can be valuable.
Reputation: Research the reputation of the antivirus software and its vendor. Read reviews, both from
experts and users, to gain insights into the software's performance, reliability, and customer satisfaction.
Frequent Updates: Regular updates are crucial for keeping your antivirus software effective against
evolving threats. Ensure that the software receives frequent updates to its virus definitions and program
features.
Trial Versions: Many antivirus programs offer free trial versions. Take advantage of the trial offers to test
the software's features, performance, and compatibility with your system before making a final decision.
Remember, no antivirus software can provide 100% security, so it's also important to practice proper
cybersecurity hygiene, such as regularly updating your operating system and software, being cautious with
email attachments, and avoiding suspicious websites.
Yes, antivirus solutions are often available from managed security service providers (MSSPs). MSSPs
offer a range of security solutions and services, and antivirus protection is commonly included in their
offerings. These services are designed to help businesses enhance their cybersecurity posture without the
need for in-house expertise in managing and maintaining security solutions.
MSSPs can provide antivirus software, deploy and manage it across an organization's network, ensure that
the software is up to date, and monitor for any security threats. This model is particularly beneficial for
businesses that prefer to outsource their IT management and security functions, allowing them to focus on
their core activities while relying on experts to handle their IT infrastructure and security.
When considering an MSSP for antivirus services, it's essential to assess their capabilities, experience, and
the specific features of the antivirus solution they provide. Additionally, businesses should ensure that the
MSSP follows best practices for security and regulatory compliance to protect sensitive data and maintain a
secure environment.
Sophos Intercept X antivirus software delivers high-impact server and container security for on-premises,
data center, and cloud. Sophos Intercept X with Extended Detection and Response (XDR) has been rated
the #1 XDR solution by G2 users in their spring 2023 reports (March 2023).
Benefits include:
Cloud Native- Advanced Windows and Linux protection and visibility across your cloud, on-premises, and
virtual server workloads.
Next-Gen Protection– Deep learning AI, anti-ransomware capabilities, and exploit prevention techniques
give you world-class protection.
Optimized for Performance– Deploy as a lightweight agent or via API for Linux to integrate with your
security operations, IT, and DevOps processes.
Sign up for a free trial of Intercept X today. If you already have an active Sophos Central account, you can
start your trial from the Sophos Central Admin Console. Log in to Sophos Central, click Free Trials, and
select Intercept X Advanced for Server with XDR.
Antivirus software is a specialized program designed to detect, prevent, and remove malicious software,
commonly known as malware. It acts as a gatekeeper for your digital devices, monitoring data and
blocking potential threats from causing harm. Its primary function is to safeguard computers and mobile
devices from malware such as viruses, worms, trojans, ransomware, and spyware.
Antivirus software operates by scanning your device regularly, looking for known threats and new,
emerging malware strains. When a threat is detected, the software either blocks it or assists in its removal,
thereby protecting your device from potential harm. Apart from providing real-time protection, antivirus
software also helps in:
Detecting, preventing, and removing malware and viruses: This is the primary function of any antivirus
software. It scans your devices for potential threats and takes necessary actions to neutralize them.
Blocking intrusive pop-ups: Pop-up advertisements can be a source of malware. Antivirus software helps in
blocking these intrusive pop-ups, ensuring a safer browsing experience.
Scanning in real time: Real-time scanning helps in protecting your devices as you browse the internet, open
emails, and download files.
Protecting external devices: Antivirus software also scans external devices like hard drives and USB drives,
preventing potential threats from infiltrating your system.
Blocking scam sites: By restricting access to harmful websites, antivirus software helps in preventing
phishing attempts and scams.
How Does Antivirus Software Work?
Antivirus software uses multiple detection methods to identify and block malicious programs. The key
methods include:
Signature Detection: This method involves scanning files for known malicious code or ‘signatures’. The
limitation of this method is that it can only detect previously identified threats.
Heuristic Detection: This method involves looking for suspicious behavior or code structures in files. It’s
designed to catch new, unknown threats by identifying general attributes of malware.
Behavior-based Detection: This detection technique examines the behavior of programs and files to
identify suspicious activities. This method is useful in detecting unknown malware that behaves in a way
similar to known threats.
Cloud-based and Sandbox Analysis: Some antivirus software uses cloud-based or sandbox analysis to test
suspicious files in a controlled environment before they can harm your system.
Intrusion Prevention (HIPS): Host Intrusion Prevention Systems (HIPS) monitor activities within a system
and alert users to any potentially harmful actions.
Antivirus software comes in various forms, each offering different levels of protection. Some of the
common types include:
Standalone Antivirus Scanners: These are basic antivirus programs that provide protection against known
threats.
Internet Security Suites: These are comprehensive packages that offer antivirus protection along with
additional security features like firewalls, privacy controls, and more.
Cloud-based Antivirus Programs: These programs use cloud technology to analyze suspicious files in a
remote server, reducing the load on the user’s device.
Machine Learning Antivirus Programs: These use artificial intelligence and machine learning to identify new
threats.
When selecting an antivirus program, it’s essential to consider the following key features:
Real-time Scanning: The software should be capable of scanning your system in real-time to detect and
block threats as they occur.
Automatic Updates: An effective antivirus program should regularly update its virus definitions to protect
against the latest threats.
Heuristic Scanning: This feature helps in detecting unknown threats that have not been defined in the virus
database yet.
Sandboxing: This feature allows the antivirus to run potentially harmful programs in a separate, secured
environment, preventing them from causing damage to the system.
Email Protection: The antivirus should scan emails and attachments for any potential threats.
Protection Against Cyber Threats: Antivirus software provides an essential layer of protection against
various cyber threats like viruses, ransomware, spyware, and more.
Safe Browsing: By blocking harmful websites and downloads, antivirus software ensures a safer browsing
experience.
Identity Protection: Some antivirus programs offer features that help protect your personal information
from being stolen.
Improved PC Performance: By eliminating malware that can slow down your PC, antivirus software helps in
improving your system’s performance.
What is its Effectiveness Against Various Threats?
While antivirus software plays a crucial role in protecting your devices, its effectiveness can vary based on
the type and sophistication of the threat. Most modern antivirus solutions can effectively deal with common
threats like viruses, spyware, and ransomware. However, new and evolving threats may sometimes slip
through if the software is not updated regularly.
While both antivirus and firewall software provide essential security, they serve different purposes.
Antivirus software is designed to detect, block, and remove malware from your devices, while a firewall
protects your devices by controlling network traffic and blocking unauthorized access to your system.
Choosing the best antivirus software depends on your specific needs. However, some key features to look
for include:
Real-time Protection: The software should provide continuous protection against threats.
Automatic Updates: It should regularly update its virus definitions to protect against the latest threats.
Comprehensive Scanning: The software should be capable of performing in-depth scans of your system
and external devices.
User-Friendly Interface: The software should be easy to use, with intuitive controls and settings.
Customer Support: The vendor should provide reliable customer support in case of any issues with the
software.
In conclusion, antivirus software plays a critical role in protecting your digital devices and data from
various cyber threats. It’s crucial to choose an antivirus program that fits your needs and keeps your
systems safe and secure. By understanding how antivirus software works and the features it offers, you can
make an informed decision and ensure optimal digital security.
Anti-virus software is a program designed to detect, prevent, and remove malicious software (malware)
from computers and networks. It plays a crucial role in protecting systems from viruses, worms, trojans,
ransomware, spyware, adware, and other types of malwares. Antivirus programs use various detection
methods and algorithms to identify and neutralize threats.
From Nortan Antivirus
Step-by-Step Process:
1. Signature-Based Detection:
o Definition: This is the most common method. The antivirus software uses a database of known
malware signatures (unique strings of data or code patterns) to identify malware.
o Process: When a file is scanned, the antivirus software compares its contents against the database
of known signatures. If a match is found, the file is flagged as malicious.
o Update Mechanism: The antivirus database is regularly updated with new signatures to protect
against the latest threats.
2. Heuristic Analysis:
o Definition: This method involves analysing the behaviour or structure of a file to determine if it is
malicious, even if it does not match any known signatures.
o Process: The software examines code for suspicious behaviour or patterns that are typical of
malware, such as attempts to modify system files or registry entries.
3. Behavioural Analysis:
o Definition: This method monitors the behaviour of programs in real-time to detect any suspicious
activity.
o Process: If a program exhibits behaviour characteristic of malware (e.g., attempting to encrypt
large numbers of files or making unauthorized network connections), the antivirus software can
block the activity and quarantine the program.
4. Sandboxing:
o Definition: This involves running suspicious files in a controlled, isolated environment to observe
their behaviour without risking harm to the actual system.
o Process: The antivirus software executes the file in the sandbox and monitors its actions. If the file
behaves maliciously, it is flagged and quarantined.
5. Real-Time Scanning:
o Definition: Also known as on-access scanning, this feature continuously monitors the system for
threats, scanning files as they are accessed or modified.
o Process: The antivirus software scans files when they are opened, executed, or saved, providing
real-time protection against malware.
6. Full System Scans:
o Definition: This involves scanning all files and programs on the system for malware.
o Process: The antivirus software performs a comprehensive scan of the entire system, including the
operating system, installed applications, and user files.
1. Performance Impact:
o Anti-virus software can consume significant system resources, leading to slowdowns, especially
during full system scans or when running on older hardware.
2. False Positives:
o Legitimate files or programs can sometimes be mistakenly identified as malware, leading to false
positives. This can cause disruptions if important files are quarantined or deleted.
3. Incomplete Protection:
o Anti-virus software may not detect all threats, especially new or sophisticated malware that uses
advanced evasion techniques. It is not a foolproof solution and should be part of a broader security
strategy.
4. Cost:
o While there are free antivirus solutions available, many offer limited features. Comprehensive
protection often requires purchasing a subscription, which can be costly.
5. User Dependency:
o The effectiveness of antivirus software depends on user behaviour. If users disable real-time
protection, ignore warnings, or fail to update the software regularly, the level of protection
diminishes.
6. Potential Conflicts:
o Running multiple antivirus programs simultaneously can cause conflicts and system instability. It is
generally recommended to use only one primary antivirus solution.
Scenario: A user wants to protect their computer from malware using Anti-virus software.
1. Installation:
o The user downloads and installs a reputable antivirus program, such as Norton, McAfee,
Bitdefender, or Kaspersky.
2. Initial Scan:
o After installation, the antivirus software performs an initial full system scan to detect and remove
any existing threats.
3. Real-Time Protection:
o The Anti-virus software enables real-time scanning, which continuously monitors the system for
any suspicious activity or files.
4. Regular Updates:
o The software is configured to automatically update its virus definitions and detection algorithms to
stay protected against new threats.
5. Scheduled Scans:
o The user schedules regular scans (e.g., weekly) to ensure ongoing protection and detection of any
new threats.
6. Quarantine and Removal:
o If the Anti-virus software detects a threat, it quarantines the affected file and notifies the user. The
user can then review the threat and choose to delete or restore the file.
Summary
Anti-virus software is a critical component of computer and network security, designed to detect, prevent,
and remove malware. It employs various detection methods, including signature-based detection, heuristic
analysis, behavioural analysis, and sandboxing, to identify and neutralize threats. The advantages of
antivirus software include protection against malware, real-time threat detection, regular updates,
quarantine and removal capabilities, improved system performance, and user-friendly interfaces. However,
there are also disadvantages, such as potential performance impact, false positives, incomplete protection,
cost, user dependency, and potential conflicts with other security programs.