Why I learned the Linux command line as a developer, and you should too

nicolasbeauvais

Nicolas Beauvais

Posted on July 26, 2022

Why I learned the Linux command line as a developer, and you should too

It’s always intimidating to find yourself in front of a command line when beginning your journey as a developer. We have the chance to live in a world where you can choose to do pretty much anything from a comfortable GUI, so why bother working with an old school terminal?

Read the original article on DivingLinux.com

Well, let me tell you that learning to use a command line is far from outdated, and it remains one of the most powerful tool to improve your productivity. I personally chose to focus on Linux, but developers working on macOS or Windows (with WSL2) can benefit just as much from it, and thanks to POSIX compliance, apply this knowledge on most operating systems.

I tell you this from experience

I'm a full-stack developer working with Linux for the past 7 years, and slowly embraced the command line, using it every day to execute simple and complex tasks.

The first time I was presented a terminal, all I saw was an inefficient and complicated way to do the things I already knew how to do with the native applications available on my operating system, so why bother?

Even if I did not like it, it was the only way I knew to tinker with servers, mostly to configure Apache to host my newbie PHP code online. From theses simple operations I got used to doing more things, started to develop confidence in my skills to a point where I can’t see myself working without a command line, not only to work with servers, but also on my desktop.

It’s hard to illustrate how learning to use a command-line might be a good idea, so we will explore together a few advantages of the command line with real-world uses-cases, and I encourage you to perform each of them on your computer for comparison.

Quickly try something, fail, and iterate

Small trial and error hacking is one of the thing that I love the most doing in a command line.

A recent example, I found a government website that list all general practitioner in my city, along with their status on accepting new patients. After a digging a little, I discovered that the list is available in JSON format, on a public URL.

Could I create an automated task that notify me when a general practitioner becomes available, all from a Linux command line? Of course, I can do that!

First I need to get the JSON file, this can be done with curl, make the JSON data readable for exploration, and finally filter it, both tasks can be done with jq :

student@waddle:~/$ curl https://health.gov/available-gp.json \
| jq '.[] | select(.available == true)'

{
"name": "Dr. John Doe",
"available": true
}
Enter fullscreen mode Exit fullscreen mode

Surely, finding the right combination of commands takes experience and time, and you could do the same thing with a few lines of Python or JavaScript. But for simple tasks like this, I find the terminal more efficient, no needs to set up a development environment, create a project, install packages, or start a code editor, all I need is my terminal.

Once you know how to properly navigate your terminal, you can try to tinker with grep, sed, awk, combined with piping and redirection. This will open you to a whole new world of possibilities.

Automating common tasks

Now I would like to automate finding available doctors and receive some sort of notification when a new availability is discovered. For this, I need to check if my list has any results, and if that's the case, receive a desktop notification.

student@waddle:~/$ curl https://health.gov/available-gp.json \
| jq '.[] | select(.available == true).name' \
| xargs -I{} notify-send "Found available general practioner" "{}"
Enter fullscreen mode Exit fullscreen mode

There's some dark magic involved for the beginners eye. The jq command will return every available general practitioner name, and thanks to the pipe to xargs, a notify-send command will be executed for each returned line, which means no notification if no results.

I can now execute this command every hour with a Cron task to fully automate the process, all it took is about 10 minutes.

Learning about Cron and systemd are great ways to start playing with automation. You can for instance build your own backup system with a single command using rsync in a cron task.

Master your developer tools

It doesn't matter if you're currently a frontend or backend developer, knowing to use a command-line will give you the most flexibility in terms of developer tools.

Both need continuous-integration and continuous delivery, that most probably run in a Linux server. Both need version control, package management, and testing tools that run in a command line.

You can use a GUI for 90% of your use cases, and it's completely fine. But when you run into an uncommon error or want to perform a very particular task, you will have to go back to your terminal, simply because most GUIs only replicate a subset of the raw capabilities of command-line tools.

A simple way to make yourself more comfortable using developer tools in the terminal is to replicate with a command line something you usually do in a GUI.

For instance, I like to see my branches and commit history in GitKraken, as it makes it more visual and understandable to manage a Git repository.

A quick Google search will show me that it's in fact possible to get a similar visual directly in a terminal with the following command:

student@waddle:~/$ git log \
--pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' \
--graph \
--abbrev-commit

* 1e274a9 - (origin/main, origin/HEAD) WIP server provisioning (1 day ago) <Nicolas>
|\
| * b62d50d - Base server driver abstraction (2 days ago) <Nicolas>
|/
* e1fdf32 - Initial commit (2 weeks ago) <Nicolas>
Enter fullscreen mode Exit fullscreen mode

It's not convenient to type, or even remember, this command, which makes it the perfect candidate for creating a Git alias.

student@waddle:~/$ git config --global alias.pretty "log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
Enter fullscreen mode Exit fullscreen mode

Now all I have to do is type git pretty to get the expected result in a few milliseconds, and I can combine it with other tools like grep to quickly search a commit.

Learn to do things once, apply everywhere

As stated in the introduction, most of the base commands you will use in a terminal are POSIX compliant, which mean that you can use them on macOs, Windows with WSL2, Linux desktop and server.

By developing the habit to work in the command-line, you will also get used to work in a remote server environment, making the switch between desktop and server friction less. Learning to perform system administration and DevOps tasks when you are comfortable with a terminal and a few base commands is much easier.

Sure, the tasks performed on a server and on your desktop aren't the same, but the tools are common between the two, in most cases running sed on macOS, Ubuntu or Debian server will provide a fairly similar result.

Expand your stack

Being at ease working in a command line, especially on a Linux operating system, is the perfect gateway to expand your knowledge.

I personally started my career as a front end developer, learned backend along the way, and later system administration and DevOps to securely host and deploy my code on the cloud. This does not make me an expert on every topic, but knowing how each pillar works definitely make me a better developer.

Having at least a base knowledge of how a Linux server work will make you a better backend developer, and understanding a bit of networking / caching / DevOps will make you a better front end developer.

And as most action on a Linux server are made from a command line, it's the perfect starting point.

It's not all fun and games, at first

As a beginner, doing tasks from a command line will be painful and slow. Give yourself enough time and practice to create the neural pathways that will make you able to “think” in command line terms. After a few weeks of doing small tasks it will become as normal as breathing, you just need to force yourself a little to get there, and it will be worth it.


If you want to learn more on this topic, consider trying DivingLinux.

💖 💪 🙅 🚩
nicolasbeauvais
Nicolas Beauvais

Posted on July 26, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related