Command Line Basics Guide for UNIX-like Systems

Overview

Preface

This guide assumes you are on a UNIX-like system, such as any flavor of Linux, BSD or OS X/MacOS!

Terminology

Booting Up Your Terminal Emulator

UNIX-like systems usually have a terminal emulator installed, so search up terminal through your system menu, to find the pre-installed terminal. If you don't find a terminal, search the internet for a terminal emulator for your UNIX-like environment; it may be under a different name (other than terminal), or one may not even be installed, in which case you will have to find one to install. I recommend alacritty, as it's fast and cross-platform!

After launching your terminal emulator, you can start typing some commands and enter them with the Return/Enter key. The next section will cover the must-know commands.

Essential Commands

Below is a list of commands and a brief description of what they do. Try them out! Experiment with them so you commit them to memory.

\_ Command Flags

Below you will read some commands that are prefixed with a dollar sign, and that dollar sign is there to indicate you can use the command is a normal user, i.e. not as root. A command that requires root privileges (or sudo) will be prefixed with a number sign. These two signs are not a part of the command itself, and are to be ignored!

Sometimes a command expects (or requires) a flag, and an easy example is the -r flag that works with the rm command:

$ rm -r baz/

The above command will remove the directory named baz, and it works recursively so it will remove anything contained within the baz directory! For more information on different flags, read the Getting Help and Consulting the Man(ual) sections.

\_ Piping Commands

One thing you may be wondering is can we filter the output of a command? This is where grep comes in. We pipe something like ls into grep as such:

$ ls | grep hello

This will only show files and directories containing hello. It doesn't have to be just one word, it can be multiple words, but we must enclose it in quotation marks like so:

$ ls | grep "hello world"

Like I said above, this will only show files and directories containing the phrase hello world.

Another thing that is commonly used when piping commands together is the less command, which is a pager that is typically used when viewing man pages, which will be covered later. Piping a command into the less makes the output scrollable:

ls -a -l | less

A quick walk-through of this command:

\_ Redirecting Output

You can redirect the output of a command into something else; like a file:

$ cat some_file.txt > another_file.txt

This will take the output of cat, and in this case, it will copy the contents of some_file.txt to another_file.txt and if another_file.txt doesn't exist, it will be created.

Notice the single > this means overwrite. If we use two greater than signs instead of one, like such:

$ cat some_file.txt >> another_file.txt

The >> means append the output of cat some_file.txt to another_file.txt. So we just stick on the contents onto the contents of the other file, without deleting anything.

\_ Chaining Commands Together

Sometimes we need to execute some commands one after another, and there are multiple ways to do this, we can either type in one command; wait for it to finish; then type in the next, or we can do something like this:

$ ping -c 3 gnu.org ; ls

\_ Standard Key Combinations

Sometimes we need to cancel an executed command, such as ping, to do this we can use Ctrl+c.

This means we press Ctrl and c together, at the same time. This will interrupt whatever command is currently executing!

We can also use Ctrl+d to which is useful for quickly exiting CLI applications or quickly closing the terminal.

Getting Help

If you ever need some quick, basic help for a specific command, passing either the -h flag or the --help flag, will typically show you some helpful information on how to use the specified command.

Consulting the Man(ual)

An essential command to know is the man command! It will spit out a manual page, on any command that has a man page, including man itself! Try it out:

$ man man

If we wanted to know about the less command:

$ man less

Closing Notes

Thank you for reading this blog post!

Was this useful? Perhaps.