Skip to main content

Ten Terminal Habits That Prevent Disasters

Media Ten Terminal Habits That Prevent Disasters

The Linux terminal is powerful because it lets you work quickly, automate repetitive jobs and manage systems remotely.

That same power also means a small mistake can have a large impact.

A command entered in the wrong directory can delete important files. A missing quote can change how a shell interprets a path. A copied command from the internet can alter system settings before you understand what it does. On a server, one careless action can affect every user.

Good terminal habits are not about being nervous or slow. They are about building small checks into your routine so that mistakes are caught before they become incidents.

Here are ten habits that make command-line work safer, clearer and easier to recover from.

1. Check Where You Are Before Running a Destructive Command

Many serious terminal mistakes happen because the command itself is valid, but it is run in the wrong location.

Before using commands such as:

rm

mv

chmod

chown

find

rsync

check the current directory:

pwd

Then list its contents:

ls -la

For example, this command could be harmless in a temporary test folder and disastrous in the root filesystem:

rm -rf *

The shell does exactly what you tell it to do. It does not know what you intended.

A safer routine is:

pwd

ls -la

Then run the actual command only after confirming the path.

On important systems, make a habit of using absolute paths:

rm -rf /srv/application/cache/*

rather than relying on your current location.

Absolute paths are longer, but they are clearer and easier to review.

2. Preview Changes Before Applying Them

Many Linux tools provide a dry-run, test or preview mode.

Use it whenever the command can delete, overwrite, move or synchronise data.

For rsync:

rsync -aHAXn --delete /source/ /destination/

The -n option performs a dry run.

Nothing changes, but you can inspect what would happen.

For package removal on Debian or Ubuntu:

sudo apt remove --simulate package-name

For log rotation:

sudo logrotate -d /etc/logrotate.conf

For filesystem-table checks:

sudo findmnt --verify

For systemd unit files:

sudo systemd-analyze verify example.service example.timer

For find, print matching files before adding a deletion action:

find /var/tmp -type f -mtime +30 -print

Only after reviewing the results should you consider:

find /var/tmp -type f -mtime +30 -delete

Preview first. Apply second.

3. Read Commands Before Running Them

Never treat terminal commands as magic phrases.

Before pasting a command from a forum, blog post or support message, understand:

  • Which program it runs
  • Which files it changes
  • Whether it uses sudo
  • Whether it downloads anything
  • Whether it deletes, overwrites or replaces files
  • Whether it affects one user or the whole system

Be especially cautious with commands such as:

curl example.com/script.sh | sudo bash

This downloads a script and sends it directly into a root shell.

You do not get a chance to review it first.

A safer approach is:

curl -o script.sh https://example.com/script.sh

less script.sh

Then, after reviewing it:

chmod +x script.sh

sudo ./script.sh

The same caution applies to:

wget -qO- URL | sh

and commands involving:

eval

A trustworthy source can still contain a typo, outdated instructions or code unsuitable for your system.

4. Quote File and Directory Names

Spaces, wildcard characters and shell symbols can change how a command behaves.

Suppose a directory is named:

Client Files

This command is wrong:

rm -rf Client Files

The shell interprets it as two separate paths:

Client

Files

Use quotes:

rm -rf "Client Files"

Or escape the space:

rm -rf Client\ Files

Quoting is also important in scripts.

Unsafe:

rm -rf $TARGET

Safer:

rm -rf "$TARGET"

Without quotes, a variable containing spaces may be split into several arguments.

A variable containing wildcard characters may also expand unexpectedly.

As a general rule, quote variable expansions unless you deliberately want word splitting:

cp "$SOURCE" "$DESTINATION"

5. Treat Wildcards With Suspicion

Wildcards such as *, ? and bracket expressions are useful, but they can match more than expected.

For example:

rm *.log

removes all files in the current directory whose names end in .log.

Before deleting, preview the match:

printf '%s\n' *.log

or:

ls -ld -- *.log

The double dash is useful:

--

It tells many commands that the remaining arguments are file names, not options.

This matters when a file begins with a hyphen.

For example, a file named:

--preserve-root

could confuse a command.

Safer:

rm -- "--preserve-root"

When working with generated paths, null-delimited output is safer than parsing normal text:

find /path -type f -print0

and:

xargs -0

This correctly handles spaces, quotes and unusual characters in filenames.

6. Use the Least Privilege Necessary

sudo is not a general-purpose fix.

It runs a command with elevated privileges, usually as root. That means the command can modify system files, change ownership, remove packages and affect other users.

Before adding sudo, ask:

  • Does this command genuinely need administrative access?
  • Am I fixing the real permission issue?
  • Will this create root-owned files in my home directory?
  • Could it damage system-wide configuration?

For example:

sudo text-editor notes.txt

may create or save notes.txt as root.

Later, your normal user may be unable to edit it.

Check ownership:

ls -l notes.txt

Correct it if necessary:

sudo chown "$USER":"$USER" notes.txt

Run normal desktop and development tools as your normal user wherever possible.

For services, use a dedicated account rather than root:

[Service]

User=backup

Group=backup

The smaller the privilege level, the smaller the potential damage.

7. Make Backups Before Editing Important Files

Before changing a critical configuration file, create a copy.

For example:

sudo cp /etc/fstab /etc/fstab.backup

or include a date:

sudo cp /etc/fstab "/etc/fstab.backup-$(date +%F-%H%M)"

This applies to files such as:

/etc/fstab

/etc/ssh/sshd_config

/etc/default/grub

/etc/sudoers

/etc/systemd/system/*.service

For a directory:

sudo cp -a /etc/nginx /etc/nginx.backup

The -a option preserves attributes and copies recursively.

For version-controlled configuration, consider Git:

cd /etc/example

sudo git init

sudo git add .

sudo git commit -m "Configuration before change"

Do not place passwords or secret keys into a repository that may later be uploaded elsewhere.

A backup should be easy to identify and easy to restore.

8. Validate Configuration Before Restarting Services

Many applications provide syntax-checking commands.

Use them before reloading or restarting the service.

For Nginx:

sudo nginx -t

For Apache on Debian or Ubuntu:

sudo apache2ctl configtest

For OpenSSH:

sudo sshd -t

For Samba:

testparm

For sudoers:

sudo visudo

Do not edit /etc/sudoers with an ordinary editor when visudo is available. It checks the syntax before saving, reducing the risk of locking out administrative access.

For systemd units:

sudo systemd-analyze verify /etc/systemd/system/example.service

Then reload:

sudo systemctl daemon-reload

For /etc/fstab:

sudo findmnt --verify

sudo mount -a

Only after validation should you restart or reboot.

9. Keep a Recovery Session Open

When changing remote access, firewall rules, networking or storage mounts over SSH, keep a second working session open.

This is especially important when editing:

  • SSH configuration
  • Firewall rules
  • NetworkManager settings
  • Static IP configuration
  • VPN settings
  • /etc/fstab
  • Authentication rules
  • User and group permissions

For example, before reloading SSH:

sudo sshd -t

Keep the existing SSH session open, then start a second session and confirm that you can still connect.

Only after the new session works should you close the original one.

For a remote server, also confirm that you have access to:

  • A cloud-provider console
  • Hypervisor console
  • Physical keyboard and display
  • Out-of-band management
  • Rescue environment

A second session is not a full recovery method, but it can save you from a simple lockout.

10. Stop When the Output Looks Wrong

One of the most valuable terminal habits is recognising when to stop.

If a command produces unexpected output, do not continue entering commands from the same instructions blindly.

Stop and inspect.

Use:

Ctrl+C

to interrupt many running commands.

Then check:

echo $?

This shows the exit status of the previous command.

A value of:

0

usually indicates success.

A non-zero result usually means an error occurred.

Review recent messages:

journalctl -xe

For a specific service:

systemctl status example.service

journalctl -u example.service

For kernel messages:

dmesg | tail -50

Do not treat error messages as clutter. They often tell you exactly what failed.

Continuing after the first unexpected result can turn a small mistake into a larger one.

Bonus Habit: Use Shell Safety Options in Scripts

Interactive habits help humans. Shell safety settings help scripts.

For Bash scripts, a useful starting point is:

#!/usr/bin/env bash


 

set -euo pipefail

These options mean:

  • -e — exit when an unhandled command fails
  • -u — treat unset variables as errors
  • pipefail — fail a pipeline if one of its commands fails

Example:

#!/usr/bin/env bash


 

set -euo pipefail


 

SOURCE="/srv/data"

DESTINATION="/mnt/backup/data"


 

if ! mountpoint -q /mnt/backup; then

    echo "Backup destination is not mounted" >&2

    exit 1

fi


 

rsync -aHAX --delete "$SOURCE/" "$DESTINATION/"

Without the mount check, the script might write the backup to the local root filesystem when the external drive is missing.

Safety options are useful, but they do not replace proper error handling and testing.

Bonus Habit: Use Clear Variable Names

Avoid vague variables such as:

x="/srv/data"

y="/mnt/backup"

Prefer:

SOURCE_DIRECTORY="/srv/data"

BACKUP_DIRECTORY="/mnt/backup"

Clear names make scripts easier to review and reduce the chance of using the wrong path.

Check that required variables are not empty:

if [[ -z "${BACKUP_DIRECTORY:-}" ]]; then

    echo "BACKUP_DIRECTORY is not set" >&2

    exit 1

fi

This matters before destructive commands.

Bonus Habit: Add Safeguards Around Deletion

Before deleting a directory held in a variable, validate it.

Example:

if [[ -z "${TARGET_DIRECTORY:-}" ]]; then

    echo "Target directory is empty" >&2

    exit 1

fi


 

if [[ "$TARGET_DIRECTORY" == "/" ]]; then

    echo "Refusing to operate on /" >&2

    exit 1

fi


 

rm -rf -- "$TARGET_DIRECTORY"

You can also require the directory to begin with an expected path:

case "$TARGET_DIRECTORY" in

    /srv/application/cache/*)

        rm -rf -- "$TARGET_DIRECTORY"

        ;;

    *)

        echo "Unexpected target path: $TARGET_DIRECTORY" >&2

        exit 1

        ;;

esac

A few extra lines can prevent a serious incident.

Bonus Habit: Use Interactive Modes When Appropriate

Some commands support confirmation prompts.

For example:

rm -i file.txt

asks before deleting.

cp -i source destination

asks before overwriting.

mv -i source destination

asks before replacing an existing destination.

For multiple files, -I can be less repetitive than -i:

rm -I *.log

Interactive modes are useful during manual work, although scripts should normally use explicit checks rather than waiting for user input.

Bonus Habit: Learn What Your Aliases Do

Some distributions create protective aliases such as:

alias rm='rm -i'

Check:

alias

and:

type rm

Do not rely on an alias being present.

A script or another system may call the real command without confirmation.

To bypass an alias deliberately:

command rm file.txt

or:

\rm file.txt

Protective aliases are a convenience, not a substitute for careful commands.

Bonus Habit: Avoid Parsing

ls

in Scripts

File names can contain spaces, tabs, line breaks and other unusual characters.

This is fragile:

for file in $(ls *.txt); do

    echo "$file"

done

Use shell globbing:

for file in ./*.txt; do

    [[ -e "$file" ]] || continue

    echo "$file"

done

For more complex searches, use find -print0:

find /path -type f -name '*.txt' -print0 |

while IFS= read -r -d '' file; do

    printf '%s\n' "$file"

done

This safely handles unusual file names.

Bonus Habit: Review Command History Carefully

Command history is useful for repeating known-good work:

history

Search previous commands with:

Ctrl+R

However, do not rerun an old command without reading it.

Your current directory, server, username or environment may have changed.

This is particularly risky:

!!

It repeats the previous command.

And:

sudo !!

repeats the previous command as root.

That can be convenient, but only after confirming exactly what the previous command was.

Bonus Habit: Keep Secrets Out of Commands

Commands may be stored in shell history and visible in process listings.

Avoid placing passwords directly on the command line:

example-command --password SuperSecretPassword

Prefer:

  • Secure prompts
  • Environment files with restrictive permissions
  • Credential stores
  • Standard input where supported
  • Secret-management tools

Check history files:

~/.bash_history

~/.zsh_history

If a secret was entered accidentally, changing or revoking the secret is safer than assuming removing one history line solves the exposure.

Other users or monitoring tools may already have captured the command.

Bonus Habit: Know How to Undo the Change

Before changing a system, know how you will reverse it.

Ask:

  • Which file will be restored?
  • Which package will be reinstalled?
  • Which service will be restarted?
  • Which kernel will be selected?
  • Which firewall rule will be removed?
  • Which snapshot will be restored?

For package changes, review logs such as:

/var/log/apt/history.log

For systemd:

systemctl cat example.service

For Git-managed configuration:

git diff

A change without a rollback plan is a risk, even if the command is correct.

Ten Habits at a Glance

The ten most important habits are:

  1. Confirm your current directory.
  2. Preview destructive actions.
  3. Read commands before running them.
  4. Quote paths and variables.
  5. Review wildcard matches.
  6. Use the least privilege necessary.
  7. Back up important configuration.
  8. Validate before restarting.
  9. Keep a recovery session open.
  10. Stop when the result is unexpected.

None of these habits requires advanced Linux knowledge.

They simply create a pause between intention and execution.

A Safe Workflow for Important Changes

For any high-impact terminal task, use this sequence:

1. Identify the system

hostname

whoami

2. Confirm the location

pwd

3. Inspect the target

ls -la /target/path

4. Back it up

sudo cp -a /target/path /target/path.backup

5. Preview the change

Use a dry-run or test mode.

6. Apply the smallest necessary change

Avoid combining unrelated modifications.

7. Validate the result

Use the application’s configuration test.

8. Keep recovery access open

Especially on remote systems.

9. Review logs

Confirm that the service or command behaved as expected.

10. Document what changed

Record the command, reason and rollback process.

Final Thoughts

The safest Linux administrators are not the people who type the fastest or memorise the most commands.

They are the people who build verification into their routine.

They check the path, preview the result, use the right privilege level, keep backups and stop when something looks wrong.

The terminal is powerful because it is precise. It will follow your instructions even when those instructions contain a mistake.

A few seconds spent checking a path or reading a dry run can prevent hours of recovery work.

Need Help Recovering From a Terminal Mistake?

Hamilton Group can help recover Linux systems after accidental deletions, permission changes, broken configuration, failed updates and remote-access lockouts.

We can restore service, protect important data and put safer administrative processes in place to reduce the risk of the same problem happening again.

Call 0330 043 0069 or visit hgmssp.com to speak with one of our IT specialists.