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)
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?
# print working directory pwd
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:
# list files in directory ls # list all files in directory (includes hidden files) ls -al # macOS and Linux ls -Hidden #PowerShell # change directory cd <DIRECTORY NAME> # leave directory / move up cd .. # go back to your $HOME directory cd ~
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:
# cd to directory "Documents" cd Doc<TAB> # => cd Documents # list hidden files in PowerShell ls -Hi<TAB> # => ls -Hidden
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 😉
# Show manual in PowerShell Get-Help <COMMAND> -Full # Show only examples in PowerShell Get-Help <COMMAND> -Examples # Show manual in macOS/Linux man <COMMAND> # Scroll with arrow keys up/down # Close with Q
# Create a new file touch <FILENAME> # Display file contents cat <FILENAME> # list target directory contents ls <DIRECTORY> # e.g. ls ~/Documents
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.
# Move a file mv <FILENAME> <TARGET-DIRECTORY>/ # e.g. mv Documents/photo.jpg Pictures/ # With the move command you can also rename files mv <FILENAME> <TARGET-DIRECTORY>/<NEW-FILENAME> # e.g. mv Documents/photo.jpg Pictures/holiday-photo.jpg # e.g. mv photo.jpg holiday-photo.jpg # Remove a file rm <FILENAME>
Now that we know how to work with files, we can alter these commands slightly to work with directories.
# Move a directory mv <DIRECTORY> <TARGET-DIRECTORY>/ # e.g. mv Scanned-Pages ~/Documents/ # With the move command you can also rename directories mv <DIRECTORYNAME> <NEW-DIRECTORYNAME> # e.g. mv Scanned-Pages Scans # Remove an empty directory rmdir <DIRECTORY> # Remove a directory with all its contents (careful with this one!) rm -rf <DIRECTORY>
Take some time to get comfortable with these commands before you move on. The cool stuff is just ahead.
# Show the content of all .txt files cat *.txt # Move Photo-london.jpg, Photo-tokio.jpg, Photo-berlin.jpg etc to parent directory mv Photo-*.jpg ../
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)
# Add execution permission to file on macOS/Linux chmod +x <FILENAME> # execute a local file ./<FILENAME> # Example: list connected devices with android developer tools ./adb list devices
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)
- Windows: Windows Terminal
- MacOS: iTerm2
- Ubuntu Linux: Guake
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.
# try to clone the bouncer repo git clpne firstname.lastname@example.org:BrutalHack/Bouncer.git # => git: 'clpne' is not a git command. See 'git --help'. # The most similar command is # clone # correct the previous command fuck # => git clone email@example.com:BrutalHack/Bouncer.git [enter/↑/↓/ctrl+c]
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.
# push new "my-branch" to the git repository git push # => fatal: The current branch new-branch has no upstream branch. # To push the current branch and set the remote as upstream, use # # git push --set-upstream origin my-branch # correct the previous command fuck # => git push --set-upstream origin new-branch [enter/↑/↓/ctrl+c]
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)
Did this post help you? Do you still have questions?
Get in touch on our Discord server and sign up for updates on our development blog.