Command line & Shells
Contents
Command line & Shells#
Introduction#
This is an introduction to navigating your computer via a command line interface like Windows Powershell or Bash. While useful for all programmers, it is essential for installing and using many computational tools including Git, Python and their management facilities like.
A useful 35-min. video tutorial (dated Oct 2020) on terminals, shells, and the command line is: How do you use the Command line? PowerShell, cmd, bash? This simultaneously demonstrates commands and behaviors using Windows command line
, Powershell
and Bash
. Concepts addressed include: >”shells vs terminals”; >”commands & command aliases”; >command “switches” or “parameters” (and use of “help” or “man”); >using “tab” and “history”; >”home directory”, “prompts”, “redirects, appends & pipes”; >the concept of “*administrator””, and more - including differences / similarities in these three shells.
Shells#
A “shell” is just a program that has access to your computer’s operating system. Shells can run other programs, open and save files, or install programs on your machine. The start menu/file explorer (windows) or dashboard/finder (mac) may be more familiar, but as a programmer or data analyst you will discover that using a shell will be more efficient for many tasks.
Mac and Linux computers typically use BASH as the default shell, and Windows computers typically use Windows Powershell, however many alternative shell programs exist for those interested in exploring options. Windows recently introduced the Windows Terminal which is not in fact a shell, but a versatile tool for managing one or more shells. See Microsoft’s Windows Terminal introduction for more details.
The command line#
Start your preferred shell and you will see a very sparse window - likely black, with just a few characters, and a Prompt such as $
or >
or C:\folder1\yourfolder
. Commands are typed at the cursor that probably blinks just after this prompt. There are useful key strokes (such as the up-arrow which recalls previous commands) and many (many!) commands, but listing all these capabilities for specific shells is beyond the scope of this page.
Bash and powershell command reference#
Here is a short list of common command-line commands, from the Numerical Methods course page. Any command in the first three columns should work. Other “short lists”, or documentation for each task, can easily be Googled.
In the following table, a Cmdlet
is microsoft’s term for a PowerShell command, the Alias
is an alterative spelling, and the Bash equiv
is the equivalent action in Bash (Mac and Linux) and also an alternative alias in PowerShell.
Cmdlet |
Alias |
Bash Equiv. |
Description |
---|---|---|---|
|
|
|
List the directories and files in the current location. |
|
|
|
Change to the directory at the given path. Typing |
|
|
|
Changes to the directory. |
|
|
|
Changes back to the previous directory after using |
|
|
( |
Creates a new item. Used with no parameter, the item is by default a file. Using |
|
none |
|
Creates a new directory. (See |
|
|
|
Open something using File Explorer (the GUI) |
|
|
|
Deletes something. Permanently! |
|
|
|
Moves something. Takes two arguments - first a filename (i.e. its present path), then a path for its new location (including the name it should have there). By not changing the path, it can be used to rename files. |
|
|
|
Copies a file to a new location. Takes same arguments as move, but keeps the original file in its location. |
|
|
|
Outputs whatever you type. Use redirection to output to a file. Redirection with |
|
|
|
Gets the contents of a file and prints it to the screen. Adding the parameter |
|
|
( |
Searches for specific content. |
|
|
( |
Gets statistical information about an object. Use |
|
none |
|
Redirection. Puts the output of the command to the left of |
|
none |
|
Piping. Takes the output of the command to the left and uses it as the input for the command to the right. |
|
none |
|
Gets the help file for a cmdlet. Adding the parameter |
|
none |
|
Exits PowerShell |
Remember the keyboard shortcuts of tab
for auto-completion and the up and down arrows to scroll through recent commands. These shortcuts can save a lot of typing!
This link has one example of a fairly good single page (although fairly long) introduction to Power shell. It includes a PDF cheat sheet of commands and aliases, but that contains way more than you will need unless you start managing systems and implementing shell scripts (i.e. automating repetative sequences of complex commandline activity).