Linux Basics for Beginners
If you're hearing about Linux for the first time, this guide will help you understand the essential commands and concepts
Introduction to Bash
Bash (Bourne Again Shell) is used for automating tasks and reducing manual work.
Common Linux Commands
touch
→ Creates a new file.ls
→ Lists files and directories.ls -ltr
→ Lists files with timestamps (sorted by modification time).nproc
→ Displays the number of CPU cores available.free
→ Shows total, used, and available memory.top
→ Displays real-time system resource usage (CPU, memory, processes).ps -ef
→ Shows all running processes (p
stands for process,ef
provides detailed information).curl
→ Retrieves information from the internet.ps -ef | grep "amazon"
→ Searches for running processes containing "amazon".
Difference Between grep
and awk
grep
→ Returns the entire sentence containing the search pattern.awk
→ More powerful; extracts specific columns from the output.
Difference Between wget
and curl
wget
→ Downloads files from the internet and saves them locally.curl
→ Fetches data from the internet and displays it in the terminal without saving.
Handling Signals in Linux (trap
Command)
Linux has many signals to manage processes.
Example: Running the command yes
prints "y" continuously and doesn't stop.
To handle such cases, the trap
command prevents interruption (e.g., disabling Ctrl + C
).
trap "echo 'Ctrl + C is disabled'" SIGINT
Error Handling in Scripts
set -e
→ Exits the script immediately if any command fails.set -o pipefail
→ Ensures the entire pipeline fails if any command in the pipeline fails.Understanding Shell in Linux
A shell is a command-line interpreter that executes user commands.
Common shell executables:
/bin/bash
/bin/dash
/bin/sh
/bin/ksh
Difference Between sh
and bash
sh
→ Older and minimal; many organizations previously used it due to compatibility.bash
→ More feature-rich and widely adopted as the default shell in modern systems.
Working with Directories
mkdir <folder_name>
→ Creates a new directory.cd <folder_name>
→ Navigates into the created directory.
DevOps and Linux
As a DevOps Engineer, Linux plays a crucial role in infrastructure management. Below are some essential tasks:
1. Version Control with Git
Git is used to track changes in code and collaborate with teams.
Common Git commands:
git clone <repo_url>
→ Clones a repository.git add <file>
→ Stages changes for commit.git commit -m "message"
→ Saves changes with a message.git push origin <branch>
→ Pushes committed changes to the repository.git pull origin <branch>
→ Fetches the latest changes from the repository.
2. Configuration Management
Configuration management ensures that all systems are consistently configured and maintained. Common tools include:
Ansible → Agentless automation for managing servers.
Puppet → Uses declarative code to manage configurations.
Chef → Automates infrastructure management.
Terraform → Infrastructure as Code (IaC) for provisioning resources.
3. Log Management & Troubleshooting
Log files help diagnose issues in applications and infrastructure.
Checking System Logs
tail -f /var/log/syslog
→ Monitors system logs in real-time.journalctl -xe
→ Displays extended system logs for debugging.dmesg | less
→ Checks kernel logs for hardware and boot issues.
Checking Application Logs
tail -f /var/log/nginx/access.log
→ Monitors live NGINX access logs.tail -f /var/log/apache2/error.log
→ Checks Apache error logs.docker logs -f <container_id>
→ Views Docker container logs.
Searching Logs Efficiently
grep "error" /var/log/syslog
→ Finds error messages in system logs.awk '{print $1, $2, $3}' /var/log/syslog
→ Extracts specific columns from logs.cat logfile | less
→ Opens large log files without loading everything at once.
4. Monitoring System Resources
Keeping track of system performance is crucial for stability.
top
→ Displays real-time CPU and memory usage.htop
→ More user-friendly version oftop
.free -m
→ Shows memory usage in megabytes.df -h
→ Checks disk space usage.
5. Automating Tasks with Shell Scripting
Automating routine tasks reduces manual effort and errors.
Example: A script to check disk usage and send alerts if usage exceeds 80%.
#!/bin/bash
THRESHOLD=80 USAGE=$(df -h / | awk 'NR==2 {print $5}' | sed 's/%//')
if [ "$USAGE" -gt "$THRESHOLD" ]; then echo "Warning: Disk usage is above $THRESHOLD% ($USAGE%)" fi
Final Thoughts
Linux is the backbone of modern infrastructure, and mastering its basics is essential for any DevOps Engineer. From managing files and processes to handling logs and automating tasks, these commands and concepts form the foundation of efficient system administration.
As you continue your DevOps journey, focus on:
✅ Scripting & Automation – Reduce manual work with Bash scripts.
✅ Configuration Management – Learn tools like Ansible, Puppet, and Terraform.
✅ Monitoring & Logging – Keep track of system health and troubleshoot issues effectively.
✅ Cloud & Containerization – Explore Docker, Kubernetes, and cloud platforms.