Linux Command Line Essentials
Learn the essential Linux commands for navigating the filesystem, managing files, process control, and working efficiently in the terminal.
DevOpsBeginner15 min read
AI Summary
Quick context for Linux Command Line Essentials
Linux Command Line Essentials is a beginner guide in DevOps. Learn the essential Linux commands for navigating the filesystem, managing files, process control, and working efficiently in the terminal.. Key topics: bash, command-line, linux, terminal.
## Why Learn the Command Line
The terminal is the fastest interface to your system. Tasks that take dozens of clicks in a GUI can be done in seconds with the right command. Every developer, DevOps engineer, and sysadmin needs fluency here.
This guide covers the commands you will use daily.
## Navigating the Filesystem
```bash
# Print current directory
pwd
# List files (long format, hidden files, human-readable sizes)
ls -lah
# Change directory
cd /var/log
cd .. # Up one level
cd ~ # Home directory
cd - # Previous directory
```
## Working with Files and Directories
```bash
# Create files
touch readme.md
# Create directories
mkdir -p src/components/utils # -p creates parent dirs
# Copy
cp file.txt backup.txt
cp -r src/ src-backup/ # Recursive copy for directories
# Move / rename
mv old-name.txt new-name.txt
mv file.txt /tmp/
# Remove
rm file.txt
rm -rf dist/ # Recursive force delete
```
## Viewing File Contents
```bash
# Print entire file
cat config.json
# View with line numbers
nl -ba script.sh
# Paginated viewing (space to scroll, q to quit)
less large-log.txt
# First/last N lines
head -n 20 access.log
tail -n 20 access.log
# Follow a log file in real time
tail -f /var/log/syslog
```
## Searching and Filtering
```bash
# Search file contents (recursive, ignore case)
grep -rn "TODO" src/
# Search with context lines
grep -rn -B2 -A2 "error" app.log
# Find files by name
find . -name "*.test.ts"
find /var -type f -mtime -7 # Modified in last 7 days
# Locate files by database (faster than find)
locate nginx.conf
```
## Piping and Redirection
Pipes send the output of one command as input to another. This is where the power of the command line emerges.
```bash
# Count lines in a file
wc -l access.log
# Count unique IP addresses
awk '{print $1}' access.log | sort | uniq -c | sort -rn | head -10
# Redirect output to a file
echo "hello" > greeting.txt # Overwrite
echo "world" >> greeting.txt # Append
# Discard output
command > /dev/null 2>&1
```
## Process Management
```bash
# List running processes
ps aux | grep nginx
# Kill a process by name
pkill -f node
# Kill by PID
kill 12345
kill -9 12345 # Force kill
# Run a command in the background
long-running-task &
# Bring background job to foreground
fg
# List background jobs
jobs
```
## Disk and System Info
```bash
# Disk usage per directory
du -sh /var/log/*
# Overall disk usage
df -h
# System info
uname -a
cat /etc/os-release
# Memory info
free -h
# Running services
systemctl status nginx
```
## Keyboard Shortcuts
These save more time than any other tip:
- `Ctrl + R` — reverse history search
- `Ctrl + A` / `Ctrl + E` — jump to start/end of line
- `Ctrl + W` — delete word before cursor
- `Ctrl + U` — delete entire line before cursor
- `Ctrl + L` — clear screen
- `Ctrl + C` — cancel current command
- `!!` — repeat last command
- `!$` — last argument of previous command
## Building a Productive Shell
Customize your shell with aliases and functions in `~/.bashrc` or `~/.zshrc`:
```bash
# Useful aliases
alias gs="git status"
alias ll="ls -lah"
alias dc="docker compose"
alias tf="tail -f"
# Function to create and enter a directory
mkcd() { mkdir -p "$1" && cd "$1"; }
```
The command line is a skill that compounds. The more you use it, the faster you become. Start with these basics and add commands as you encounter them.