A Practical Guide To Linux Commands Editors And Shell Programming

A Practical Guide To Linux Commands Editors And Shell Programming

Reading time1 min
#Linux#Scripting#DevOps#Shell#CommandLine

Mastering Linux: Essential Commands, Editors, and Shell Scripting

Managing cloud deployments, debugging CI/CD failures, or automating server maintenance—all of these require solid Linux fundamentals. While graphical interfaces and web dashboards come and go, the command line is still the engineer’s sharpest tool. In practice, mastery of shell utilities, text editors, and scripting separates those who maintain systems from those who shape and optimize them.


The Linux Command Line: Core Utilities

Linux CLI remains architecture-agnostic—local dev, Docker containers, or cloud VMs. Knowing a core subset accelerates nearly all operational and development workflows.

File and Directory Navigation

CommandPurposePro Tip
ls -lahDirectory listing, human-readablels -ltr sorts by modified
cd /etc/nginxMove to NGINX configcd - toggles previous dir
pwdPrint current working directoryUseful in scripts for logging

File Manipulation

cp -r /opt/app /tmp/backup_app
mv app.log app.log.1
rm -rf /tmp/old_data

Note: rm -rf can delete critical data—no confirmation by default. For added safety, alias it to prompt:
alias rm='rm -i'

Finding and Searching

grep -i "timeout" /var/log/nginx/error.log
find /var/log -name "*.gz" -mtime +14 -delete
  • -i in grep ignores case.
  • mtime +14: match files older than 14 days.
    Gotcha: find ... -delete acts in-place, use caution.

Process and System Monitoring

ps aux | grep postgres
top -Hp $(pgrep -u nginx)
kill -9 23124
  • -9 signal forces immediate kill. Prefer kill -15 for graceful termination.
  • For dynamic resource monitoring, install htop. Not present by default; sudo apt install htop.

Permissions and Ownership

chmod 644 config.yaml
chown nginx:nginx /var/www/html -R

Note: Always double-check recursive changes.


Text Editors: nano vs. vim

Editing configuration under pressure is common—think failing Kubernetes rollout (Helm chart typo, anyone?).

nano: For Rapid Tweaks

  • nano /etc/fstab
  • Arrow keys navigate, Ctrl+O saves, Ctrl+X exits.
  • Lightweight, but limited for large code refactoring.

vim: Ubiquitous, Scriptable, Unforgiving

vim /etc/systemd/system/myapp.service
  • Modes: normal, insert (i), command (:).
  • Quick actions: :wq (save & exit), /keyword (search).
  • Copy whole lines (yy), paste (p).
    Practicing with temporary files avoids accidental production edits.
    Non-obvious tip: vim -d file1 file2 launches a built-in diff tool.

Shell Scripting: From Quick Fixes to Automation

Scripts should be explicit—no silent failures. Always set set -euo pipefail at the top.

Backup Example: Timestamps, Logging

#!/bin/bash
set -euo pipefail

SRC="$HOME/Documents"
DST="/backup/documents_$(date +%Y%m%d_%H%M%S)"
mkdir -p "$DST"
cp -av "$SRC/" "$DST/" >"$DST/backup.log" 2>&1

echo "Backup stored at $DST"
  • -a in cp preserves permissions and timestamps.
  • Logs stdout and stderr.

Cron Automation: Log Rotation

Prevent uncontrolled log growth—manual cleanups don't scale.

#!/bin/bash
set -e

LOGDIR="/var/log/myapp"
find "$LOGDIR" -type f -name '*.log' -mtime +30 -print -delete
  • Always use -print in destructive commands to verify what’s being removed.
  • Add 0 3 * * * /path/cleanup.sh to the crontab for scheduled runs.
  • Known issue: If another process writes logs during deletion, you may encounter "Text file busy" errors.

Advanced Usage and Additional Tools

  • man pages: For deep-dive flags and options. man tar | less +/compression jumps to “compression”.
  • awk, sed, cut: Transform log output or CSV data inline.
  • Multiplexing: tmux or screen for durable SSH sessions; avoids accidental command termination during long jobs.
  • Version Checks: Always confirm syntax against current system version (bash --version, grep --version). Some flags change.

Conclusion (Interjected)

In practice, you spend more time reading logs, patching configs, or automating repeatable work than you do writing “greenfield” code. Shell mastery accelerates problem diagnosis and operational peace of mind.


Side Note: Alternative Approaches

Text editors like emacs or IDE plugins (e.g., VSCode Remote - SSH, as of v1.90) offer more UX, but if the server is in a restricted environment or in a recovery state, basic CLI tools are indispensable.


Final Recommendations

  • Iterate scripts—never trust one-offs. Parameterize, log, and handle error paths.
  • Never automate what you don’t understand.
  • Use source control for scripts managing infrastructure.

No system—or engineer—is ever truly “done” sharpening command line skills. The most effective solutions are sometimes the simplest, with a find, a well-placed pipe, or a dozen-line script.


If you have real-world war stories from the trenches or hard-earned command line tips, contribute below or send a patch.