In the first part of a series of articles about the command-line, I’ll go through some of the core command-line tools, basic terminologies, and point you in the right direction to get everything set up; a gentle introduction to the command-line.
The command-line interface (CLI) is a means of interacting with computer programs using text commands as input/output. Barring input methods such as punch cards, it is one of the oldest ways to interact with a computer with its origins in the 1960’s. Despite the vintage of command line input, it is still widely used today, and it remains as arguably the most powerful input/output system available.
While most users will never need to use a command line, for developers and enthusiasts alike it’s an incredibly powerful tool to keep in their arsenal, and one that I highly recommend picking up.
Note: I will only be covering POSIX-based operating systems such as Linux, BSD, Mac OSX etc, but Windows users can still get most of the functionality by either using Powershell and adopting slightly different commands (I won’t go into those here) or installing Cygwin which attempts to bring the Linux command line to Windows.
1. Core command-line tools
Most operating systems will come with a core set of commands built in which enable things such as traversing and manipulating the file system, moving or copying files, and deleting things. For the most part these commands are:
rm. Here are 3 fundamental commands with examples:
pwd – Print Working Directory
This command simply prints out the current directory.
$ pwd ~/
The output above ~/ means your users home directory.
cd – Change Directory
‘Change directory’, this command allows you to change to different directories in a variety of ways. Here are a few examples:
$ cd Desktop $ pwd ~/Desktop $ cd $ pwd ~/
ls – List
ls command is something you’ll use very often on the command line. It simply lists files. It can be used with a variety of options (covered in the next step):
$ ls Documents Downloads Pictures
To continue learning basic commands along with tutorials and examples I highly recommend The Command Line Crash Course.
2. Command-line options
Now it’s time to introduce command-line options (also known as flags or switches). Put simply, command-line options allow you to alter a commands behaviour, affecting its actions and/or results. Options are almost always preceded by a single or double dash
-, but not always. Take the following example using the
$ ls -l lrwxr-xr-x 1 user staff 34 Mar 3 10:20 Documents lrwxr-xr-x 1 user staff 33 Mar 3 10:20 Downloads lrwxr-xr-x 1 user staff 33 Mar 3 10:20 Pictures
In the above example, an option has been added:
-l. This option means “list in long format” and, as you can probably guess, simply displays results in more of a list format. But let’s not stop there, we can do more:
$ ls -la drwxr-xr-x+ 60 user staff 2040 Jun 6 14:00 . drwxr-xr-x 5 user staff 170 Feb 27 10:42 .. lrwxr-xr-x 1 user staff 34 Mar 3 10:20 Documents lrwxr-xr-x 1 user staff 33 Mar 3 10:20 Downloads lrwxr-xr-x 1 user staff 33 Mar 3 10:20 Pictures
In this example, I’m showing how more than one option can be passed at once. The first option
-l is the same as before so will display results in a list format, but there is another option as well
-a which means “include directory entries whose names begin with a dot (.) I could add more options if I wanted, but this brings up a question, where do we find out what options are available?
3. Man pages
Man pages are essentially built-in reference pages, they (usually) provide a description of the tool, available command-line options, and (sometimes) a few examples. They are usually written by the developers themselves so can be highly (often overly) technical, but they should, at a minimum, include a list of available command-line options. Let’s looks at the man page entry for
As you can see, it has a description and shows options. The man page is being displayed using a command line program called
less, which has a few keyboard commands to navigate. To go to the next page, simply hit the
To quit the man page, just hit
If you want to use
less yourself, for example to output a plain text file onto the screen, you can do so from the command-line like so:
$ less filename.txt
4. Unix shells (Bash, Zsh, fish etc…)
I won’t go into too much detail about what Unix shells, there is a great Wikipedia article available if you want to read more, but since there are several shells available I will simplify it drastically and say that a Unix shell is the user interface and interpreter between you and Unix. Bash is one of the most popular shells, and the one I personally use. Others include Zsh, fish and the older Bourne shell (sh). The differences are pretty minor, for example Zsh has some nice autocomplete features, but I prefer to stick to Bash as it is ubiquitous and usually the shell I will find myself on when I log into a remote server for example. There are a few keyboard shortcuts you can do with Bash to take your command-line Ninja skills to the next level:
Tab ↹: Autocomplete from cursor, so if you want to
cdinto a directory, you could simply start to write the name and hit tab to autocomplete the directory name.
Ctrl+a: Moves the cursor to the line start (equivalent to the key Home).
Ctrl+b: Moves the cursor back one character (equivalent to the key ←).
Ctrl+c: Sends the signal SIGINT to the current task, which aborts and closes it.
Ctrl+e: (end) moves the cursor to the line end (equivalent to the key End).
Ctrl+f: Moves the cursor forward one character (equivalent to the key →).
Ctrl+l: Clears the screen content (equivalent to the command clear).
Alt+b: (backward) moves the cursor backward one word.
Alt+f: (forward) moves the cursor forward one word.
Bonus: make it pretty
By default most terminal applications are pretty plain to look at, with a little bit of configuration however they can display colours, more useful prompts, and nicer fonts. Here’s a shot of my configuration:
You can download and use my “dotfiles” from Github.
5. Package management
Package management allows you to install, update and remove applications on your system using just a few commands. On Linux there are several package managers, Debian based OS such as Ubuntu use one called APT, and Mac OS X while it doesn’t come with an official package manager has the excellent Homebrew package manager. The advantage of using a package manager becomes clear when you try to set-up a system without one. Without a package manager you can spend hours on internet searches, downloading random zip files, and face headaches when things need to be updated. With a package manager, you simply issue a command and it will go out and check if a newer version is available and optionally download and update the current one. Once you have a package manager installed, you can really start to explore the command-line with some of the amazing software out there.
Over to you
Now we have covered the basics, next time I’ll cover some of my favourite command-line software and tools. Do you have some favourite command-line tools you rely on every day, is there anything you would really like me to cover in the next part, or do you think there is something awesome I have missed? Let me know in the comments or drop us a tweet @mycleveragency!