Command Line Basics Guide for UNIX-like Systems

Posted 2024-06-29, last edited 2024-10-11

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.

whoami shows the current user that is logged in.

pwd stands for 'print working directory' and it shows what directory you're currently in.

ls stands for list and it lists files and directories located in your current working directory.

cat stands for concatenate and it prints the contents of a file to the terminal.

cd stands for 'change directory' and as it would suggest; it changes the current directory you're in.

touch creates a file named whatever is passed to it, i.e. touch foo will create a file named foo.

rm stands for remove and it simply removes a file that has been passed to it, i.e. rm bar will remove the bar file.

mkdir stands for 'make directory' and as it would suggest; it creates a directory with the name of the passing argument, i.e. mkdir baz will make a directory named baz.

mv stands for move and it does what it says on the tin; it moves files or directories to another location, and it takes two arguments, and can be used to rename files or directories, i.e. mv foo/ bar/ will rename the foo directory, to bar. Same can be done for files!

sudo executes the following command with root privileges.

\_ 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:

ls lists the current directory's contents.

-a stands for all and just lists everything in a directory, including hidden files, which are also referred to as dotfiles.

-l stands for list which will put everything in a neat long list with a bunch of extra information aside every file/directory.

| is the pipe we're covering now, just funnels the output of the command on the left into the input of the command on the right.

less is the pager program that makes the output scrollable.

\_ 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

ping sends a signal to a specified web address or IP address, to check if it's online. This command is often used to check if there's internet access.

-c 3 tells the ping command to only send a signal three times. You can change the number to be any number you want.

gnu.org is the web address or IP address, that we want to ping.

; tells the shell to run the next command no matter if the previous command failed or succeeded.

ls lists all files and directories in the current directory.

\_ 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.