Unix/Linux and Shell Scripting - Extended Full Notes
Generated on: 2025-04-20
Unix/Linux and Shell Scripting - Full Notes
1. Introduction to Unix and Linux
- Unix: Multiuser, multitasking OS developed at Bell Labs.
- Linux: Open-source Unix-like OS created by Linus Torvalds.
- Differences: Linux is open-source and widely used in modern systems.
- Kernel, Shell, Utilities, Application Programs.
2. File System and Directory Management
- Filesystem is hierarchical, starting from root (/)
- Directories: /bin, /home, /etc, /usr, /var, etc.
- Commands:
cd, pwd, ls, mkdir, rmdir, touch, rm, cp, mv
- File Permissions:
- rwxr-xr--
- chmod, chown, chgrp
3. Basic Shell Scripting Concepts
- Shell: Interface between user and kernel (bash, sh)
- Script: File with commands
#!/bin/bash
echo "Hello, World!"
- Variables:
name="Hemanth"
echo $name
4. Advanced Shell Commands and Utilities
- grep: Search text
grep "word" file.txt
- sed: Stream editor
sed 's/old/new/g' file.txt
- awk: Text processing
awk '{print $1}' file.txt
- sort, uniq, head, tail, cut, tr
5. Pipes and Redirection
- >, >>, <, |
- ls > list.txt
- cat file.txt | grep "text"
6. vi/vim Editor
- Modes: Command, Insert, Visual
- i to insert, Esc to return, :wq to save and quit
- Basic commands: dd, yy, p, u
7. Process and Job Management
- ps, top, kill, bg, fg, jobs
- nohup, nice, renice
- kill -9 PID
8. Network Commands
- ping, netstat, ssh, scp, ftp, curl, wget
9. Conditional Statements
if [ $num -gt 0 ]; then
echo "Positive"
elif [ $num -lt 0 ]; then
echo "Negative"
else
echo "Zero"
fi
10. Looping Constructs
- for i in 1 2 3; do echo $i; done
- while [ $x -le 5 ]; do echo $x; ((x++)); done
- until [ $x -gt 5 ]; do echo $x; ((x++)); done
11. Practical Script Example
#!/bin/bash
echo "Backup starting..."
tar -czf backup_$(date +%F).tar.gz /home/user/
echo "Done!"
12. Useful Built-in Variables
$0, $1, $#, $*, $?, $$
13. Functions in Shell
function say_hello() {
echo "Hello $1"
say_hello Hemanth
14. Debugging Scripts
- Use `set -x` and `set +x` to enable/disable debugging
- Use `bash -x script.sh` to trace
15. Scheduling with cron
- Edit with `crontab -e`
- Format: * * * * * /path/to/script.sh
- List: crontab -l
16. Best Practices
- Comment your scripts
- Use quotes to prevent globbing
- Use functions for modularity