Beginners Tips for Working with the Command Line for Mac
Aaron Minyard
Posted on February 13, 2020
Hello readers and welcome to my first blog post! I recently began attending Flatiron School for Software Engineering in San Francisco. Within the first couple of days, it became clear to me that I needed to grow my skills working in the command line. This post is meant to serve as an introduction for those new to the command line, and provide examples of useful and necessary shortcuts.
For those that do not know, the command line (or CLI) is an alternative way to communicate with your computer using text commands through a terminal, rather than using a cursor with a mouse or trackpad. The typical way that people interact with a computer is using a cursor to accomplish whatever task, and this type of program is referred to as a graphical user interface, or GUI. The trade-off here is that we are often able to accomplish whatever task more quickly using the CLI.
As developers, being able to quickly and efficiently navigate the CLI is a very valuable skill, and is something that professional developers are expected to know very well. Because we use text commands (almost) exclusively in the command line, there are many different commands/shortcuts that we can leverage to make using the command line easier. This requires a bit of memorization and practice using the commands, but will eventually speed up your processes. First I will go over my most utilized standard commands to traverse through files/directories on a machine.
General CLI Commands
option + cursor click
If you have written out a longer command, holding down option and clicking your cursor on the character you'd like to move to will take you to that character. This is very useful for instances where you've made a small mistake or typo and need to make a small change.
[partial_command] + tab
If you're typing out a command that you've entered in your current terminal session, you can start typing the command and then press tab to complete it automatically. This is especially convenient for longer commands that you'd rather not waste time typing out again. For example, say you're trying to re-navigate to a directory with a long name you've since navigated a long way from with a name like 'exceptionally-long-directory-name-for-no-good-reason'. Typing in 'cd exc' + tab will automatically fill in the rest.
pwd
Pwd stands for 'print working directory' and no surprises here. It displays into your terminal what directory you are working in, revealing each directory all the way up to your Users folder.
cd [target_directory]
Using cd (abbreviation of 'change directory') will allow you to change the current directory you are in, as long as your target directory is accessible (either one behind, or one ahead) the target directory specified.
cd ../
Using cd ../ will take you back one directory in the file tree. For example, if you are in documents/photos, if you cd ../ from photos, you will leave the photos folder and return to the parent documents folder.
cd -
When using cd -, rather than going back a folder like cd ../, cd - will take you back to whatever folder you were in last.
cd ~
This command will take you to your home folder. In most cases this will be your individual user folder. For example if I run this on my machine it takes me to Users/aaronminyard.
open [filename]
This is an exceptionally obvious command. Typing in open followed by the filename specified will open the file using the program set to open up the file type by default.
โ
If you need to reenter a previous command, pushing up on an empty line in terminal will bring up the last command entered. If you need the command you ran multiple commands prior, you can find it by pressing โ multiple times.
File and Directory Creation
touch [new_filename_here]
Touch will create a new file in the directory you're currently in, with whatever name you specify.
mkdir [new_directory_name_here]
Mkdir will create a new directory inside whatever directory you are in.
mkdir -p [dir]/[dir]
This command is very similar to the standard mkdir command, but this will allow you to create a new nested directory.
Shortcuts to speed things up
ctrl + c
This command will kill whatever processes you are running, and brings up a new line for you to use.
cmd + k
or
clear
Cmd + k will clear everything in your terminal window. Alternatively, you can type in and enter the word clear. Although why press six keys when you can press two?
ctrl + a
This will take you to the beginning of the line you are currently typing on. This is quite useful, especially if you've typed out something long but misspelled a word early on in the line.
ctrl + e
Ctrl + e is the functional opposite of ctrl + a. Rather than going to the beginning of the line you've already typed, ctrl + e will take you back to the last character on the line.
ctrl + u
Using Ctrl + U will delete everything on the line that you have typed so far.
Hopefully you learned something helpful from this post. While navigating the CLI can feel quite awkward at first, with a bit of practice you'll soon be flying through your terminal, accomplishing things quicker than you would with the traditional GUI interface.
Posted on February 13, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.