0% found this document useful (0 votes)
7 views22 pages

Advanced Linux Summary 1723323191

Uploaded by

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

Advanced Linux Summary 1723323191

Uploaded by

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

Advanced Linux Summary

Over this Summary, I've covered a wide range of topics related to Linux and Shell
scripting. I started with Mastering the command Line, including file system structure,
basic commands and command-line help. Next, we delved into networking concepts
such as network interfaces, configuration, tools and troubleshooting. Next, I explored
the file system hierarchy, including directory structure, permissions, file management
and system files. Finally, I covered Shell scripting, focusing on script structure,
variables, control structures, functions, file operations and advanced scripting
techniques.
Each summary has been designed to provide a thorough understanding of these topics,
offering both theoretical insights and practical use of the commands.

Advanced Linux Summary:


Mastering the Command Line & Beyond
Shell Mastery and Scripting
1. Shell Environments

• Environment Variables:
o export VAR=value: Set environment variables.
o printenv: Display environment variables.
o env: Run a program in a modified environment.
• Shell Initialization Files:
o .bashrc, .bash_profile, .profile: Customize shell environments.
o source [file]: Apply changes from a shell script or config file without logging
out.

2. Shell Scripting

• Control Structures:
o if, elif, else, fi: Conditional execution.
o case, esac: Multi-way branching.
o for, while, until, do, done: Loop structures.
• Functions:
o function_name() { commands; }: Define a function.
o return [value]: Exit a function with a return value.
o local var_name=value: Define local variables in functions.
• Text Processing:
o grep [pattern] [file]: Search for patterns in files.
o sed 's/old/new/g' [file]: Stream editor for basic text transformations.
o awk '{print $1, $3}' [file]: Pattern scanning and processing language.
o cut -d[delimiter] -f[field] [file]: Remove sections from each line.
• Automation and Scheduling:
o cron: Schedule tasks to run periodically (crontab -e to edit cron jobs).
o at [time] [command]: Schedule one-time tasks.
o sleep [time]: Delay execution for a specified time.

File Management & Manipulation


3. Advanced File Operations

• File Permissions:
o chmod [permissions] [file]: Change file permissions (e.g., chmod 755).
o chown [user]:[group] [file]: Change file owner and group.
o umask [mask]: Set default file permissions.
• Special Permissions:
o SUID, SGID, Sticky Bit: Advanced permission settings (chmod u+s).
• File Compression and Archiving:
o tar -czvf [archive.tar.gz] [files]: Create a compressed archive.
o tar -xvzf [archive.tar.gz]: Extract a compressed archive.
o zip [archive.zip] [files] / unzip [archive.zip]: Create/extract ZIP files.
• Symbolic Links:
o ln -s [target] [link]: Create a symbolic (soft) link.

4. Filesystem Management

• Disk Usage:
o df -h: Display disk space usage.
o du -sh [directory]: Display the size of a directory.
• Mounting and Unmounting:
o mount [device] [mount-point]: Mount a filesystem.
o umount [device/mount-point]: Unmount a filesystem.
o lsblk: List information about block devices.
• File Systems:
o mkfs.ext4 [device]: Create an ext4 filesystem.
o fsck [device]: Check and repair a filesystem.
o tune2fs: Adjust filesystem parameters.

System Administration
5. Process Management
• Process Control:
o ps aux: Display all running processes.
o top / htop: Interactive process viewer.
o kill [PID]: Terminate a process by PID.
o killall [process_name]: Terminate all instances of a process.
o bg / fg: Background and foreground process control.
• Process Priority:
o nice -n [priority] [command]: Start a process with a given priority.
o renice [priority] -p [PID]: Change the priority of a running process.

6. User and Group Management

• User Management:
o useradd / usermod / userdel: Add, modify, delete users.
o passwd [username]: Change user passwords.
o whoami: Display current username.
• Group Management:
o groupadd / groupmod / groupdel: Add, modify, delete groups.
o usermod -aG [group] [user]: Add a user to a group.
• User Session Management:
o who / w: Display logged-in users.
o last: Show login history.
o su [user]: Switch to another user.

7. Package Management

• Debian-based Distributions:
o apt-get update / apt-get upgrade: Update package lists and upgrade installed
packages.
o apt-get install [package]: Install a package.
o dpkg -i [package.deb]: Install a package from a .deb file.
• Red Hat-based Distributions:
o yum update / yum upgrade: Update package lists and upgrade installed
packages.
o yum install [package]: Install a package.
o rpm -ivh [package.rpm]: Install a package from an .rpm file.
• Universal Package Managers:
o snap install [package]: Install a snap package.
o flatpak install [package]: Install a flatpak package.

Networking & Security


8. Networking Essentials

• Network Configuration:
o ifconfig / ip addr: Display or configure network interfaces.
o ping [host]: Test connectivity to a host.
o netstat -tuln: Display active connections and listening ports.
o traceroute [host]: Trace the route to a host.
o ss -tuln: View open sockets.
• SSH (Secure Shell):
o ssh [user]@[host]: Connect to a remote host via SSH.
o scp [file] [user]@[host]:[destination]: Securely copy files between hosts.
o rsync -avz [source] [destination]: Synchronize files and directories.
• Firewall Management:
o ufw enable / ufw disable: Enable/disable the UFW firewall.
o ufw allow [port] / ufw deny [port]: Allow/deny traffic on a port.
o iptables -L: List firewall rules.
• Network Diagnostics:
o dig [domain]: Query DNS servers.
o nslookup [domain]: DNS lookup utility.
o nmap [host]: Network exploration and security auditing tool.

9. Security Practices

• User Permissions:
o sudo [command]: Execute a command as the superuser.
o visudo: Edit the sudoers file.
• File Encryption:
o gpg -c [file]: Encrypt a file with GPG.
o gpg -d [file.gpg]: Decrypt a GPG-encrypted file.
• System Auditing:
o auditctl -l: List active audit rules.
o ausearch -i: Search audit logs.
• SELinux:
o getenforce / setenforce [enforcing|permissive|disabled]: Get or set SELinux mode.
o semanage fcontext -a -t [type] '[file]': Set file security context.

System Monitoring & Performance


10. Performance Monitoring

• System Load:
o uptime: Display system uptime and load average.
o vmstat: Report virtual memory statistics.
o iostat: Report CPU and I/O statistics.
• Resource Usage:
o free -h: Display memory usage.
o df -h: Report file system disk space usage.
o sar: Collect, report, and save system activity information.
• Network Performance:
o iperf: Network bandwidth measurement tool.
o ethtool: Display or change ethernet device settings.
o tcpdump: Packet analyzer for network traffic.

11. Log Management

• Log Files:
o /var/log/syslog: System log messages.
o /var/log/auth.log: Authentication log.
o /var/log/kern.log: Kernel log messages.
• Log Monitoring:
o tail -f /var/log/syslog: Follow live log file updates.
o journalctl: Query systemd journal logs.
o logger [message]: Add a message to the system log.

Backup, Recovery & Virtualization


12. Backup and Recovery

• Backup Tools:
o rsync -av --delete [source] [destination]: Synchronize and backup directories.
o tar -cvpzf [backup.tar.gz] [directories]: Create a full backup.
• Disk Imaging:
o dd if=[input] of=[output]: Create a disk image.
o partclone: Partition cloning and imaging tool.
• Recovery Techniques:
o fsck /dev/[device]: Repair a filesystem.
o testdisk: Recover lost partitions and files.

13. Virtualization

• Virtual Machines:
o virt-manager: GUI tool to manage virtual machines (KVM/QEMU).
o virsh: Command-line interface to manage virtual machines.
• Containers:
o docker ps: List running containers.
o docker images: List available images.
o docker run -d [image]: Run a container in detached mode.
• Container Orchestration:
o kubectl get pods: List Kubernetes pods.
o kubectl logs [pod]: View logs of a Kubernetes pod.
o kubectl apply -f [file.yaml]: Apply a Kubernetes configuration.
Troubleshooting & Advanced Topics
14. Troubleshooting Techniques

• System Diagnostics:
o dmesg: Print kernel ring buffer messages.
o strace [command]: Trace system calls and signals.
o lsof: List open files and the processes that opened them.
• Boot Issues:
o grub2-install: Reinstall GRUB bootloader.
o systemctl rescue: Boot into rescue mode.
o systemctl default: Switch back to the default target (usually graphical).

15. Advanced Kernel Management

• Kernel Compilation:
o make menuconfig: Configure the Linux kernel.
o make bzImage: Compile the Linux kernel.
o make modules / make modules_install: Compile and install kernel modules.
• Kernel Modules:
o lsmod: List loaded kernel modules.
o modprobe [module]: Load a kernel module.
o rmmod [module]: Remove a kernel module.
• System Tuning:
o sysctl -p: Apply kernel parameter changes.
o ulimit -n [value]: Set the maximum number of open file descriptors.
o echo 1 > /proc/sys/vm/drop_caches: Clear the system cache.

16. Advanced Networking

• VPNs and Tunnels:


o openvpn [config.ovpn]: Connect to an OpenVPN server.
o ssh -L [local_port]:[remote_host]:[remote_port] [user]@[host]: Create an SSH tunnel.
• Advanced Firewall Configuration:
o iptables -A INPUT -p tcp --dport [port] -j ACCEPT: Allow incoming traffic on a port.
o firewalld: Advanced firewall management (firewall-cmd --add-port=[port]).
• Network File Systems:
o mount -t nfs [server]:/[share] [mount-point]: Mount an NFS share.
o smbclient //server/share: Access an SMB/CIFS share.
Advanced Linux Networking Resume
Networking Theory and Concepts
1. Networking Basics

• OSI Model: 7 Layers – Physical, Data Link, Network, Transport, Session,


Presentation, Application.
• TCP/IP Model: 4 Layers – Network Interface, Internet, Transport, Application.
• IP Addressing: Unique identifier for each device on a network. IPv4 and IPv6.
• Subnetting: Dividing an IP network into sub-networks to improve performance
and security.
• Routing: Process of forwarding packets from source to destination across
networks.
• NAT (Network Address Translation): Translates private IP addresses to a
public IP address.

2. Network Protocols

• TCP (Transmission Control Protocol): Connection-oriented, reliable


communication.
• UDP (User Datagram Protocol): Connectionless, faster but less reliable
communication.
• ICMP (Internet Control Message Protocol): Error reporting and diagnostic
functions (e.g., ping).

3. Network Services

• DNS (Domain Name System): Translates domain names to IP addresses.


• DHCP (Dynamic Host Configuration Protocol): Automatically assigns IP
addresses and network configurations.
• HTTP/HTTPS (Hypertext Transfer Protocol/Secure): Web traffic protocols.
• FTP (File Transfer Protocol): Protocol for transferring files.
• SSH (Secure Shell): Secure remote login and command execution.

Networking Commands and Utilities


4. Network Interfaces and Configuration

• ifconfig: Display or configure network interfaces (deprecated, use ip command).


o Example: ifconfig eth0 up / ifconfig eth0 down
• ip: Modern replacement for ifconfig.
o ip addr show [interface]: Display IP addresses.
o ip link set [interface] up/down: Enable/disable an interface.
o ip route show: Display routing table.

5. Network Diagnostics

• ping: Check connectivity to a remote host.


o Example: ping [hostname or IP]
• traceroute: Trace the route packets take to a destination.
o Example: traceroute [hostname or IP]
• mtr: Combine ping and traceroute for real-time network diagnostics.
o Example: mtr [hostname or IP]
• netstat: Network statistics and connections.
o Example: netstat -tuln (list listening ports and services)
• ss: Utility to investigate sockets.
o Example: ss -tuln (list open ports)

6. Network Configuration

• route: Display or modify the IP routing table (deprecated, use ip command).


o Example: route add default gw [gateway IP]
• ip route add: Add routes to the routing table.
o Example: ip route add [network] via [gateway]
• hostname: Display or set the system's hostname.
o Example: hostname / hostnamectl set-hostname [new-hostname]
• nmcli: NetworkManager command-line interface.
o Example: nmcli d (show network devices) / nmcli con up [connection] (activate a
connection)

7. Network Security

• Firewall Management:
o iptables: User-space utility to configure the IP packet filter rules of the
Linux kernel.
▪ Example: iptables -A INPUT -p tcp --dport 22 -j ACCEPT (allow SSH)
o firewalld: Dynamic firewall management tool.
▪ Example: firewall-cmd --add-port=22/tcp --permanent (open port 22 for
SSH)
• SELinux:
o getenforce: Check the current SELinux mode.
▪ Example: getenforce
o setenforce: Change SELinux mode.
▪ Example: setenforce 0 (set SELinux to permissive mode)
• VPN Configuration:
o openvpn: Set up and manage OpenVPN connections.
▪ Example: openvpn --config [config-file]
o strongSwan: IPsec-based VPN solutions.
▪ Example: ipsec status (check VPN status)

Advanced Networking Topics


8. Advanced Routing

• Static Routing: Manually configured routes.


o Example: ip route add [network] via [gateway]
• Dynamic Routing Protocols:
o OSPF (Open Shortest Path First): Link-state routing protocol.
▪ Example: Configure using quagga or bird.
o BGP (Border Gateway Protocol): Path vector protocol used for inter-
domain routing.
▪ Example: Configure using BIRD or ExaBGP.

9. Network Address Translation (NAT)

• Source NAT (SNAT): Modify source addresses of packets.


o Example: iptables -t nat -A POSTROUTING -o [interface] -j MASQUERADE
• Destination NAT (DNAT): Modify destination addresses of packets.
o Example: iptables -t nat -A PREROUTING -p tcp --dport 80 -j DNAT --to-destination
[backend IP]:[port]

10. Network Troubleshooting

• tcpdump: Capture and analyze network traffic.


o Example: tcpdump -i [interface] / tcpdump port 80 (filter by port)
• wireshark: GUI-based network protocol analyzer.
• nmap: Network discovery and security auditing tool.
o Example: nmap -sS -p [port] [hostname or IP]

11. Network Performance Optimization

• QoS (Quality of Service):


o tc: Manage traffic control settings.
▪ Example: tc qdisc add dev [interface] root handle 1: htb default 12
• Bandwidth Monitoring:
o iftop: Display bandwidth usage per interface.
o vnstat: Network traffic monitoring tool.
12. Network File Systems

• NFS (Network File System):


o mount -t nfs [server]:/[share] [mount-point]: Mount an NFS share.
o exportfs: Configure NFS exports.
• CIFS (Common Internet File System):
o mount -t cifs //[server]/[share] [mount-point] -o
username=[user],password=[password] : Mount a CIFS share.
o smbclient //server/share: Access an SMB/CIFS share.

13. Network Namespace

• Network Isolation:
o ip netns add [namespace]: Create a network namespace.
o ip netns exec [namespace] [command]: Run a command within a network
namespace.
o ip link set [interface] netns [namespace]: Move an interface to a network
namespace.

14. Network Bridging and Bonding

• Bridging:
o brctl: Bridge control.
▪ Example: brctl addbr [bridge-name] (create a bridge)
o ip link set [interface] master [bridge-name]: Add an interface to a bridge.
• Bonding:
o bonding: Aggregate multiple network interfaces into a single bond.
▪ Example: Configure /etc/modprobe.d/bonding.conf and
/etc/sysconfig/network-scripts/ifcfg-bond0.

15. Network Time Protocol (NTP)

• Synchronize Time:
o ntpdate [server]: Manually synchronize time with an NTP server.
o chrony: Modern replacement for NTP.
▪ Example: chronyc sources (list NTP sources).

Practical Use Cases and Examples


16. Securing Network Communications

• TLS/SSL:
o openssl: Generate and manage TLS/SSL certificates.
▪ Example: openssl req -new -x509 -keyout server.key -out server.crt -days 365
17. Setting Up a Basic Web Server

• Apache/Nginx:
o Apache: sudo apt-get install apache2 / sudo systemctl start apache2
o Nginx: sudo apt-get install nginx / sudo systemctl start nginx

18. Configuring a Simple VPN

• OpenVPN:
o Server Configuration: openvpn --config /etc/openvpn/server.conf
o Client Configuration: openvpn --config /etc/openvpn/client.conf

Advanced Linux File Management


and Hierarchy Resume
File System Hierarchy
1. Linux File System Structure

• Root Directory (/): The top of the directory tree.


• Important Directories:
o /bin: Essential command binaries (e.g., ls, cp).
o /sbin: System binaries for administrative tasks (e.g., fsck, shutdown).
o /etc: Configuration files and scripts.
o /home: User home directories.
o /root: Home directory for the root user.
o /var: Variable data files, such as logs and mail spools.
o /tmp: Temporary files.
o /usr: User binaries and read-only data (e.g., /usr/bin, /usr/lib).
o /lib: Essential shared libraries and kernel modules.
o /dev: Device files (e.g., /dev/sda).
o /proc: Virtual filesystem providing process and kernel information.
o /sys: Virtual filesystem exposing kernel and device information.
o /mnt: Mount point for temporary file systems.
o /media: Mount point for removable media.
2. Files and Directories

• Regular Files: Contain data, executable programs, or scripts.


• Directories: Containers for files and other directories.
• Special Files:
o Character Device Files: Interface to devices (e.g., /dev/tty).
o Block Device Files: Interface to block devices (e.g., /dev/sda).
o Named Pipes (FIFOs): Used for inter-process communication.
o Sockets: Endpoints for network communication.

File and Directory Commands


3. File Manipulation

• Creating Files:
o touch [file]: Create an empty file or update timestamp.
o echo 'text' > [file]: Create a file with specific content.
o cat > [file]: Create a file and enter content interactively.
• Viewing Files:
o cat [file]: Concatenate and display file content.
o more [file] / less [file]: View file content page-by-page.
o head [file]: Display the first 10 lines of a file.
o tail [file]: Display the last 10 lines of a file.
• Editing Files:
o nano [file] / vi [file] / vim [file]: Text editors for modifying files.
• Copying, Moving, and Renaming:
o cp [source] [destination]: Copy files or directories.
o mv [source] [destination]: Move or rename files or directories.
o rsync [source] [destination]: Synchronize files and directories.
• Removing Files and Directories:
o rm [file]: Remove files.
o rm -r [directory]: Remove directories and their contents.
o find [directory] -type f -name [pattern] -exec rm {} \;: Find and remove files
matching a pattern.

4. File Permissions and Ownership

• Viewing Permissions:
o ls -l [file]: Display file permissions, ownership, and other details.
• Changing Permissions:
o chmod [mode] [file]: Change file permissions.
▪ Example: chmod 755 [file] (owner can read/write/execute, others can
read/execute).
o chmod u+x [file]: Add execute permission for the file owner.
• Changing Ownership:
o chown [owner][:group] [file]: Change file owner and group.
▪ Example: chown user:group [file]
• Special Permissions:
o Setuid (s): Execute file with the file owner’s privileges.
▪ Example: chmod u+s [file]
o Setgid (s): Execute file with the group’s privileges or ensure new files
inherit group ownership.
▪ Example: chmod g+s [directory]
o Sticky Bit (t): Ensure only file owners can delete their files in a directory.
▪ Example: chmod +t [directory]

5. File System and Disk Management

• Disk Usage:
o df -h: Display disk space usage in a human-readable format.
o du -sh [directory]: Show disk usage of a directory and its contents.
• File System Checks and Repairs:
o fsck [device]: Check and repair file systems.
o tune2fs -l [device]: Display file system information.
• Mounting and Unmounting:
o mount [device] [mount-point]: Mount a filesystem.
o umount [mount-point]: Unmount a filesystem.
o mount -t [type] [device] [mount-point]: Specify filesystem type (e.g., ext4, ntfs).
• Creating and Formatting Filesystems:
o mkfs -t [type] [device]: Create a filesystem (e.g., mkfs.ext4 /dev/sda1).
o mkfs.ext4 [device]: Format a partition with the ext4 filesystem.
o parted / fdisk: Partition management tools.

6. Advanced File Operations

• File Compression and Archiving:


o tar -cvf [archive.tar] [files]: Create a tar archive.
o tar -xvf [archive.tar]: Extract a tar archive.
o gzip [file] / gunzip [file.gz]: Compress and decompress files using gzip.
o bzip2 [file] / bunzip2 [file.bz2]: Compress and decompress files using bzip2.
o zip [archive.zip] [files] / unzip [archive.zip]: Compress and decompress files
using zip.
• File Linking:
o ln -s [target] [link]: Create a symbolic link (symlink).
o ln [target] [link]: Create a hard link.
• File Deduplication:
o fdupes [directory]: Find duplicate files in a directory.
o rdfind: Another tool for finding and removing duplicate files.
7. File Searching and Finding

• Basic Search:
o find [directory] -name [pattern]: Find files by name.
o locate [pattern]: Find files using a pre-built index (requires updatedb).
o grep [pattern] [file]: Search for a pattern within a file.
• Advanced Search:
o find [directory] -type f -size +100M: Find files larger than 100 MB.
o grep -r [pattern] [directory]: Recursively search for a pattern in files.
o ack [pattern] [directory]: Search for patterns using ack, an advanced text
search tool.

8. File Integrity and Security

• File Hashing:
o md5sum [file]: Generate MD5 hash of a file.
o sha256sum [file]: Generate SHA-256 hash of a file.
• File Auditing:
o auditd: Audit daemon for monitoring file access and modifications.
o ausearch: Search audit logs.
• SELinux and AppArmor:
o SELinux:
▪ getenforce: Check SELinux mode.
▪ setenforce 0: Set SELinux to permissive mode.
o AppArmor:
▪ aa-status: Check AppArmor status and profiles.
▪ aa-enforce [profile]: Enforce an AppArmor profile.

Advanced Shell Scripting in Linux


Resume
Introduction to Shell Scripting
1. What is Shell Scripting?

• Definition: Shell scripting involves writing scripts using shell commands to


automate tasks in Unix-like operating systems. A shell script is a file containing a
sequence of commands executed by the shell.
• Purpose: Automate repetitive tasks, manage system administration, process
data, and streamline workflows.
2. Shell Types

• Bash (Bourne Again Shell): Default shell in many Linux distributions, known for
its features and scripting capabilities.
• sh (Bourne Shell): Traditional Unix shell, simple and portable.
• zsh (Z Shell): Advanced shell with enhanced features like improved tab
completion.
• ksh (Korn Shell): Combines features of Bourne Shell and C Shell.
• Fish (Friendly Interactive Shell): User-friendly shell with an emphasis on
interactive use.

Shell Scripting Basics


3. Script Structure and Execution

• Shebang Line: #!/bin/bash or #!/usr/bin/env bash


o Purpose: Specifies the interpreter to execute the script.
• Script File: Saved with .sh extension (e.g., script.sh).
• Permissions:
o chmod +x [script.sh]: Make the script executable.
o ./[script.sh]: Execute the script.

4. Basic Commands and Syntax

• Echo:
o echo "text": Display text on the screen.
• Variables:
o Definition: Store data to be used later.
o Syntax: variable_name=value
o Usage: echo $variable_name
o Example: name="John"; echo "Hello, $name"
• Comments:
o Single-line Comment: # This is a comment
o Multi-line Comment: : 'This is a multi-line comment'
• Conditionals:
o if [ condition ]; then ... fi: Basic conditional statement.
o Example:

if [ $age -ge 18 ]; then


echo "Adult"
else
echo "Minor"
fi

Loops:

• For Loop: for var in list; do ... done


• While Loop: while [ condition ]; do ... done
• Until Loop: until [ condition ]; do ... done
• Example:

for i in {1..5}; do
echo "Number $i"
done

5. Functions

• Definition: Reusable blocks of code.


• Syntax:

function_name() {
commands
}
• Example:

greet() {
echo "Hello, $1"
}
greet "Alice"

Advanced Shell Scripting Concepts


6. Input and Output

• Reading Input:
o read variable_name: Read user input.
o Example: read -p "Enter your name: " name
• Redirecting Output:
o >: Redirect output to a file, overwriting.
o >>: Append output to a file.
o Example: echo "Text" > file.txt
• Pipes:
o Usage: Chain commands by passing the output of one as input to another.
o Example: ls -l | grep "file"
• Error Handling:
o Standard Error: 2> (e.g., command 2> error.log)
o Redirecting Both Outputs: command > output.log 2>&1

7. Arrays

• Definition: Collections of variables.


• Syntax:
o Declare: array_name=(value1 value2 value3)
o Access: ${array_name[index]}
• Example:

fruits=("apple" "banana" "cherry")


echo ${fruits[1]} # Output: banana
8. String Manipulation

• Concatenation: str1="Hello"; str2="World"; echo "$str1 $str2"


• Length: ${#string}
• Substring: ${string:position:length}
• Replace: ${string/old/new}

9. File Operations

• File Existence:
o -e: Check if file exists.
o Example: if [ -e file.txt ]; then echo "File exists"; fi
• File Type:
o -f: Regular file, -d: Directory, -s: Non-empty file.
o Example: if [ -d /path/to/dir ]; then echo "Directory exists"; fi
• Handling Files:
o cp, mv, rm: Copy, move, and remove files.
o Example: cp file1.txt file2.txt

10. Process Management

• Background Processes:
o command &: Run a command in the background.
o jobs: List background jobs.
o fg %job_number: Bring a background job to the foreground.
• Process IDs:
o $$: Process ID of the script.
o $!: Process ID of the last background command.
• Killing Processes:
o kill [PID]: Terminate a process by ID.
o killall [process_name]: Terminate all instances of a process.
o pkill [pattern]: Terminate processes matching a pattern.

11. Advanced File and Directory Operations


• Find Command:
o find [directory] -name [pattern]: Search for files.
o Example: find /home -name "*.txt"
• Locate Command:
o locate [pattern]: Find files using a pre-built index.
o updatedb: Update the file database used by locate.
• Diff Command:
o diff [file1] [file2]: Compare two files line by line.

12. Scripting Best Practices

• Error Checking:
o set -e: Exit script on error.
o trap 'commands' ERR: Execute commands on error.
• Debugging:
o set -x: Print commands and their arguments as they are executed.
o bash -n [script.sh]: Check syntax without executing.
• Portability:
o Write portable scripts by adhering to POSIX standards.
• Documentation:
o Use comments and help functions to document scripts.

13. Advanced Features and Techniques

A. Regular Expressions

• Usage: Pattern matching in files.


• Example: grep -E '^pattern' file.txt
• Extended Regex: grep -P 'pattern' file.txt (Perl-compatible regex)

B. Scripting with awk

• Purpose: Text processing and pattern scanning.


• Example: awk '{print $1}' file.txt (Prints the first column).

C. Scripting with sed

• Purpose: Stream editor for filtering and transforming text.


• Example: sed 's/old/new/g' file.txt (Replace all occurrences of 'old' with 'new').

D. User Interaction

• Dialogs: Use dialog or whiptail for interactive user interfaces in scripts.


• Example: dialog --msgbox "Hello World" 10 30

E. Command Substitution

• Syntax: $(command) or `command`


• Example: files=$(ls) (Assign output of ls to variable files).

F. Arithmetic Operations

• Syntax: $((expression))
• Example: result=$((5 + 3))

G. Network Operations

• Using curl and wget:


o curl [URL]: Transfer data from or to a server.
o wget [URL]: Download files from the web.
o Example: curl -O http://example.com/file.txt
Security and Best Practices
14. Security Considerations
• Avoid Hardcoding Sensitive Data: Use environment variables or configuration files.
• Validate User Input: Prevent code injection and malicious input.
• Use readonly for Constants: readonly variable_name

15. Error Handling


• Use trap for Cleanup:
o trap 'commands' EXIT: Execute commands upon script exit.
• Check Command Exit Status:
o if [ $? -ne 0 ]; then echo "Error occurred"; fi

16. Script Optimization

• Profile Script Execution:


o time [script.sh]: Measure execution time.
• Optimize Loops and Conditions:
o Use efficient algorithms and avoid unnecessary computations.

Real-world Examples and Applications


17. System Administration

• Automating Backups:

#!/bin/bash
backup_dir="/backup/$(date +%F)"
mkdir -p $backup_dir
cp -r /important_data/* $backup_dir
18. Log Management

Rotate Logs:

#!/bin/bash
log_file="/var/log/myapp.log"
max_size=10485760 # 10 MB

if [ $(stat -c%s "$log_file") -ge $max_size ];


then
mv "$log_file" "$log_file.old"
touch "$log_file"
fi

19. User Management

• Bulk User Creation:

#!/bin/bash
for user in user1 user2 user3; do
useradd $user
echo "$user:password" | chpasswd
done
20. Monitoring and Alerts

• Disk Space Monitoring:

#!/bin/bash
threshold=90
used_space=$(df / | grep / | awk '{ print $5 }' |
sed 's/%//g')

if [ $used_space -gt $threshold ]; then


echo "Disk space is above threshold!"
fi

You might also like