6 Linux terminal mistakes beginners make

Configurare noua (How To)

Situatie

The Linux terminal open on the MALIBAL Aon S1 displaying an echo command

Why Use the Command Line?

The command line came long before graphical desktop environments appeared. It was the primary way to use and administer Unix, the operating system that Linux is based on.

There are hundreds of commands available on the Linux command line, and many of these have dozens of options. This delivers granularity, power, speed, and flexibility. The trouble is, that blinking cursor in a terminal window isn’t welcoming, and the prospect of learning all of those commands and options can be overwhelming to the newcomer.

Solutie

The command line is a way to interact with a computer by typing text-based instructions to it, and reading the responses.

The software—called an interpreter—that translates your input into commands and executes them for you is the shell. On most Linux distributions, the default shell is Bash, or Zsh.

If your Linux installation has a graphical desktop environment such as GNOME or KDE, you’ll need to open a terminal window to access the command line. Strictly speaking these are terminal emulation windows, but hardly anyone bothers to call them that.

Originally, a terminal was a teletype that used a paper roll to display what you typed, and to print out the computer’s response. These were replaced by cathode-ray tube (CRT) terminals that did away with the paper. Your terminal window is pretending to be one of these old-school CRT terminals. Inside the terminal there’s the shell, and the shell provides the command line.

All you need to remember is, if someone tells you type something on the command line, or execute a shell command, or to type something in a terminal window, they all mean the same thing.

Open a terminal window, and type at the command prompt.

1. Don’t Expect Consistency

Most commands accept command line options or flags that modify their behavior. But don’t expect an option to mean the same thing from one command to the next. For example, with grep , the -i option means ignore case. But to ls , it means display the inode number of each listed file.

man ls

The man page for ls with the -i option highlighted

At the time of writing, Linux is over 30 years old and Unix is in its mid-50s. Things have evolved over the years. In addition to two tools using the same command line option differently, you can find different releases of the same tool that have different functionality and options.

If you see different behaviors when you use the same command on different computers, check the software version of the tool. One option that is almost universal is the --version flag. This tells the tool to print its release number to the terminal window.

grep --version

Using the grep --version option to print its version number to the terminal window

Researching a tool’s options might sound like a job for the internet. The trouble is, the information you find may be for any vintage of the tool. The man pages on your computer are guaranteed to be accurate. They’ll match exactly with the versions of the tools and commands installed on your computer.

Traditionally, man pages are designed to be succinct, with no real concession to user-friendliness. Reading and interpreting man pages is a skill in itself. But, if you intend to hang around the command line, it’s one you need to start developing sooner rather than later.

The validity of online resources change over time. Unfortunately, articles on the internet don’t come with an expiry date. If you’re following an online tutorial, check the date it was published.

Also, check which Linux distribution and version it was written for. For example, most modern distributions use systemd now, so following a tutorial that describes its predecessor, the System V init daemon, is pointless.

2. Don’t Believe Everything You Read

Even if online information is still current, it may be factually incorrect. If you look for help on well-respected websites you’re more likely to get quality information. Look for articles with explanation, not just instructions. Step by step screenshots are a good sign. It means the process has been followed by the author and it worked. If you’re in doubt, cross-reference the information with another reputable site.

Bad people lay cut-and-paste traps for the unwary. They want newcomers to use destructive commands that either delete data or crash your system. If you’re offered a command that allegedly does what you want, but you don’t know how it works, drop it in the command parser at ExplainShell.com.

The web site breaks the command down for you and explains what each piece does. Instead of copy and pasting an impenetrable one-liner to your terminal window and hoping for the best, take the sensible step of having that command parsed and explained.

Of course, it isn’t just malicious commands that you need to watch out for. Innocent commands issued in the wrong order, or a sequence of commands that accidentally miss a step, can wreak havoc on your computer too.

It can be as innocuous as forgetting a step telling you to cd back to your home directory. Subsequent commands will be executed in the wrong place in file system. The best you can hope for is they don’t work and you see error messages, but you’re just as likely to do something you’ll regret.

3. Don’t Log In as root

Whether you’re fed up of entering sudo or not, logging in as root is never a good idea. The whole purpose of sudo is to prevent just that. It means you only get root’s special privileges for the duration you need them, and no more. Whatever degree of damage you can do as a regular user, it’s nothing compared to what a moment’s absent mindedness as root can do.

You might think you’re careful enough not to make mistakes, but the mistake might not be yours. If there’s an error in a script or program you’re using, the damage it can do when launched by root is unlimited. root can do anything on the system, and so can the processes launched by root.

And if you walk away from your computer for a moment and don’t hit “Super+L” to lock your desktop, anyone who sits down at your keyboard has root access to your computer.

Logging in as root? Just say no.

4. Don’t Struggle – Work Smarter, Not Harder

The commands you type have to be letter perfect. Typos can cause unexpected things to happen. For example, if you mean to type ln but type nl, it’s nl that runs, whether it makes sense or not.

Of course, you need the know name of the command before you can type it. Thankfully, you don’t need to to carry them all in your head.

If you know what you want to do but can’t remember the command you need, use apropos. Typing apropos followed by whatever you’re interested in, like “printing” or “remote connection”, lists all the commands associated with that topic.

apropos printing

The output from the apropos printing command

You can pipe the output through grep to filter the results, or into less to scroll and search through them. To see how to use a particular command, use man to open its man page, or use the --help command line option.

man lp

The top of the man page for the lp command

lp --help

Using the --help option with the lp command

Use Aliases and Shell Functions

Create aliases or shell functions for commonly used or unwieldy command sequences. Give them memorable, meaningful names, so they’re easy to recall. You’ll save on keystrokes and avoid looking up command syntax and command line options.

Use the Shell History Feature

If you’ve used a command, it’ll be stored in your shell’s history to be used again. You can scroll through your history using the Up Arrow and Down Arrow keys. Hitting Enter executes the currently displayed command.

Search the History

You can also search through your history using the Ctrl+R key combination. Type the first few letters of a command and press Ctrl+R. If you’ve used a command that matches your search clue, it’ll be displayed. If it isn’t the command you’re looking for—perhaps it is the right command but the wrong parameters—press the Ctrl+R key combination to jump to the next match.

Pressing Enter executes the displayed command. To put the command on the command line so you can edit it, press the Left Arrow or Right Arrow keys.

5. Don’t Use a Difficult Editor

Hot take: ignore vim and emacs, at least for now. You’ve got enough to learn without having to master either of these powerful editors at the same time.

Make life easier for yourself, and use a simple editor that behaves like you’d expect, and only take a few minutes to understand. Then you can focus on the thing you’re editing, instead of wrestling with the editor.

You’ll have no problems using either nano or micro. They’re great little editors, and they work in a terminal window. Of the two, micro is the easiest, and has more functionality than nano. It uses familiar keyboard shortcuts like “Ctrl+s” for save, and “Ctrl+q” for quit. On the other hand, nano is likely to be preinstalled for you.

6. Don’t Settle for Bad Workarounds

If you can’t make something work, persevere and find the correct solution. Don’t put a clunky workaround in place just to get the result you need. That can lead to security holes or system instabilities.

A good example of bad practice is setting the permissions for entire directories to 777 just to get something to run. It might seem like a solution when you’re frustrated and out of time, but you should really figure out the permissions you need and do the job properly. Setting directories and files to have permissions with the flag 777 means anyone on the computer can read, write, and execute those files, and create and delete files in the directories.

Tip solutie

Permanent

Voteaza

(6 din 15 persoane apreciaza acest articol)

Despre Autor

Leave A Comment?