Image of a keyboard

Top 5 mistakes every new Linux terminal user makes

0

Learning to use the terminal is an important step in becoming a true power user of Linux, but it’s easy (and normal) to make mistakes along the way. Here are the top 5 mistakes new terminal users make, and what you can learn from them.

1. Current working directory

When you first open a terminal, your current working directory is your home folder. You have access to all those directories you see in your home directory every time you open a file manager (Desktop, Documents, Downloads, Music, Pictures, and Videos). You can verify your location with the pwd command:

$ pwd
/home/seth

You can list the files and folders within your current directory with the ls or dir or tree commands:

$ ls
Desktop  Documents  Downloads
Music    Pictures   Videos

But you don’t usually stay in one place while using the terminal. You frequently move from folder to folder so you can open or modify or edit files. It’s easy to get lost, forgetting what directory you’re in and what files are around you.

Lesson learned: When working in the terminal, it’s important to regularly verify your current working directory with pwd so you don’t accidentally issue a command you intended to run in a different location.

2. Use interactive options when using wildcards

Wildcards are great shorthand to make command entry faster, and to perform bulk actions on lots of files. However, they can be dangerous when you get them wrong.
It’s easy to process hundreds of the wrong files by using a wildcard in the wrong directory, or by using a wildcard that’s too broad.

For example, suppose you want to run a sed command on all HTML files in a directory, so you run this:

$ sed --in-place 's/day/night/g' *ml

Job done, until you find out that you accidentally ran that command on all your XML files, too.

Lesson learned: Run a safe test command on the wildcard you think you want to target before making a change. Some commands have a literal --dry-run option. Others have an --interactive option that forces the command to prompt you to confirm that you want to carry out the action. Sometimes the logic is reversed: a command refuses to make a major change unless you use a command (for example, sed doesn’t write changes to a file without the --in-place option or redirection).

When in doubt, improvise. You can always “expand” a wildcard using the echo command:

$ echo ./*ml
./four.html ./one.xml ./three.html ./two.xml
$ echo ./*tml
./four.html ./three.html

3. File paths

Many new terminal users don’t understand where files are located within the file system. It’s not a common mistake to make on the desktop because there are visual reminders there. You wouldn’t try to double-click on a document to open it if there was no icon to double-click. It’s easy to assume that the terminal application contains all your files all at once, but the terminal is, by design, limited in scope. Were the terminal to have access to every file everywhere on your system all at once, you’d end up accidentally renaming and moving and copying a lot more files than intended. Specificity is a super power, and it’s defined by the file path.

A file path describes where a file exists in a file system. A full (or “absolute”) file path always starts from the single folder at the start of your operating system, indicated by just a /, and then lists each folder within that folder until it traces the path to a specific file. For example, I have a file called IMG_0001.JPG in my Pictures directory. You probably have a mental image of where that file is and how you’d get there on the desktop. But for the terminal to understand how to find it, the location must be expressed as /home/seth/Pictures/IMG_0001.JPG.

An absolute file path is definitive. The terminal always understands an absolute file path, no matter what your current working directory is.

The absolute path to a file can be unwieldy, though. Once you understand absolute paths, you can abbreviate any path to a relative file path.

A relative file path is based on your current location in the terminal. As long as you’re in the Pictures folder, the full path /home/seth/Pictures/IMG_0001.JPG can be shortened to just IMG_0001.JPG, or ./IMG_0001.JPG for added clarity (the . indicates no movement from your current location, and the / is a directory separator as usual).

But suppose your current working directory was your home directory. Your Pictures folder is located in your home directory, so to get to IMG_0001.JPG you have to enter Pictures first. The relative path in that case is ./Pictures/IMG_0001.JPG or just Pictures/IMG_0001.JPG.

Lesson learned: An absolute file path always starts from the start of a file system. A relative file path changes based on your location. The terminal understands both. For new users, the absolute file path is the most explicit and exact way to reference a file, so practice using them until you’re comfortable with the concept of file paths.

4. Executable permissions

By default, most files aren’t executable. You can’t run them like an application, because most files are meant to be opened in an application. That’s not true for shell scripts, though.

Shell scripts are text files containing a list of commands, and they’re meant to be run like an application. They’re a powerful way to string existing commands together to form a new custom command. However, because a shell script starts out as a regular text file, it’s not seen by your terminal as an executable entity.

To execute a file as an application, you can grant it executable permission with the chmod command:

$ chmod +x ./example.sh

Alternatively, you can run the file in a sub-shell:

$ bash ./example.sh

Notice that in these examples, I use the ./ notation as if the example.sh shell script exists in my current directory.

5. Typing errors

It sometimes feels like the more you type, the more you’re getting done. In a terminal, though, typing too much is one of the best ways to introduce mistakes.

When you try to type a long and complex command, you’re liable to spell something wrong or use the wrong option. When you try to type a filename or a file path, you might forget to escape special characters (like spaces). The errors aren’t usually catastrophic, but they’re frustrating and time consuming.

Lesson learned: There are several ways to ensure you’re entering the correct commands into your terminal:

  • Copy and paste: If you’re using a command you found on a trusted website, copy it in your browser and then paste it into your terminal using Ctrl+Shift+V or right-click and select Paste.
  • TAB: You can type part of a command or file path, and then press the TAB key on your keyboard for either auto-completion or for suggested completions. Use it even when you don’t think you need it. It’ll save you errors every single time, even when it appears to not work (hint: it’s not working because you’re trying to auto-complete something that’s not where you think it is).
  • Drag-and-drop: It’s the 21st century! You can drag a file or folder from anywhere on your computer, and drop it into your terminal. It gets replaced by its absolute path automatically.

Practice makes perfect

To get good in the terminal, you have to use it as often as you can. You don’t have to use it for “serious” work at first, and you arguably shouldn’t, but you can and should do simple exercises in the terminal. Understand file paths, get used to wildcards, learn shortcuts, use the TAB key. The biggest mistake you can make when learning the terminal is to not use the terminal, so open it up every day, do your exercise, and you’ll be an expert in the terminal in no time.