Learning to Use Computer's Terminal

From Sustainability Methods

IN EDITION MODE

The terminal, also known as the command line or console, is a powerful tool that allows you to interact with your computer using text commands. It provides greater control and efficiency for various tasks such as file management, running programs, and automating workflows.

This entry covers how to access and use the terminal on both macOS and Windows, focusing on basic commands that are common between the two operating systems.

Why Use the Terminal?

  1. Efficiency: Perform tasks faster than using graphical interfaces.
  2. Automation: Automate repetitive tasks with scripts.
  3. Development: Essential for programming and development workflows, for examle, for installing and managing packages.
  4. Version control: The most efficient way to use versio control systems such as Git is with the terminal.

Accessing the Terminal

macOS

On macOS, the terminal is called Terminal.app and provides a Unix-like command-line environment.

To open the terminal:

  1. Use Spotlight Search.
  2. Press Command (⌘) + Space to open Spotlight.
  3. Type Terminal and press Enter.

Windows

Windows offers several command-line interfaces:

  • Command Prompt(cmd): The traditional Windows command-line interface.
  • PowerShell: An advanced shell with scripting capabilities.
  • Windows Terminal: A modern application that can host multiple shells, including Command Prompt and PowerShell.

Difference Between Command Prompt and PowerShell

While both Command Prompt and PowerShell are command-line interfaces available on Windows, they have key differences:

  • Command Prompt (cmd): This is the traditional Windows command-line interface, dating back to the MS-DOS era. It supports a basic set of commands and is suitable for simple tasks like file manipulation and running older scripts.
  • PowerShell: Introduced by Microsoft as a more advanced shell. It offers powerful scripting capabilities, uses cmdlets and provides enhanced functionality for system administration, automation, and configuration management.

Which One Should You Use?

For most modern programming, data analysis, and automation tasks, PowerShell is the recommended choice due to its advanced features and compatibility with contemporary tools. PowerShell supports many of the same commands as Command Prompt (with some syntax differences) and offers a richer set of functionalities. However, if you’re working with legacy systems or scripts that were specifically designed for Command Prompt, you might need to use cmd to ensure compatibility.

For practical reasons, we will focus exclusively on PowerShell. To open PowerShell, click on the Start button and type PowerShell and select it from the search results.

Common Terminal Commands

Below are basic commands commonly used in both macOS and Windows. Note that macOS uses Unix-based commands, while Windows uses different syntax. However, Windows PowerShell shares some similarities with Unix commands.

Navigating Directories

Change directory: To move to an specific folder, path or directory, you use the command "cd". For example, to go to the Downloads folder: (macOS and Powershell)

cd Downloads

List files and directories: If you want to see all the files inside and specific directory or folder, just type "ls". For example, now that you are already in your Downloads directory, you can type: (macOS and Powershell)

ls

Print Working Directory: If you want to know the specific path where you are located, you can type "pwd". (macOS and Linux)

pwd

This will get you something like: "User/Downloads/"

Managing Files and Directories

Create a Directory: If you want to create a new folder, type the command "mkdir" followed by the name of your folder. For example, imagine that in your Downloads folder you wan to create new folder named "pdfs_uni". (macOS and Powershell)

mkdir pdfs_uni

Delete a File: Imagine you want to delete a pdf file named "grades_record" in your Downloads folder: (macOS)

rm grades_record.pdf

(PowerShell)

Remove-Item grades_record.pdf

Delete a Directory: If you want to delete, for example, a folder in your Download directory that is named "songs", you type: (macOS)

rm -r songs

(PowerShell)

Remove-Item songs -Recurse

In the last example, with PowerShell, you use -Recurse if the folder is not empty

Copy a File: (macOS)

cp [source_file] [destination]

cp [source_file] [destination]

• Windows Command Prompt:

copy [source_file] [destination]

• Windows PowerShell:

Copy-Item [source_file] [destination]

• Move/Rename a File: • macOS and Linux:

mv [source] [destination]


• Windows PowerShell:

Move-Item [source] [destination]


Viewing File Contents

• Display Contents of a File: • macOS and Linux:

cat [file_name]

• Windows Command Prompt:

type [file_name]

• Windows PowerShell:

Get-Content [file_name]


Clearing the Screen

• Clear Terminal Screen: • macOS and Linux:

clear

• Windows PowerShell:

Clear-Host


Useful Tips

• Auto-Completion: Press Tab to auto-complete file and directory names. • Command History: Use the Up and Down arrow keys to navigate through previous commands. • Cancel Command: Press Ctrl + C to cancel the current command. Help and Manual Pages: macOS and Linux:

man [command]

Windows PowerShell:

Get-Help [command]


Further Reading

• macOS Terminal User Guide: Apple Support • Windows Command Prompt Documentation: Microsoft Docs • PowerShell Documentation: Microsoft Docs