Search
Monday 8 July 2024
  • :
  • :

8 Typos You Really Need to Avoid on Linux

Closeup of a keyboard key labelled "Oops!"
Redpixel.pl/Shutterstock
Double-check you’ve typed your command correctly before you press “Enter.” On Linux, a typo can have destructive repercussions. Use tab completion to autofill as much command line entry as you can. Aliases for long and complicated commands are a great idea, too.

The Linux command line delivers great power. The problem is, the correct use of that power hinges on the accuracy of your typing. Here are eight typos you never want to make.

Table of Contents

The Linux Command Line
1. Don’t Forget the -a
2. Using the Wrong Drive Identifier With dd
3. Using the Wrong Drive Identifier With mkfs
4. Don’t Delete Your crontab File
5. Repeating History
6. The Calamity of Spaces
7. Using > Instead of >>
8. Redirection in the Wrong Direction
How to Avoid Command Line Typos

The Linux Command Line

The Linux command line is a portal to great power, but one typo is all it takes for that power to turn against you. We’ve all heard about the command you should never run. What we’re talking about here are commands that you do want to run, but where one slip up can mean disaster.

When you press “Enter”, everything you’ve typed gets processed by the shell. Aliases and variables are expanded. Commands, options, and parameters are identified. This is called parsing. The next step hands your parsed input over to the commands that are going to carry out your instructions.

If you make a mistake when you type your commands, it might be trapped as a syntax error. But if your mistake creates another valid command line, it’ll get executed.

A simple typo can be truly destructive. The level of destruction depends on the command and the mistake. You might lose time. You might lose a file. You might lose an entire file system.

RELATED: What Is the Bash Shell, and Why Is It So Important to Linux?

1. Don’t Forget the -a

You might need to add someone to a group to allow them, say, to use a particular piece of software. For example, VirtualBox requires users to be part of the “vboxusers” group. We can do that with usermod.

The groups command lists a user’s groups.

groups

Listing a user's groups with the groups command

We’ll add user dave to a new group. The -a (append) adds the new group to the list of existing groups the user is in. The -G (groups) option identifies the group.

sudo usermod -a -G vboxusers dave

Adding a user to a new group, using the usermod command

The new group is seen after the user logs in and out.

groups

The vboxusers group showing in the list of groups this user is in

He’s now in “vboxusers” group. However, if you forget to use the -a (append) option, all of the user’s existing groups are removed. The only group they’ll be in, is the new group.

This is the incorrect command:

sudo usermod -G vboxusers dave

Adding a user to a group without using the -a append option

When they log in next, they’ll find they are only in one group.

groups

The user is now in a single group

If you have a single configured user, and you do this to them, you’ll have serious problems. For one thing, the user is no longer a member of the “sudo” group, so you can’t use sudo to start correcting things.

RELATED: Add a User to a Group (or Second Group) on Linux

2. Using the Wrong Drive Identifier With dd

The dd command writes blocks of data to file systems. It is often used to write ISO images to USB memory sticks.

The Linux naming scheme for storage devices uses a single letter for identification. The first hard drive is named “/dev/sda”, the second is “/dev/sdb”, the third is “/dev/sdc”, and so on. Partitions are identified by number. The first partition on the first hard drive is “/dev/sda1”, the second is “/dev/sda2”, and so on.

If you burn an image to a USB memory stick, you need to know the drive identifier of the USB memory stick. We’ll find that by piping lsblk through grep, looking for entries with “sd” in them.

lsblk | grep sd

Using grep to search for lines with "sd" in them, in the output of the lsblk command

We can see that hard drive “/dev/sda” is a 32GB drive with three partitions. One of the partitions is the “/boot” partition, and partition “/dev/sda3” is mounted at “/”, which is the root of the file system.

Hard drive “/dev/sdb” is reported as a 7.5GB drive. It is mounted at “/media/dave/Pink.” Plainly, drive “/dev/sda” is the main hard drive on this computer, and “/dev/sdb” is the USB memory stick.

The command to write an ISO file that’s in the “~/Downloads” directory to our USB memory stick is:

sudo dd bs=4M if=Downloads/distro-image.iso of=/dev/sdb conv=fdatasync status=progress

Writing an image to a USB memory stick with the dd command

We’re prompted for our password, then dd dives into action. There are no “Are you sure?” warnings or chances to back out. The writing starts immediately.

The dd progress display as it writes an image to a USB memory stick

However, if you type the wrong letter for the drive identifier and it matches an existing hard drive, you’ll overwrite that drive instead of the memory stick.

This is the incorrect command:

sudo dd bs=4M if=Downloads/distro-image.iso of=/dev/sda conv=fdatasync status=progress

Mistakenly writing an image to the main hard drive ends with an error message

We told dd to use “/dev/sda“, so it did. The write action is much faster, but ends with a warning. You’ve just trashed your Linux installation.

Check and double-check drive identifiers before hitting “Enter.”

RELATED: How to Burn an ISO File to a USB Drive in Linux

3. Using the Wrong Drive Identifier With mkfs

There are other commands that take drive identifiers as part of their command line, such as the mkfs tools. These format drives by creating file systems on partitions.

On this computer we have a 25GB drive and a 10GB drive.

The output of lsblk piped through grep, displaying the hard drives in a computer

If we want to create an Ext4 file system on the first partition of the 10GB drive, we’d use these commands.

sudo umount /dev/sdb1
sudo mkfs.ext4 /dev/sdb1

Creating an ext4 file system on a USB memory stick

But if we make the mistake of using an “a” instead of a “b” in the drive identifier, we’ll wipe out one of the partitions on the 25GB drive and render our computer unbootable.

This is the incorrect command:

sudo umount /dev/sda1
sudo mkfs.ext4 /dev/sda1

Mistakenly formatting the main drive of a computer

That one little letter packs a punch, so make sure you’re hitting the right drive.

RELATED: How to Use the mkfs Command on Linux

4. Don’t Delete Your crontab File

The cron daemon runs tasks at predetermined times for you. It takes its configuration from a crontab file. Each user—including root—can have a crontab file. To edit your crontab, use this command:

crontab -e

The command to edit your crontab file: crontab -e

The crontab file is opened in an editor. You can make changes and add new commands.

A crontab file opened in an editor

But if you mistype the command and hit “r” instead of “e”, you’ll remove—as in delete—your crontab file.

This is the incorrect command:

crontab -r

The command that deletes your crontab file: crontab -r

The next time you use the crontab -e command, you’ll see a default, empty file.

This is an easy mistake to make, because “e” and “r” are next door to each other on most keyboards. Rebuilding a complicated crontab file is no fun.

RELATED: What is a Cron Job, and How Do You Use Them?

5. Repeating History

Using the history command is great when you’re trying to reduce keystrokes and save time. If you can pull a long-winded command out of history, you gain speed and accuracy. As long as you select the right command from your history.

The history command lists your previous commands in the terminal window. They’re numbered. To re-use a command, precede its number with an exclamation mark “!“, and hit the “Enter” key.

history

Suppose we’ve cloned a Git repository, got into a mess with it, and deleted it. We need to clone it once more. By scrolling in the terminal window, we can soon spot the git clone command. We can re-run it by typing:

!60

But if we’ve only glanced at the screen and misread the number, we might pick the next number in error:

!61

That runs the next command in the list, rm *. That deletes all files in your current directory.

You can also use the “!” exclamation mark with a string of text. The first matching command is executed for you. It isn’t displayed so that you can check it’s the one you were thinking of, it is executed immediately.

Imagine the scenario where you have a script called “restart.sh”. This script defaults a set of config files for some software you’re writing. Periodically, as you develop and test,you need to wipe the slate clean, so you call your script.

This command should be enough to find and match the command in your history, and to execute it.

!re

But if you’ve used the reboot command since you last used your script, it’s the reboot command that is found and immediately executed.

The rm * command being run by mistake

On your single-user home computer that is probably just an annoyance. On a shared server it’s an annoyance for many other people, too.

RELATED: How to Use the history Command on Linux

6. The Calamity of Spaces

Spaces in filenames and directory paths can cause havoc. That’s why they should always be escaped or quoted.

How to Deal With Spaces in Filenames on Linux

RELATEDHow to Deal With Spaces in Filenames on Linux

Issues with spaces can be avoided by using tab completion. Press the “Tab” key when you’re typing a filename or directory path and the shell will auto-complete as much of the path or filename that it can. You might need to type a letter to differentiate between the file you want and any other files that share part of the same name, but another press of the “Tab” key will complete the remainder of the filename for you.

This saves on keystrokes, prevents spaces from creeping in because of typos, and correctly escapes any legitimate spaces so that they don’t cause issues.

Let’s say we have a “Development” directory that contains two other directories, “geocoder” and “bin.” There’s also a “bin” directory inside the “geocoder” directory.

To delete the files in the “geocoder/bin” directory and remove the directory, you’d use this command.

rm -r geocoder/bin

Deleting the geocoder/bin directory

Now imagine you inadvertently added a space after “geocoder/”, like this.

This is the incorrect command:

rm -r geocoder/ bin

Mistakenly deleting the geocoder and bin directories

Boom. The “Development” directory is now empty. The “Development/geocoder”, “Development/geocoder/bin”, and “Development/bin” directories have all been completely wiped out.

Remember, tab completion is your friend.

RELATED: Use Tab Completion to Type Commands Faster on Any Operating System

7. Using > Instead of >>

Redirection sends the output of a process to a file. We use the greater-than sign “>” to capture output from a process. If the file exists, it is emptied first.

Let’s say we’re investigating a memory leak. We’ve got a script called “memlog.sh.” It displays memory statistics once per second. We’re going to redirect that into a file called “memory.txt”, for later analysis.

memlog.sh > memory.txt
head memory.txt

Capturing and displaying the output from the memlog.sh script

Next day, we want to continue with our investigation, and we restart the script. This time we need to use two greater-than signs “>>” so that the new data is appended to the file.

memlog.sh >> memory.txt

Appending new data to the end of an existing file

If we use a single greater-than sign “>”, we’ll lose yesterday’s data because the file is emptied first.

RELATED: What Are stdin, stdout, and stderr on Linux?

8. Redirection in the Wrong Direction

Redirection can use the contents of a file as input to a program.

We have a file called “placenames.sql” that we want to import into sqlite3. The schema file describes how to recreate the database tables. It also holds the data that we want stored in the database. At 1.3GB and over 11 million lines, it’s a big file.

ls -hl placenames.sql
wc placenames.sql

Checking the size of a file with ls and wc

We can create a new database called “places.sqlite3” with this command.

sqlite3 places.sqlite3 < placenames.sql

Importing a schema from a file into sqlite3

More often than not, when we’re redirecting we use the “>” character. You must concentrate to avoid typing “>” out of habit. If you do, whatever output sqlite3 generates is written to your schema file, obliterating it.

This is the incorrect command:

sqlite3 places.sqlite3 > placenames.sql

Mistakenly redirecting into a schema file from sqlite3

Our schema file has been destroyed, overwritten by the welcome message from the sqlite3 shell.

cat placenames.sql

The overwritten schema file containing the sqlite3 welcome message

Bye bye, 1.3GB of data.

How to Avoid Command Line Typos

There are good habits that you can adopt to help avoid making these types of mistakes.

Use tab completion where ever possible. You’ll avoid issues with spaces in directory paths and filenames.

Create your own short and memorable aliases for long, complicated commands that you occasionally need to use. That way, you won’t mess up by using the wrong options and parameters.

How to Type Less and Work Faster in the Linux Terminal

RELATEDHow to Type Less and Work Faster in the Linux Terminal

It’s notoriously difficult to proof-read your own writing, but that’s what you need to do on the command line. Read what’s really there. Don’t just glance at it and think it says what you meant to type. What does it really say? Because that’s what it’s really going to do.

RELATED: 8 Deadly Commands You Should Never Run on Linux




Leave a Reply