🐧 Terminal Simulator — Module 2

File Manipulation & Content Inspection

Learn the backup-before-edit habit, safe deletion, and live log debugging — the skills that separate senior engineers from the rest.

Module Progress0/5 steps
STEP 1 / 5
cp

The Sacred Rule — Backup Before You Edit

Real-World Scenario

It's 2 AM and you've been paged. A misconfigured Nginx reverse proxy is returning 502 errors on production. You need to edit nginx.conf immediately — but before touching ANY production config, you MUST create a backup. If your fix makes things worse, you can restore in under 5 seconds. This habit is what separates senior engineers from the rest.

Technical Breakdown

`cp source destination` copies files. The pattern `cp config config.bak` is the single most important habit in production operations. Always create a `.bak` or timestamped copy before editing. With `-p`, you preserve the original permissions and timestamps — critical for config files managed by automated tools like Ansible or Puppet. With `-v` (verbose), you get immediate confirmation.

-vVerbose — print the source and destination of each copy.
-pPreserve mode, ownership, and timestamps.
-iInteractive — prompt before overwriting an existing file.
-rRecursive — copy entire directories and their contents.

Your Task

Create a backup of the Nginx config. Type: cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak

devops@prod-server — bash
devops@prod-server:~$

Quick Guide: File Operations

Understanding the basics in 30 seconds

How It Works

  • cp creates a copy of a file — always backup before editing configs
  • mv moves or renames files — atomic on same filesystem
  • rm deletes files permanently — Linux has NO recycle bin
  • cat displays entire file contents — best for short files
  • tail shows the last N lines — essential for log debugging

Key Benefits

  • Instant rollback with .bak backups
  • Clean server directories with organized archives
  • Safe disk cleanup without data loss
  • Quick config verification before reload
  • Real-time log analysis with tail -f

Real-World Uses

  • Backing up Nginx/Apache configs before hotfixes
  • Archiving old deployment logs for post-mortems
  • Cleaning up CI/CD build artifacts to free disk space
  • Verifying config file integrity after edits
  • Debugging 502/500 errors from Nginx error logs

The File Operations Every DevOps Engineer Must Master

The Backup-Before-Edit Habit

The single most important habit in production operations is creating a backup before editing any configuration file. The command cp config.conf config.conf.bak takes less than a second but can save hours of downtime. Senior engineers do this reflexively — it's as automatic as breathing.

Some teams take this further with timestamped backups:cp nginx.conf nginx.conf.$(date +%Y%m%d-%H%M%S). This creates a named snapshot you can reference during post-mortems. Combined with diff, you can see exactly what changed between versions.

Safe File Operations

Best practices for manipulating files safely on production servers.

  • cp -p — Preserve permissions when backing up.
  • mv -i — Ask before overwriting files.
  • rm -i — Confirm each deletion interactively.
  • Always pwd + ls before rm -rf.

Content Inspection

Tools for quickly reading and monitoring file content.

  • cat — Quick view for short files (<100 lines).
  • tail -n 20 — Last 20 lines of any file.
  • tail -f — Stream logs in real-time.
  • head -n 10 — Check file headers quickly.

The rm -rf Safety Checklist

The rm -rf command is the most dangerous tool in a Linux administrator's toolkit. There is no "undo" — deleted files are gone forever. Before every rm -rf, senior engineers follow this mental checklist:

Before rm -rf:
  1. Run pwd — verify you're in the right directory.
  2. Run ls target/ — verify the target contents.
  3. Double-check the path — no typos, no trailing spaces.
  4. Consider rm -ri for interactive confirmation first.
  5. Execute the command only when 100% confident.