I have a confession to make:
When I started Software Development, I almost suffered from anxiety each time that I read open the terminal and type in the following command
… 🥲
Coming from a non-tech background, the terminal always seemed something to be used only in movies or by the most senior developers.
But, as I shifted my focus from Android to Flutter, I decided to battle this ol’ fear of mine, and bit by bit I started to see the true power of the command line - by just chaining a couple of commands, I can quickly create a new Flutter project and open it in Visual Studio Code. Or I can create a new folder, with a configuration file inside, and populate it with some code. Or I could find a folder or file in a maze of folders and nested folders.
But here’s the thing - I only saw the true potential of the command line as I started using it daily. And that’s the main focus of this article - to show you how you can start using the terminal with common tasks. Then you can decide if it’s something that’s going to ease your life as a developer or not.
We’re going to dive deep into 5 different use cases, throughout several articles.
- Listing the contents of a directory
- Navigating through folders
- Showing a file’s contents
- Searching for a specific line in a file
- Creating new folders and files
This week we’ll be just focusing on the first chapter - listing the contents of a directory.
1. Listing the contents of a directory
We all faced a scenario similar to this - we have to find a specific folder or file, but it’s buried inside a maze of folders and sub-folders and hidden folders and who knows what else! Since we don’t know where to search, we start our tango dance - open folder, search, go back, open folder, search, open folder, search, go back, go back (and repeat!).
Luckily, the command line provides us with some tools to help us in our search!
But before moving on, let’s take the first step into the unknown - opening a new terminal window.
Currently, I’m using a macOS system, but if you use any Linux-based terminal, such as WSL for Windows, or the Terminal apps for Mac and Linux you will be able to follow along just fine.
In the case of macOS and Linux, we will be presented with a $
symbol or maybe in most cases a ~
. ~
denotes the current directory, and the special character ~
is the root for the current user, where you have the folders for Desktop, Downloads, Videos, and so on. $
states that we are going to start typing in a new command.
Now onto our objective - when we are looking for a specific file or folder, we can be in one of two different scenarios:
- I am looking for something inside the current folder_;
- I don’t know where the file or folder is, but I know it’s inside a subfolder of the current directory.
Let’s go to the first one. Effectively, we want to l
is
t all the files in a folder, and to do that, we have a command called ls
.
(Please note, in the following screenshot I have changed to another folder, we will see in a future article how we can do that!)
As you can see, my terminal is friendly enough to color-code files vs folder - we have files in white, while we have all the folders in blueish color.
PROTIP If we already have our terminal filled with information and we want to clean it all, we can use the
clear
command.
Now, what if I wanted to see the contents in the folder open-source
? We just need to give to the ls
command the path to of the directory ls open-source
And, if we wanted to see what was inside project-web-covid
, we could do it by giving the relative path, which is in this case ls open-source/project-web-covid
.
PRO TIP - in some terminals, you can use the /TAB/ key to give you all the available options and cycle through them, so if I type
ls o
and click on the tab key, it will auto-complete with open-source OR show the list of the available folders.
However, it can be rather boring to do several ls
commands targeting different folders to search for the correct file. Wouldn’t it be best to see a tree
with all the files and folders? 🤔
Well… that’s why we have the tree
command! Let’s give it a shot!
Oof!!! It outputs in everything that’s inside all the nested folders. But at least it’s easier to search than outputting a ls
for every folder.
Still, we are not happy with the result. We know for sure that the folder we are looking for is at a maximum of 2 folders in, not more than that. The question is - how we can pass this information to the tree
command?
If we don’t know how a specific machine works, we read the man
ual, right? In command-line-land, it’s the same thing! We use the man
command before a command: man tree
and a new “window” opens containing all the information:
We are now inside one of the file readers of the command line (for the curious ones, this one is less
), and you can notice two things right away:
- The prompt where we typed our commands disappeared (no
~
or$
) - At the bottom we have a
:
followed by a cursor
And at this point you might panic “oh no! I broke this, how do I get out of here?! LET’S JUST CLOSE THE WINDOW”. Wait. Wait. Breathe in. Breath out. Let’s explore it before trying to run away from it!
First, how do we navigate this file? Well by using the arrow keys up and down on our keyboard!
If we continue to read the file we will reach the Listing Options chapter:
Huh! -d
list only directories! It’s exactly what we were looking for!
Let’s continue a bit further…
Aha! -L
followed by a number will filter by depth!
But wait… how do we q
uit this screen? Easily! Just type the q
letter on your keyboard.
So to reach our objective of “showing only directories, and at maximum 2 levels down”, we just need to use tree -L 2 -d
:
PROTIP: In the terminal, we can chain command, so instead of using
tree -L 2 -d
, we can use only-
and combine both:tree -dL 2
.
And voilá! Now I can easily find the folder that I was looking for!
This is quite a lot for a first introduction to the terminal, but at least now we know how to list the content of a directory and how we can list all the nested folders and files inside it.
As a small exercise, I want you to explore the man
of ls
, and try to find out how you can:
- List hidden folders (folders that start with a
.
in Linux and macOS); - List all the files plus their size.
A Final Note
As a footnote, you may hear the terms terminal, console and shell quite a lot and it’s hard to keep track of what is what, but thankfully Gilles answered this in a brilliant AskUbuntu question, or you can also read Scott Hanselman’s blog post: What’s the difference between a console, a terminal, and a shell?.
If you’re starting you don’t need to care about what shell or terminal you use. Don’t think about it. First, get used to the most simple commands, use them daily, and then you can expand your horizons. However, without getting too much into it, on my laptop I use the following:
- OhMyZsh - It uses
zsh
instead ofbash
, and provides several plugins to make a programmer’s life easier - iTerm2 - my preferred terminal emulator for macOS
As always, I’d like to hear from you!
Did you use the command line before apart from installing new software? Are you curious about using it? And if so, what do you want to do with it?
Tag me on Twitter (and follow me 🤩) - @gonpalma - and share it with the hashtag #FinalFormDev!