For around 18 months now I’ve been using the terminal more and more. Perhaps the most suprising part of this has been just how enjoyable it is to use once you move past the very basics and become more proficient with what can at first seem on a level with black magic.
Whereas historically it was unusual for a front-end dev to stare too deeply into the command line abyss, it has now become a tool that can aid many front-end tasks. Whether using git, grunt or jekyll, there are many tools that require a small amount of terminal knowledge.
So I wanted to jot down the basics when starting out with the terminal and share some of the most common and useful commands that I tend to use most often.
Note. The commands and shortcuts will refer directly to the OSX terminal, but many of the basics cross over to Windows command line as well.
The most basic terms you'll need to survive on the command line:
Short for change directory and will navigate to the directory specified after the command.
For example, cd Users/Sites/
will move you into the Users/Sites folder.
To navigate back one directory (to the current locations parent directory) use cd ../
. To return to your home directory type cd
. To return to the previous directory you were located in type cd -
.
Using the ~
character means to use the $HOME internal variable, which is usually the root directory of the current user. So for example typing cd ~/
means to traverse from the users home directory.
To navigate to a folder which has spaces in its folder name, use cd /Users/Ash/My\ Folder\ Name/
where the \
escapes each space or cd “/Users/Ash/My Folder Name/"
.
mkdir my-directory-name
.touch index.html
or can be used to create multiple files by writing touch index.html default.css
.cp /Users/Ash/originalfile.txt tmp/copiedfile.txt
.Short for list. Lists all files in a directory.
To view files as a vertical list use ls -l
.
To view all files including hidden ones use ls -a
.
To view only directories, type ls -l | grep '^d'
rm -rf folder-name
.Opens a file.
open .
opens the current directory in a Finder window.
man rm
will display information about the rm
command.A useful reference if you'd like to see more detail about what specific characters mean when using them on the command line is Chapter 3 of The Bash Reference Manual.
Ctrl + C
as it doesn’t completely stop the process – you can return to it later by entering fg 'process name'
where fg
stands for foreground.Clears the current window, although you will still be able to scroll back up to see your history. You can also use Ctrl + L
as a shortcut.
If you use iTerm (see later in article), you can also clear the screen with Cmd + K
sudo !!
.cd Sites/AshsFolder && mkdir css
will change the directory to the Folder “Sites/AshsFolder” and create a new folder called "css".killall Finder
will restart all Finder windows.Requests the version of the operator being called. So ruby -v
will display the version of ruby installed.
Sometimes, packages can use a slightly different terminology such as Git which uses git —version
.
ping www.bbc.co.uk
attempts to get a response from www.bbc.co.uk and will tell you the response time. Useful for checking if a server is responding or to find the external IP of a web address.ipconfig getifaddr en1
curl ipecho.net/plain; echo
sudo fs_usage
You can change all sorts of OSX preferences straight from your command line. If you know about dotfiles, then an OSX dotfile simply runs a set of bash commands on your command line to make a set of preference changes.
For example, to show hidden files and folders in Finder windows, you can run this command in your terminal – defaults write com.apple.finder AppleShowAllFiles -bool TRUE; killall Finder
.
To see more examples of preference command line, checkout my OSX dotfile.
Aliases are a godsend for making short work of commonly used commands. They are basically a way of creating a shorthand of a longer command, so that you can then type the shorter command in order to execute it.
I have written a separate article about how to create terminal aliases and functions, so if you're interested in finding out more, go take a look.
An emulator for the default terminal provided by Mac OSX, iTerm provides extra features that the default terminal doesn't.
Especially useful for creating profiles – shortcuts for common directories and commands – and providing split pane views, a number of its features are detailed on its site.
If you want to know more about iTerm, checkout its website.
history | awk '{print $2}' | awk 'BEGIN {FS="|"}{print $1}' | sort | uniq -c | sort -nr | head
sudo shutdown -r +60
.I’m always interested in seeing what terminal commands people find most useful, so if you haveof a command that you can’t live without, I’d love to hear about it in the comments!
Article posted on the 24th June 2014
Last updated on the