The terminal is one of the tools that every developer spends a considerable amount of time looking at each day. This post outlines basic methods that will help you to get to know the terminal. At the end of the article, there’s a list of helpful tricks that you can use to make your life easier!
For many non-technical people, the command line (commonly also referred to as CLI, Terminal, bash, or shell) is a place full of mysteries. Thankfully you only need to know a handful of commands to start feeling comfortable and use Terminal to speed up your work.
Opening Your Command Line Interface
On a Mac, the most common application for command-line workflow is "Terminal". It comes pre-installed with every macOS system. However, the much better flavor of the terminal, that I personally use, is iTerm 2. It offers features you won’t find in Terminal like keyboard shortcuts, mouse support and lots of customization features you should find interesting (when you have to look at it, why not make it pretty?).
Finding Your Way Around
The command-line interface is used to execute all sorts of different commands. It works like that - you type something and confirm the command by hitting ENTER. Most of these commands are dependent on your current location (meaning a certain directory or path on your computer).
Why CLI is so important?
Well, it allows you to use lots of different tools for developing your programs. Anything from controlling the environment versions, laying foundations to your applications to committing code changes using git can be controlled using CLI.
When I started coding I didn't know much about CLI and how to leverage its commands, hopefully, this list will help you out with your first steps.
Let's start with the most basic commands that will help you interact with your computer in a new way.
Print working directory
You can easily remember this command when you know what it stands for: "print working directory”. This command returns the current path you're working on.
To change this current working directory, you can use the “cd” command (where "cd" stands for "change directory").
$ cd .. $ cd name-of-folder/subfolder $ cd ~
Change directory command is very useful when traversing the folder tree on your machine. The first command listed above allows you to enter into the directory that's the parent of the current directory you're currently in.
The second command allows you to enter the directory based on its name. The last command is really useful too. It allows to enter the root directory of your machine. Thanks to that, when you want to enter your downloads folder on Mac you can use
cd ~/Downloads instead of
Listing directory content
We know how to check the current working directory, and enter the ones we want. But how can we check the contents of our current directory?
ls command comes in!
$ ls // Output README.md bootstrap config package.json resources storage vendor app composer.json database phpunit.xml routes terms.md webpack.mix.js artisan composer.lock node_modules public server.php tests yarn.lock
ls command allows you to use a couple of options. The
-l flag formats the output list with additional information about the contents of the directory you're in.
$ ls -l // Output -rw-r--r-- 1 username staff 1681 14 sty 18:46 README.md drwxr-xr-x 9 username staff 288 15 sty 11:55 app -rw-r--r-- 1 username staff 1686 14 sty 18:33 artisan drwxr-xr-x 4 username staff 128 14 sty 18:33 bootstrap -rw-r--r-- 1 username staff 1665 15 sty 11:46 composer.json -rw-r--r-- 1 username staff 219146 15 sty 11:52 composer.lock drwxr-xr-x 15 username staff 480 14 sty 18:33 config drwxr-xr-x 6 username staff 192 14 sty 18:33 database drwxr-xr-x 719 username staff 23008 15 sty 11:57 node_modules -rw-r--r-- 1 username staff 1223 15 sty 11:54 package.json -rw-r--r-- 1 username staff 1405 14 sty 18:33 phpunit.xml drwxr-xr-x 12 username staff 384 15 sty 11:58 public drwxr-xr-x 6 username staff 192 15 sty 11:54 resources drwxr-xr-x 6 username staff 192 14 sty 18:33 routes -rw-r--r-- 1 username staff 563 14 sty 18:33 server.php drwxr-xr-x 6 username staff 192 15 sty 11:49 storage -rw-r--r-- 1 username staff 99 15 sty 11:54 terms.md drwxr-xr-x 6 username staff 192 14 sty 18:33 tests drwxr-xr-x 52 username staff 1664 15 sty 13:26 vendor -rw-r--r-- 1 username staff 1271 15 sty 11:54 webpack.mix.js -rw-r--r-- 1 username staff 294909 15 sty 11:57 yarn.lock
The option I use most is
ls -a will list all files adding "hidden" files as well (which is helpful when working with version control) or changing .env settings.
$ ls -a // Output . .env.example README.md composer.lock phpunit.xml storage yarn.lock .. .git app config public terms.md .DS_Store .gitattributes artisan database resources tests .editorconfig .gitignore bootstrap node_modules routes vendor .env .styleci.yml composer.json package.json server.php webpack.mix.js
You can also use multiple options at the same time, combining them together into 1 command
ls -la (order of options doesn't matter).
Working with Files and Directories
Now you should be able to traverse all the directories on your machine, so it's time to create some new files and directories!
Let's begin with creating a directory and adding a file to it.
$ mkdir terminal-playground $ cd terminal-playground/ $ touch index.html
In the first line we're creating a folder named
terminal-playground then we enter it and create a file with
touch command called
Renaming files and moving them
Let's imagine you want to rename your
index.html file to
home.html, and move it into a folder called
$ mv index.html home.html $ mkdir website $ mv home.html website/home.html
We just used a new command
mv (simply: move). It can be used both to rename files as well as moving them between folders. Yes! You guessed it! We can rename and move a file using single command :)
Removing files and directories
Let's see how removing files and directories works
$ rm website/home.html $ rm -rf website
rm (remove) command can be used both to remove files and folders. Removing folders requires using the
-r option (recursive) but it will only remove empty folders. To remove whole directories we need to add
-f flag (force). You can see it used on the last line of the above example
The command-line is quite an all-rounder: it can also display a file's contents - although it won't do this as elegantly as your beloved editor. Nonetheless, there are cases where it's handy to use the command line for this. For example when you only want to take a quick look - or when GUI apps are simply not available because you’re working on a remote server.
cat command outputs the whole file in one go:
$ cat file.ext
head command displays the file's first 10 lines, while
tail shows the last 10 lines. You can simply scroll up and down in the output like you're used to from other applications.
less command is a little different in this regard.
$ less file.ext
Although it's also used to display output, it controls page flow itself. This means that it only displays one page full of content and then waits for your explicit instructions. You'll know you have
less in front of you if the last line of your screen either shows the file's name or just a colon (":") that waits to receive orders from you.
Hitting SPACE will scroll one page forward,
b will scroll one page backward, and
q will simply quit the
Opening folders with GUI and editors
There's a handful of little tricks that make your life a lot easier while working with the command line. Let's say you want to view the current directory using a GUI interface.
$ open . $ open image.jpg
open . command will open Finder on Mac in your current working directory. You can also add file name to open specific file in its default application.
Most of the modern code editors add CLI command when installed. This works like
open command except it uses editor application to open files and directories.
$ atom . $ atom index.html
The above example will open folder or file using Atom code editor if installed.
Making Your Life Easier on the Command Line
Autocomplete paths with the TAB key
Pressing the TAB key while entering file names autocompletes what you've written, which reduces typos very efficiently.
In case your typed characters that are ambiguous (because "dev" could be the "development" or the "developers" folder...), the command line won't be able to autocomplete. In that case, you can hit TAB another time to get all the possible matches shown and can then type a few more characters.
Browse the history of commands with ARROW Keys
The command line keeps a history of the most recent commands you executed. By pressing the ARROW UP key, you can step through the last commands you called (starting with the most recently used). ARROW DOWN will move forward in history towards the most recent call.
Navigate command line with CTRL Key
When entering commands, pressing CTRL+A moves the caret to the beginning of the line, while CTRL+E moves it to the end of the line.
Stop command processes with CTRL+C
Finally, not all commands are finished by simply submitting them: some require further inputs from you after hitting return.
In case you should ever be stuck in the middle of a command and want to abort it, hitting CTRL+C will cancel that command. While this is safe to do in most situations, please note that aborting a command can, of course, leave things in an unsteady state.