Hopefully, you have seen a command line interface, CLI, before. You can find them on nearly every system: Windows, Linux, macOS, and even my old Amiga had a CLI. Each of them work slightly different, but there is one standard which has come to dominate, which is based on the Unix operating system. Both Linux and macOS derive from the Unix operating system. When interacting with a CLI on Linux or Mac, you will use mostly the same Unix commands. Unix command line interfaces have also become popular on Windows the last few years through Windows Subsystem for Linux (WSL).
You interact with the Unix command line through a program, usually called terminal or console. Here is an example of using iTerm2 on my Mac. In this particular example, I am issuing commands to look the contents of the Godot
directory, where I have stored some games I have developed.
If you use a graphical file manager such as Finder, this Godot
directory would have looked as follows:
Why should you learn how to use a Unix command line interface? Isn't it much easier to just click around in a file manager? Indeed, graphical user interfaces tend to be easier to explore and learn. But text-based interfaces have numerous advantages which are hard to replicate in graphical user interfaces:
They often prove a lot more power and flexibility.
Complex graphical interfaces are harder to learn than complex text-based interfaces.
Text-based interfaces naturally lend themselves to automation, which can be a big time saver.
The second point requires some justification. It is true that it is easier to understand a graphical user interface, but only up to a point. Large and complex graphical applications can be hard to learn. What to do is not obvious. Finding the right buttons to click or dialogs to open may require a video or series of images with careful step-by-step instructions.
Text-based interfaces in contrast are straightforward to explain in articles such as this one. Complex operations which you easily forget how to do, so you can easily copy to a notebook and write an explanation. These notes can later easily be retrieved using search tools. Searching images of a graphical UI is much harder.
In this article, I will teach you how to do the following things:
Navigate around the file system just as you would do with a graphical file manager.
Look at what files are in a directory and filter out files or directories you are not interested in.
Do operations on files.
Explain the binary search path. It is used by the terminal to locate the command you write.
What are environment variables, and what can you use them for?
Work with Files and Directories
When you start the Unix command line, you will get some kind of greeting. It will differ depending on the system and configuration you have. Mine looks like this:
Last login: Thu Jun 23 22:10:30 on ttys001
Welcome to fish, the friendly interactive shell
Type help for instructions on how to use fish
~
❯
You will have a cursor next to a prompt, indicating that the CLI is waiting for you to type a command and press enter. The prompt at the start of the line is usually one of the symbols $
, #
or ❯
. Mine is a ❯
because I am using the Starship prompt. It adds several cool features, but you don't have to use it.
Let us look at the most basic commands ls
, cd
, pwd
, man
, cat
, touch
and file
.
LS - LIST DIRECTORY CONTENTS
With the ls
command, you can see what files and directories are in a directory, just like you can with a file manager.
❯ ls /bin
[ expr pwd
bash hostname rm
cat kill rmdir
chmod ksh sh
cp launchctl sleep
csh link stty
dash ln sync
date ls tcsh
dd mkdir test
df mv unlink
echo pax wait4path
ed ps zsh
❯
This shows the contents of the bin
directory on a Unix system. That is where Unix keeps binaries for the most common commands. We call a file containing a program you can run for binary.
The ls
command is smarter than your file manager. You can do many neat tricks. Characters such as *
and ?
get interpreted in a special way. They form what we call glob patterns. You can use glob patterns to filter what files are show. The *
matches any number of letters while ?
matches a single letter. So a?
would match files named ab
, ab
, ad
, ae
and so on, while *.txt
would match files named foo.txt
, bar.txt
, baz,txt
and so on. What is up with the foo
and bar
names? Those a just silly names we tend to use in programming and Unix circles. As soon as you see the words foo
, bar
, qux
or baz
you should know that they are placeholders. The specific word used isn't important. It could be anything.
Keep reading with a 7-day free trial
Subscribe to Erik Explores to keep reading this post and get 7 days of free access to the full post archives.