All you need to know about Terminal as a programmer (Mac and Linux)

Denis Godovanets
3 min readMar 12, 2019

--

Terminal is a useful tool for software engineering, so it’s important to know at least its basics. In this tutorial we will take a look at navigation in terminal, use some simple commands and investigate usage of arguments. Let’s get started!

Finding a terminal in your OS

If you’re using Linux distribution, the program can be called “Terminal”, “Command line” or “Command prompt”. You can find it in your applications list.

If you’re using OS X, seek for the “Terminal” in your apps list.

Command structure

Here’s a basic structure of a command:

command argument1 argument2 ...

Let’s take a look at a specific example:

ls --help

The command ls stands for “list”. We pass --help argument to the command. Usually it means that we want to have printed some manual about usage of the command. After entering the command, let’s what we get:

denis@Ideapad:~$ ls --help
Usage: ls [OPTION]... [FILE]...
List information about the FILEs (the current directory by default).
Sort entries alphabetically if none of -cftuvSUX nor --sort is specified.

Mandatory arguments to long options are mandatory for short options too.
-a, --all do not ignore entries starting with .
-A, --almost-all do not list implied . and ..
--author with -l, print the author of each file
-b, --escape print C-style escapes for nongraphic characters
--block-size=SIZE scale sizes by SIZE before printing them; e.g.,

It prints out a lot of stuff, we might not need 90% of it. But let’s take a look what they offer to us.

On the top of the output, there’s a short description of what the command does, followed by a list and description for the each argument we can pass.

Notice that often arguments have shortened versions, like -a for --all.

It’s also fine to use a command without passing any arguments at all. Let’s try:

denis@Ideapad:~$ ls
Downloads Music Public Videos
Desktop Documents Templates

I suggest you to play with the command on your own, to get used to using terminal.

Navigation in Terminal

As you already learned, you can use ls command. It lists all folders and files in the current directory. The start point of the Terminal is usually a home folder on your machine (marked as tilde ~).

Next, you might want to switch to another folders. You can do this with cd command which stands for “change directory”. Here’s several use cases. Step into a directory:

denis@Ideapad:~$ cd Music
denis@Ideapad:~/Music$

Go to a previous directory:

denis@Ideapad:~/Music$ cd ..
denis@Ideapad:~$

Also, you can combine the two usages:

denis@Ideapad:~$ cd Music
denis@Ideapad:~/Music$ cd ../Documents
denis@Ideapad:~/Documents$

Instead of writing just a folder name, you can specify a full path (relatively to a current directory):

denis@Ideapad:~$ cd Documents/Reports/
denis@Ideapad:~/Documents/Reports$

Basic operations

Let’s create a directory, using mkdir command, which stands for “make directory”:

denis@Ideapad:~$ mkdir testDir
denis@Ideapad:~$ cd testDir
denis@Ideapad:~/testDir$

Now let’s make a file, using touch command:

denis@Ideapad:~/testDir$ touch example.txt
denis@Ideapad:~/testDir$ ls
example.txt
denis@Ideapad:~/testDir$

To remove files, you can use rm command:

denis@Ideapad:~/testDir$ ls
example.txt
denis@Ideapad:~/testDir$ rm example.txt
denis@Ideapad:~/testDir$ ls
denis@Ideapad:~/testDir$

To remove folders, use rm command with -r flag:

denis@Ideapad:~/testDir$ cd ..
denis@Ideapad:~$ rm testDir
rm: cannot remove 'testDir': Is a directory
denis@Ideapad:~$ rm -r testDir
denis@Ideapad:~$

Tips and suggestions

Some commands may need admin privileges to execute. To achieve that, use sudo before the command (it will ask you your user password). For example, it may be needed to remove some protected file:

denis@Ideapad:~$ sudo rm someProtectedFile
[sudo] password for denis:
denis@Ideapad:~$

To not type whole filenames or directory titles, you can press TAB when you started entering, the Terminal will try to autocomplete. It can save your time.

Summary

So, here’s all what you need to know to work with Terminal. If it seems too much, for the first time, it’s fine. No need to try to memorize all the commands. You can bookmark the page or Google next time. The commands are easy and you will be comfortable after using them several times. Have luck with Terminal, subscribe to the blog and see you in the next articles!

--

--