Introduction to CLI & Basic Commands
1. Importance of Shell scripting.
2. Introduction to Operating System and Linux environment.
3. Linux file system hierarchy (Explanation of directories like /home, /etc, /var, /bin, /usr, etc.
4. Understanding the terminal.
○ What is a shell ?
5. Basic Command Line Usage:
○ How to navigate the filesystem using commands (cd, pwd, ls etc).
○ File and directory creation and removal commands (touch, mkdir, rm).
○ Copying, moving, and renaming files and directories (cp, mv).
Importance of Shell Scripting for Software Engineers :-
1. Data Backup and Restoration: A routine task performed by system administrators is to
back up data regularly. This can be automated using a shell script that compresses
directories, copies files to a backup server or cloud storage, and logs the status.
Automating these tasks ensures they are performed consistently and can be scheduled
for off-peak times to reduce impact on system performance.
2. Automation: Shell scripting allows us to automate repetitive tasks. This can save a lot of
time and reduce the chance for errors. For example, instead of typing the same series of
commands every time to compile a program, we can create a script that does this
automatically.
3. Task Scheduling: With shell scripting and tools like cron, tasks can be scheduled to run
at specific times. For instance, data backups or system maintenance tasks can be
automated to run during off-peak hours. This reduces the manual intervention needed
and allows engineers to focus on more important tasks.
4. System Monitoring: Writing a shell script to monitor system performance, disk usage,
or network connections can provide valuable insights. For instance, a script could be set
up to run every hour, checking the system's CPU or memory usage, and sending an alert
email to the admin if usage exceeds a certain threshold.
5. User Management: In environments with numerous users, like a school or a large
company, a shell script can automate the process of adding, deleting, and managing
users. For example, a script could create a new user, set up their password, create user
directories, set up user permissions, and more.
6. Automating Software Installation: Shell scripting is often used to automate the
installation and configuration of software, ensuring consistency across multiple
installations. These scripts can download software, unpack archives, set environment
variables, modify system configuration files, and more.
7. Web Scraping and Data Extraction: Shell scripts can also be used for web scraping -
downloading web pages and extracting useful information. For instance, a shell script
could be set up to download daily weather forecasts, extract the relevant information,
and even send a summary email.
Introduction to Operating System and Linux :
● What is an Operating System?
○ Definition: An operating system (OS) is system software that manages computer
hardware, software resources, and provides various services for computer
programs.
○ Examples: Windows, MacOS, Linux, Android, etc.
● Functions of an Operating System
○ Process Management: The OS manages processes in the system, including the
allocation and reallocation of resources.
○ Memory Management: The OS is responsible for managing the computer's
memory, including the allocation (and reallocation) of memory space to programs
as they run.
○ File System Management: The OS manages files and directories on storage
devices (like hard drives), and it controls access and usage of these
files/directories.
○ Device Management: The OS manages device communication via their
respective drivers.
○ Security and Access Control: The OS ensures that unauthorized users do not
access the system.
● Introduction to Linux
○ Definition: Linux is an open-source operating system. It is based on the Unix
operating system and provides functionality required for system administrators,
professionals, and home users.
● Advantages of Linux
○ Pen Source : As it is open-source, its source code is easily available. Anyone
having programming knowledge can customize the operating system. One can
contribute, modify, distribute, and enhance the code for any purpose.
○ Security : The Linux security feature is the main reason that it is the most
favorable option for developers. It is not completely safe, but it is less vulnerable
than others. Each application needs to be auclthorized by the admin user. The
virus is not executed until the administrator provides the access password. Linux
systems do not require any antivirus program.
○ Free : Certainly, the biggest advantage of the Linux system is that it is free to
use. We can easily download it, and there is no need to buy the license for it.
○ Lightweight : Linux is lightweight. The requirements for running Linux are much
less than other operating systems. In Linux, the memory footprint and disk space
are also lower.
○ Stability : Linux is more stable than other operating systems. Linux does not
require to reboot the system to maintain performance levels. It rarely hangs up or
slow down. It has big up-times.
● Linux Shell
○ Introduction: Shell is a command-line interface that allows users to interact with
the operating system. Common Linux shells include bash, sh, and zsh.
● Basic Linux Commands
○ File Operations: Commands like ls, cd, cp, mv, rm, mkdir, rmdir.
○ Text Manipulation: Commands like cat, more, less, head, tail, sort, cut, grep.
○ Process Management: Commands like ps, top, kill, bg, fg, shutdown, reboot.
○ Permissions: Commands like chmod, chown, chgrp.
● File System Structure
○ Linux file system hierarchy standard, the importance of key directories (/bin, /etc,
/home, /root, /dev, /usr, etc.).
Linux file system hierarchy :-
Introduction: Explain that unlike Windows, which uses a drive letter abstraction (C:, D:), Linux
has a single, unified directory structure, starting from root ("/"). All files, devices, and resources
appear under this root directory, even if they're stored on different physical devices.
● The Root Directory ("/"): /: The root directory. Everything on our Linux system is
located under this directory, known as the root directory. We can think of the root
directory as being similar to the C:\ drive in Windows – but this isn't a perfect
comparison, because in Linux, even devices like hard drives are treated as files.
● /bin: Short for 'binaries', this directory contains essential command binaries (programs)
that need to be available in single user mode; for all users, e.g., cat, ls, cp.
● /etc: This is the nerve center of our system, it contains all system related configuration
files here or in its sub-directories. A "configuration file" is defined as a local file used to
control the operation of a program; it must be static and cannot be an executable binary.
● /home: Home directories for all users to store their personal files. For example,
/home/john could be a directory.
● /root: The home directory for the root user. It is not the same as the root directory ("/")!
● /var: This directory contains files to which the system writes data during the course of its
operation. It's the administrative area of the Linux file system that contains data that
changes as the system is running, such as log files and print queues.
● /usr: This directory is for shareable, read-only data, and is typically fairly large. This is
where user-accessible programs and data are stored. It has several subdirectories,
including bin, sbin, lib, and others.
● /lib: Contains libraries needed by the essential binaries in the /bin and /sbin folder.
Libraries needed by the binaries in the /usr/bin folder are located in /usr/lib.
● /tmp: Directory that is used by system and users to store temporary files. These files are
generally deleted upon system reboot.
● /dev: Contains all device files. Linux treats everything as files and hardware devices like
hard drive, terminals, USB devices are considered files and their respective file
representations are located in this directory.
Introduction to Terminal & Shell :
● Introduction to Terminal: Terminal or command line or console, is a text-based
interface used to operate the computer. All commands written in the terminal are passed
directly to the operating system.
Example: Show the terminal to the students. Type the command echo "Hello, world!"
and explain that echo is a command that tells the terminal to display whatever text we
provide. When we press 'Enter', the terminal should print Hello, world!
● Introduction to Shell: Shell is a program that reads the commands we type into the
terminal and translates them into a language the operating system can understand. We
could say it's like an intermediary between us and the operating system. There are
different types of shells, but the most commonly used is the 'Bourne Again Shell', or
'bash'.
Example: Type bash into the terminal and hit enter. Explain that we have now started a
new bash shell session. If we type exit and hit enter, it will end the session. To
demonstrate how the shell interprets commands, we can again use the echo "Hello,
world!" command.
Basic Command Line Usage :
● cd
● ls command
● pwd
● mkdir
● rmdir
CD Command :-
The cd command in Linux is used to change the current working directory.
● cd [directory]: This changes the current directory to the specified directory.
● cd: Without any arguments, cd will change the current directory to the user's home
directory.
● cd ~: The tilde (~) is a shortcut for the home directory. So, cd ~ will change the current
directory to the home directory. This behaves the same as cd with no arguments.
● cd -: This command changes the current directory to the previous directory (or the
directory where we were before the current directory).
LS Command :-
The ls command in Linux is used to list the contents of a directory.
● ls : The basic command that lists the files and directories in the current directory.
● ls -l : Lists files and directories with detailed information including permissions, number
of links, owner, group, size, and time of last modification.
● ls -a : Shows all files and directories including hidden ones (those whose names start
with .)
● ls -R : Lists files and directories recursively, meaning it also lists the contents of any
subdirectories.
● ls -t : Lists files and directories sorted by the time of the last modification.
● ls -S : Lists files and directories sorted by size.
● ls -r : Lists files and directories in reverse order.
● ls -lh: Lists files and directories with detailed information including size in a
human-readable format (K, M, G).
● ls -la: Shows all files (including hidden ones) with detailed information.
● ls -ld : Shows details about the directory itself, not the files within the directory.
PWD Command :- Print Working Directory
MKDIR Command :-
mkdir directory_name: This is the basic usage of mkdir which creates a new directory with the
specified name.
● mkdir directory_name: This is the basic usage of mkdir which creates a new directory
with the specified name.
● mkdir directory1 directory2: We can create multiple directories at the same level by
specifying the names separated by a space.
● mkdir -v directory_name: The -v (verbose) option will make mkdir explain what it's
doing. That means it will print a message for each created directory.
RMDIR Command :-
The rmdir command in Linux is used to remove empty directories.
● rmdir directory_name: This is the basic usage of rmdir which removes the empty
directory with the specified name.
● rmdir directory1 directory2: We can remove multiple directories at the same time by
specifying the names separated by a space.
● rmdir -v directory_name: The -v (verbose) option will make rmdir explain what it's
doing. That means it will print a message for each directory it removes.
NOTE : rmdir can only remove empty directories. If a directory contains files or other
directories, we would need to use a different command, such as rm -r directory_name, to
remove it and its contents. We need to be careful while using rm -r as it can remove directories
and their contents without confirmation.