For the non-technical people among us, evil has a name: The “command line.” Or, in fact, many names: CLI, Terminal, bash, shell … When I prepared my free online book “Learn Version Control with Git,” I was surprised to hear how many people were afraid of the command line. In reality, there’s really nothing to fear because once you know a couple of basic commands and concepts, the CLI is instantly demystified.
When Do You Need the CLI?
I know that the 80s are long gone. And so are the days when you had to control your computer with textual commands. However, even in the modern GUI age, there are still some scenarios where a command line prompt is your best choice. For example when interacting…
- with a remote server (and you want to do more than just FTP some files up and down).
- with a modern programming framework like Ruby on Rails.
- with a version control system like Git or Subversion (and you don’t have a nice GUI at hand).
These and many more examples make clear that the command line still has its place.
Before we begin typing, you have to start up the command line interface on your computer.
If you’re on a Mac, you can use “Terminal.app,” which comes installed with every Mac OS. You’ll find it in the “Utilities” subfolder of the default “Applications” folder.
If you’re using Windows, you can open a new command line window by selecting “Start –> Run…” and typing “cmd”.
To help learn and remember basic commands, it’s helpful to have a cheat sheet at hand.
Make Yourself at Home
Working on the command line always involves the same set of steps:
- Type a command.
- Possibly add some parameters.
- Confirm by hitting ENTER.
Most of these commands work in the context of your current location – where “location” means a certain directory or path on your computer. Therefore, our first command helps us find out where we currently are:
If you need a memory aid for this command: it stands for “__p__rint __w__orking __d__irectory”. This means it will return the path to the folder where you’re currently at.
Of course, you can change this current working directory by using the “cd” command (where “cd” stands for “__c__hange __d__irectory”). As an example, let’s move into a subfolder of the current directory:
Moving upward, to the parent directory, works by providing two “.” characters for the cd command:
One special kind of path notation is very common: “~”. This symbolizes your user account’s “home” folder. Therefore, there are two ways to move to your home folder. Either by providing the full path…
$ cd /Users/<your-username>/projects/
…or by using the shorthand form:
$ cd ~/projects/
Another essential command is “ls”. It lists the contents of the current directory – or the directory you specify as a parameter:
$ ls path/to/folder
Two important options can be specified with this command:
- The “-l” flag formats the output as a more structured list with additional information.
- The “-a” flag includes “hidden” files in the listing. This is helpful in a lot of cases, for example when working with version control.
Combining these flags, you could call it like this on the current directory…
…or like this on a different directory:
$ ls -la path/to/folder
Working with Files
The most important file operations can be controlled with just a handful of commands.
Let’s start by removing a file:
$ rm path/to/file.ext
When trying to delete a folder, you’ll have to add the “-r” flag (which stand for “recursive”):
$ rm -r path/to/folder
Moving a file is just as simple:
$ mv path/to/file.ext different/path/file.ext
This command can also be used to rename a file:
$ mv old-filename.ext new-filename.ext
If, instead of moving the file, you want to copy it, simply use “cp” instead of “mv”.
Finally, to create a new folder, you call the “make directory” command:
$ mkdir new-folder
You can also use the command line to display a file’s contents. Although, without a doubt, your favorite editor will perform this task a lot more elegantly, there are cases where it’s handy to use the command line. 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.
Using the “cat” command will output the complete file in one stream:
The “head” and “tail” commands are very similar, except that they only show the first (or respectively last) 10 lines. You can simply scroll up and down in the output like you’re used to from other applications.
The “less” command is a little different in this regard. 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 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 “less” program.
Controlling with the CTRL Key
Pressing CTRL+A will move the caret to the beginning of the line, while CTRL+E moves it to the end of the line. Another nice-to-know shortcut is CTRL+C: it will abort the currently running command. This can be helpful in cases where you’re faced with either a long-running command or with one that requires further input from you that you can’t or don’t want to provide. However, please note that aborting a command can of course leave things in an unsteady state.
Autocomplete with the TAB Key
Typing file names and paths is a very error-prone task: it’s easy to misspell something along the way. The TAB key can help you here. It autocompletes what you’ve written, which reduces typos very efficiently. E.g., when you want to switch to a different directory, you can either type every component of the path yourself:
$ cd ~/design/favorite-customer/mockups/
Or you make use of the TAB key (try this yourself!):
Not always will your typed characters be unambiguous — because “de” could also be the “development” or “dentist” folders in that directory. In that case, TAB cannot automatically autocomplete your typing. However, by hitting TAB a second time, you’ll be presented with all possible matches and can then type a few additional characters to make your input unambiguous.
Command History with the ARROW Keys
Often, you will need to execute the same (or a very similar command) again after some time. You’ll be pleased to hear that the CLI keeps a history of recent commands just like your browser. You can step through this history with the arrow keys on your keyboard:
- ARROW UP will move back in history, starting with the most recent call.
- ARROW DOWN will move forward in history towards the most recently used command.
Alternatives to CLI
Let’s wrap up with good news: You don’t have to be close friends with the CLI. Because luckily, a lot of areas don’t require command line gymnastics any more; you can use an FTP client like Transmit to handle file uploads. Or a GUI like Tower to make version control easier. Examples are many…
But some scenarios remain where CLI is still your best option. And knowing your way around a bit will help you master these situations with confidence.