Difference between revisions of "Learning to Use Computer's Terminal"

From Sustainability Methods
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
 +
''' 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.
 
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.
 
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? ===
+
== Why Use the Terminal? ==
  
 
# Efficiency: Perform tasks faster than using graphical interfaces.
 
# Efficiency: Perform tasks faster than using graphical interfaces.
 
# Automation: Automate repetitive tasks with scripts.
 
# Automation: Automate repetitive tasks with scripts.
 
# Development: Essential for programming and development workflows, for examle, for installing and managing packages.
 
# Development: Essential for programming and development workflows, for examle, for installing and managing packages.
 +
# Version control: The most efficient way to use versio control systems such as Git is with the terminal.
  
=== Accessing the Terminal ===
+
== Accessing the Terminal ==
  
==== macOS ====
+
=== macOS ===
  
 
On macOS, the terminal is called Terminal.app and provides a Unix-like command-line environment.
 
On macOS, the terminal is called Terminal.app and provides a Unix-like command-line environment.
Line 27: Line 30:
 
* Windows Terminal: A modern application that can host multiple shells, including Command Prompt and PowerShell.
 
* Windows Terminal: A modern application that can host multiple shells, including Command Prompt and PowerShell.
  
=== Difference Between 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:
 
While both Command Prompt and PowerShell are command-line interfaces available on Windows, they have key differences:
Line 36: Line 39:
 
Which One Should You Use?
 
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 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.  
  
To open the Command Prompt:
+
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.
  
1. Using Run Dialog:
+
== Common Terminal Commands ==
• Press Windows Key + R to open the Run dialog.
 
• Type cmd and press Enter.
 
2. From Start Menu:
 
• Click on the Start button.
 
• Type Command Prompt and select it from the search results.
 
 
 
To open PowerShell:
 
 
 
• From Start Menu:
 
• Click on the Start button.
 
• 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.
 
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 ====
+
=== Navigating Directories ===
 
 
• Change Directory:
 
• macOS and Linux:
 
 
 
cd [directory]
 
 
 
• Windows (Command Prompt and PowerShell):
 
 
 
cd [directory]
 
 
 
• List Files and Directories:
 
• macOS and Linux:
 
 
 
ls
 
 
 
• Windows Command Prompt:
 
 
 
dir
 
  
• Windows PowerShell:
+
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)
 +
<syntaxhighlight lang="bash">
 +
cd Downloads
 +
</syntaxhighlight>
  
 +
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)
 +
<syntaxhighlight lang="bash">
 
ls
 
ls
 +
</syntaxhighlight>
  
Print Working Directory:
+
Print Working Directory: If you want to know the specific path where you are located, you can type "pwd".
macOS and Linux:
+
(macOS and Linux)
 
+
<syntaxhighlight lang="bash">
pwd
 
 
 
• Windows Command Prompt:
 
 
 
cd
 
 
 
• Windows PowerShell:
 
 
 
 
pwd
 
pwd
 +
</syntaxhighlight>
 +
This will get you something like: "User/Downloads/"
  
 +
===Managing Files and Directories===
  
==== 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)
 +
<syntaxhighlight lang="bash">
 +
mkdir pdfs_uni
 +
</syntaxhighlight>
  
• Create a Directory:
+
Delete a File: Imagine you want to delete a pdf file named "grades_record" in your Downloads folder:
macOS and Linux:
+
(macOS)
 +
<syntaxhighlight lang="bash">
 +
rm grades_record.pdf
 +
</syntaxhighlight>
 +
(PowerShell)
 +
<syntaxhighlight lang="bash">
 +
Remove-Item grades_record.pdf
 +
</syntaxhighlight>
  
mkdir [directory_name]
+
Delete a Directory: If you want to delete, for example, a folder in your Download directory that is named "songs", you type:
 +
(macOS)
 +
<syntaxhighlight lang="bash">
 +
rm -r songs
 +
</syntaxhighlight>
 +
(PowerShell)
 +
<syntaxhighlight lang="bash">
 +
Remove-Item songs -Recurse
 +
</syntaxhighlight>
 +
In the last example, with PowerShell, you use '''-Recurse''' if the folder is '''not''' empty
  
• Windows (Command Prompt and PowerShell):
+
Copy a File:
 
+
(macOS)
mkdir [directory_name]
+
<syntaxhighlight lang="bash">
 
+
cp [source_file] [destination]
• Delete a File:
+
</syntaxhighlight>
macOS and Linux:
 
 
 
rm [file_name]
 
 
 
• Windows Command Prompt:
 
 
 
del [file_name]
 
 
 
• Windows PowerShell:
 
 
 
Remove-Item [file_name]
 
 
 
• Delete a Directory:
 
• macOS and Linux:
 
 
 
rm -r [directory_name]
 
 
 
• Windows Command Prompt:
 
 
 
rmdir /s [directory_name]
 
 
 
• Windows PowerShell:
 
 
 
Remove-Item [directory_name] -Recurse
 
 
 
• Copy a File:
 
• macOS and Linux:
 
  
 
cp [source_file] [destination]
 
cp [source_file] [destination]
Line 150: Line 118:
 
mv [source] [destination]
 
mv [source] [destination]
  
• Windows Command Prompt:
 
  
move [source] [destination]
 
  
 
• Windows PowerShell:
 
• Windows PowerShell:
Line 181: Line 147:
  
 
clear
 
clear
 
• Windows Command Prompt:
 
 
cls
 
  
 
• Windows PowerShell:
 
• Windows PowerShell:
Line 196: Line 158:
 
• Command History: Use the Up and Down arrow keys to navigate through previous commands.
 
• Command History: Use the Up and Down arrow keys to navigate through previous commands.
 
• Cancel Command: Press Ctrl + C to cancel the current command.
 
• Cancel Command: Press Ctrl + C to cancel the current command.
Help and Manual Pages:
+
Help and Manual Pages:
macOS and Linux:
+
macOS and Linux:
  
 
man [command]
 
man [command]
  
• Windows Command Prompt:
+
Windows PowerShell:
 
 
[command] /?
 
 
 
Windows PowerShell:
 
  
 
Get-Help [command]
 
Get-Help [command]
  
 
=== Advanced Usage ===
 
 
• Piping and Redirection: Redirect output of commands or chain commands together.
 
• macOS and Linux Example:
 
 
ls | grep [pattern]
 
 
• Windows PowerShell Example:
 
 
Get-ChildItem | Where-Object { $_.Name -like “[pattern]” }
 
 
• Environment Variables:
 
• Display Environment Variables:
 
• macOS and Linux:
 
 
printenv
 
 
• Windows Command Prompt:
 
 
set
 
 
• Windows PowerShell:
 
 
Get-ChildItem Env:
 
 
• Executing Scripts:
 
• macOS and Linux: Use shell scripts with .sh extension.
 
 
bash script.sh
 
 
• Windows PowerShell: Use scripts with .ps1 extension.
 
 
./script.ps1
 
 
 
=== Conclusion ===
 
 
Understanding how to use the terminal enhances your ability to interact with your computer efficiently and is essential for many programming and development tasks. While there are differences between macOS and Windows command-line interfaces, many basic concepts and commands are similar or can be adapted across platforms.
 
  
 
=== Further Reading ===
 
=== Further Reading ===

Latest revision as of 22:42, 14 September 2024

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