DEV Community

Abhishek Gupta
Abhishek Gupta

Posted on

LINUX & SHELL PROGRAMMING โ€” COMPLETE NOTES (BCA-2 CIITM Dhanbad)

๐ŸŸฆ UNIT โ€“ I : Linux Introduction, Architecture, File System & Basic Commands


โญ 1. What is Linux?

  • Linux is an open-source operating system based on UNIX.
  • It is used in:

    • Servers
    • Web hosting
    • Ethical hacking
    • Cloud computing
    • Programming environments
    • IoT devices
  • Highly secure, stable, free and powerful.


โญ 2. Basic Features of Linux

  • Multi-user โ†’ many users can use at same time
  • Multi-tasking โ†’ multiple programs run simultaneously
  • Open-source โ†’ public code, free to edit
  • Portable โ†’ runs on any hardware
  • Secure โ†’ permissions + encryption
  • Networking โ†’ built-in tools for communication
  • Shell scripting โ†’ automation support

โญ 3. Linux Flavors (Distributions)

Popular Linux distros:

  • Ubuntu
  • Debian
  • Fedora
  • RedHat (RHEL)
  • CentOS
  • Kali Linux
  • Linux Mint

โญ 4. Advantages of Linux

  • Free
  • Highly secure
  • Virus-free
  • Stable and fast
  • Customizable
  • Used by developers and servers
  • Command-line power

โญ 5. Installation Requirements

  • 1โ€“2 GB RAM
  • 20โ€“30 GB Disk
  • Bootable USB
  • 1 GHz Processor
  • BIOS/UEFI system

โญ 6. Linux Architecture

1. Hardware Layer

Physical parts (CPU, RAM, I/O devices)

2. Kernel

  • Heart of Linux
  • Manages CPU, memory, processes, devices

3. Shell

  • Interface between user & kernel
  • Executes commands
  • Types: Bash, Zsh, Ksh, Tcsh

4. User Space

Applications & utilities


โญ 7. Linux File System (Standard Directories)

Directory Purpose
/ Root of file system
/home User home directories
/bin Basic commands
/sbin System commands
/etc Configuration files
/usr Installed software
/var Logs, mail, spool
/dev Device files
/tmp Temporary files
/root Root userโ€™s home

โญ 8. Basic Linux Commands

Command Use
pwd Show current directory
cd Change directory
ls List files
mkdir Create directory
rmdir Remove empty directory
cp Copy files/directories
mv Move/rename
rm Delete file/folder
file Show file type
more, less View long files

๐ŸŸฆ UNIT โ€“ II : File Handling, Shells, Pipes, I/O Redirection, Processes


โญ 1. Creating & Viewing Files using cat

cat > file.txt     # create
cat file.txt       # view
cat file1 file2    # view multiple
Enter fullscreen mode Exit fullscreen mode

โญ 2. File Comparison

Command Use
cmp f1 f2 Compare byte-by-byte
comm f1 f2 Compare line-by-line

โญ 3. Disk Related Commands

  • df -h โ†’ disk free space
  • du -sh folder โ†’ folder size
  • mount โ†’ attach device
  • umount โ†’ detach device

โญ 4. Shells in Linux

A shell is a program that reads commands and executes them.

Types:

  • Bash
  • Sh
  • Zsh
  • Ksh
  • Tcsh

โญ 5. Processes in Linux

What is a process?

A running program.

Types:

  • Foreground
  • Background

โญ 6. Pipes

Used to connect output of one command to another.

ls | wc -l
Enter fullscreen mode Exit fullscreen mode

โญ 7. I/O Redirection

Redirection Meaning
> Output to file
>> Append
< Input from file
2> Error output
&> Redirect both output & error

โญ 8. Manual Help Commands

  • man command โ†’ detailed help
  • help command โ†’ shell built-in help

โญ 9. Background Processing

command &
jobs
Enter fullscreen mode Exit fullscreen mode

๐ŸŸฆ UNIT โ€“ III : Process Management, Scheduling, Commands


โญ 1. Managing Processes

Command Purpose
ps List processes
top Live processes
kill PID Kill process
jobs Background jobs
bg / fg Move job foreground/background

โญ 2. Changing Process Priority (nice & renice)

nice -n 10 program
renice 5 PID
Enter fullscreen mode Exit fullscreen mode

Lower value โ†’ higher priority
Higher value โ†’ lower priority


โญ 3. Scheduling Processes

Using cron

Used for repeated jobs.

crontab -e
Enter fullscreen mode Exit fullscreen mode

Cron format:

min hour day month weekday command
Enter fullscreen mode Exit fullscreen mode

Using at

One-time scheduling:

at 17:00
Enter fullscreen mode Exit fullscreen mode

โญ 4. File Related Commands

  • wc file.txt โ†’ count lines, words, characters
  • cut -d',' -f1 โ†’ cut column
  • dd if=in of=out โ†’ copy/convert
  • head / tail โ†’ first/last lines

โญ 5. Math Commands

expr 2 + 3
echo "5*7" | bc
Enter fullscreen mode Exit fullscreen mode

๐ŸŸฆ UNIT โ€“ IV : vi/vim Editor & Shell Programming (Detailed)


โญ 1. vi/vim Editor

Modes:

  • Command Mode โ†’ navigation
  • Insert Mode โ†’ i, a, o
  • Last Line Mode โ†’ :w, :q, :wq

Important commands:

Command Description
i Insert text
x Delete character
u Undo
/text Search
:w Save
:q Quit
:wq Save & quit

๐ŸŸจ SHELL PROGRAMMING (BEST DETAILED NOTES)


โญ What is Shell Programming?

Shell programming means writing a series of Linux commands in a file to automate tasks.
A shell script is simply a text file with commands.


โญ Writing Your First Shell Script

  1. Create file:
nano script.sh
Enter fullscreen mode Exit fullscreen mode
  1. Add shebang:
#!/bin/bash
echo "Hello World"
Enter fullscreen mode Exit fullscreen mode
  1. Make executable:
chmod +x script.sh
Enter fullscreen mode Exit fullscreen mode
  1. Run:
./script.sh
Enter fullscreen mode Exit fullscreen mode

โญ Shell Variables

name="Abhishek"
echo "My name is $name"
Enter fullscreen mode Exit fullscreen mode

Variable rules:

  • No spaces
  • Use $var to access
  • Strings need quotes

System variables: $HOME, $USER, $PATH


โญ Input Using read

read username
echo "You typed $username"
Enter fullscreen mode Exit fullscreen mode

โญ Conditional Statements

if [ $a -gt 10 ]
then
  echo "Greater"
else
  echo "Smaller"
fi
Enter fullscreen mode Exit fullscreen mode

Operators:

  • -eq, -ne, -gt, -lt, -ge, -le

โญ Case Statement

case $choice in
1) echo "One" ;;
2) echo "Two" ;;
*) echo "Invalid" ;;
esac
Enter fullscreen mode Exit fullscreen mode

โญ Looping

For loop

for i in 1 2 3
do
 echo $i
done
Enter fullscreen mode Exit fullscreen mode

While loop

while [ $i -lt 5 ]
do
 echo $i
 i=$((i+1))
done
Enter fullscreen mode Exit fullscreen mode

โญ Passing Arguments

./script.sh arg1 arg2
echo "First arg = $1"
echo "Second arg = $2"
Enter fullscreen mode Exit fullscreen mode

โญ Arrays

fruits=("apple" "banana" "cherry")
echo ${fruits[1]}
echo ${fruits[@]}
Enter fullscreen mode Exit fullscreen mode

โญ Functions

hello() {
  echo "Hello $1"
}
hello "Abhishek"
Enter fullscreen mode Exit fullscreen mode

โญ Automating System Tasks (Example)

  • Backup script
  • Auto update script
  • Log cleanup
  • Disk monitoring
  • User creation

โญ FINAL SHELL PROGRAM (BEST EXAM SCRIPT)

#!/bin/bash
# System Backup Script

echo "Enter folder to backup:"
read folder

if [ -d "$folder" ]; then
  echo "Backing up..."
  tar -czf backup.tar.gz "$folder"
  echo "Backup saved as backup.tar.gz"
else
  echo "Folder does not exist!"
fi
Enter fullscreen mode Exit fullscreen mode

๐ŸŸฆ UNIT โ€“ V : Filters & Regular Expressions


โญ Simple Filter Commands (Very Important)

Command Use
head First 10 lines
tail Last 10 lines
cut Extract columns
paste Merge files
sort Sort text
uniq Remove duplicates
tr Replace characters
pr Format text

โญ grep (search text)

grep "hello" file.txt
Enter fullscreen mode Exit fullscreen mode

โญ egrep (extended grep)

Supports advanced patterns:

egrep "cat|dog" file.txt
Enter fullscreen mode Exit fullscreen mode

โญ sed (find & replace)

sed 's/old/new/g' file.txt
Enter fullscreen mode Exit fullscreen mode

โญ awk (pattern scanning)

awk '{print $1}' file.txt
Enter fullscreen mode Exit fullscreen mode

Top comments (0)