Quick Links
Summary
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.
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,VirtualBoxrequires users to be part of the “vboxusers” group. We can do that with
commandlists a user’s groups.
We’ll add userdaveto 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.
The new group is seen after the user logs in and out.
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 theincorrectcommand:
When they log in next, they’ll find they are only in one 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 ofthe “sudo” group, so you can’tusesudoto start correcting things.
Related:Add a User to a Group (or Second Group) on Linux
2. Using the Wrong Drive Identifier With dd
Theddcommand writes blocks of data to file systems. It is often used to writeISO imagestoUSB memory sticks.
The Linux naming scheme for storage devices uses a single letter for identification. The firsthard driveis 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 pipinglsblkthroughgrep, looking for entries with “sd” in them.
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:
We’re prompted for our password, thendddives into action. There are no “Are you sure?” warnings or chances to back out. The writing starts immediately.
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.
We toldddto 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 asthemkfstools. These format drives by creating file systems on partitions.
On this computer we have a 25GB drive and a 10GB drive.
If we want to create anExt4 file systemon the first partition of the 10GB drive, we’d use these commands.
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.
That one little letter packs a punch, so verify you’re hitting the right drive.
Related:How to Use the mkfs Command on Linux
4. Don’t Delete Your crontab File
Thecrondaemon runs tasks at predetermined times for you. It takes its configuration from acrontabfile. Each user—including root—can haveacrontabfile. To edit yourcrontab, use this command:
Thecrontabfile is opened in aneditor. you may make changes and add new commands.
But if you mistype the command and hit “r” instead of “e”, you’ll remove—as in delete—yourcrontabfile.
The next time you use thecrontab -ecommand, 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 mostkeyboards. Rebuilding a complicatedcrontabfile is no fun.
5. Repeating History
Using thehistorycommand 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.
Thehistorycommandlists 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.
Suppose we’vecloned 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 thegit clonecommand. We can re-run it by typing:
But if we’ve only glanced at the screen and misread the number, we might pick the next number in error:
That runs the next command in the list,rm *. Thatdeletes all filesin 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 ascriptcalled “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.
But if you’ve usedtherebootcommandsince you last used your script, it’s therebootcommand that is found and immediately executed.
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.
Related:How 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 willcomplete 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.
Now imagine you inadvertently added a space after “geocoder/”, like this.
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
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 amemory 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.
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.
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 intosqlite3. 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.
We can create a new database called “places.sqlite3” with this command.
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 outputsqlite3generates is written to your schema file, obliterating it.
Our schema file has been destroyed, overwritten by the welcome message from thesqlite3shell.
Bye bye, 1.3GB of data.
How to Avoid Command Line Typos
There are good habits that you’re able to 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 memorablealiasesfor long, complicated commands that you occasionally need to use. That way, you won’t mess up by using the wrong options and parameters.
Related:How 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.