The command line can be daunting for many. And yet it is one of the most powerful tools we developers can have. And it’s free!
So in this episode I want to help you get comfortable using your command line. You don’t have to use it much, but there are some tasks that were just made for the command line. Also, this is a foundation for writing scripts for any operating system.
As part of my Getting Started with DevOps series, this tutorial is cross-platform. We will be using PowerShell on Windows and bash (or zsh, they are compatible) on macOS and Linux.
Feel free to use this article as a reference to look up commands later:
- The Basics
- Read the Manual
- File Operations
- The Wildcard
- Executing Programs
- Make it fun to use!
- Thefuck (plugin)
Table of Contents
Command Line Basics
First off, start your engines.
- Windows: press the start button and search for a program called PowerShell.
- macOS: Use Spotlight to launch terminal.app
- Linux: Search for an application called Terminal. In Ubuntu, you can also press ctrl+alt+T
Now the first question to answer is: Where are we?
This is one command for Windows, macOS and Linux. How can this be?
The command line interface in macOS and Linux are identical (bash or another compatible shell like zsh, fish, etc).
You can use most commands in both operating systems.
And while Windows uses PowerShell, they have integrated aliases for unix commands. The output will often differ, but you get nearly the same information.
Now let’s navigate around your file system with the following commands:
So we can now navigate around our file systems, but typing the folder names takes long. Time to try out tab-completion.
While typing anything in your terminal, you can often press tab to auto-complete your command, filename or directory name.
Just a few examples to get you started:
Read the Manual
There are many commands coming up and both PowerShell and bash/zsh have manuals you can read on every single command.
After all, this is just your beginning 😉
So now that we can navigate through our file system, it‘s time for some interaction. Creating, Copying, Moving and removing files are the three basic operations I use everyday.
Now that we know how to work with files, we can alter these commands slightly to work with directories.
Take some time to get comfortable with these commands before you move on. The cool stuff is just ahead.
Now the last part missing is: How do I execute a program on the command line? Of course, globally installed software can be run by simply executing the command. But local files? They sometimes need execution permission (macOS and linux only)
Beware that the above command sets execution permissions for all users on your computer. If you are managing permissions for a file outside of your personal directory or generally want to learn more about your unix/linux permissions, tutorialspoint explains it well.
Make it fun to use!
Now I would love to share an easy-to-setup terminal, that is really fun to use. Including bells and whistles!
First, let‘s get a good terminal Emulator that supports tabs.
Just like in my browser, I often use multiple Terminal Tabs (e.g. for working in different Git repositories)
Okay. Now that the terminal emulator (the window) is easy to rich and offers tabs, support for ctrl+f/cmd+f text search, copy-paste and more, it‘s time to upgrade what‘s inside of that window.
Starship is a feature-loaded command prompt, which automatically shows you the following information and more without you asking for it:
- Git Status
- Current User
- If the last command returned an error
- Current directory’s NodeJS/Python/Ruby/Rust/Dotnet/Java/etc version
- Current Kubernetes/Azure/AWS/etc environment
- command duration
Just check out the gif on its website. it shows it all.
As I also like the features oh-my-zsh brings to the table, I use iTerm2+oh-my-zsh+spaceship, which is very similar to starship but only available for zsh. If you‘re interested, I might go into detail on my personal setup.
Another Plugin I love using is thefuck. Yes, that‘s its name. And it does what you think it does: It adds support for writing fuck when you messed up your last command.
My most common case: When I create a local git branch and try git push, git says that I must first setup your new branch to track a remote new branch.
I do not know this command, but instead write fuck, which then suggests the correct command.
For those of us working for clients, we can change the word to „carp“, „meh“ or something funny.
If this was a little much at once, no worries.
You can come back to this article anytime and look up the commands.
If you wanna learn more about your command line, here are some interesting links:
- tldp on the Bash Line History (also applies to PowerShell)
- Ryans Tutorials on Piping (also applies to PowerShell)