Tutorial 2 - Terminal and Command Line¶
Introduction to Command Line Interface¶
Command Line Interface (CLI) is a text-based interface that allows users to interact with a computer system by typing commands.
It’s a way to give instructions to the computer using textual input. The command line is used to execute various commands, run programs, navigate the file system, and perform various tasks.
It’s commonly found in operating systems like Unix, Linux, and Windows, where users can enter commands directly into the command-line interface.
Why use CLI?
CLI is more efficient for certain tasks, since we can execute the task with a single command instead of multiple clicks on different windows.
Although Windows has features like Windows Task Scheduler, CLI is more flexible and easier to automate tasks.
For some tasks, such as running tasks on a remote server, CLI is the only way to interact with a computer.
Basic Definitions Related to CLI¶
Terminal: A terminal is a software application that offers access to a CLI. It’s the platform or environment in which the command line is displayed and utilized. Essentially, it’s the visual medium that facilitates communication with the computer via the command line.
Shell: A shell is a program designed to interpret and execute the commands entered by users through the command line. Acting as a command-line interpreter, it translates human-readable commands into instructions comprehensible by the operating system for execution. The shell also manages tasks like input and output redirection, environment variables, scripting, and various other functionalities. Different operating systems deploy distinct shell programs. For example, Unix-like systems typically utilize shells such as Bash, Zsh, or Fish, while Windows employs the Command Prompt and PowerShell.
Console: The console serves as the physical device that enables you to interact with the computer.
In summary, the command line functions as a text-based instruction method, the terminal provides the visual interface for the command line, the shell is the program responsible for interpreting and executing input commands through the command line, and the console is the computer screen, keyboard, mouse we use to interact with our computer.
Access the Command Prompt in Windows¶
To access the Command Prompt, enter “command prompt” in the Start menu’s search bar to search for it. Alternatively, you can enter “cmd”, which is the abbreviated name of the executable responsible for running the Command Prompt.
Or, press Win + R to open the Run box, then enter “cmd” and press Enter.
Alternatively, press Win + X, and choose Command Prompt (or Powershell, depending on Windows settings) from the menu.
To run Command Prompt as an admin, hold Ctrl+Shift before pressing Enter, or right-click on the Command Prompt to select “Run as administrator”.
Other Ways to Access CLI in Windows¶
Access through Jupyter Lab. After opening Jupyter Lab, in the top menu select the File tab, then click New, and select Terminal.
Or, enter Anaconda Prompt in the Start menu’s search bar to search for it. The difference between Anaconda Prompt and Command Prompt is that Anaconda Prompt offers some conda-specific features, such as conda environment management, package management, and pre-configured data science packages.
Command Prompt Basics¶
After opening a Command Prompt window, some basic information will be displayed.
For example, the current directory C:\Users\[Username]>
will be displayed as in the figure below.
Only valid commands will be accepted in the Command Prompt, otherwise a message like this will be displayed.
Help command: typing help
in the Command Prompt will display a list of commands and the associated descriptions.
For more information on a specific command, type HELP command-name
, e.g. HELP DIR
. As you may have noticed, the commands in Windows Command Prompt are not case-sensitive, and both help
and HELP
work.
Listing and Changing Directories¶
DIR
is an abbreviation for the word directory, and it lists the contents of the directory (folder) that you are currently in.
To change directory, use CD
followed by the name of the folder you want to access.
To go back to the parent directory, use CD ..
Creating and Deleting Files/Folders in Command Prompt¶
Use mkdir
followed by [new folder name/new file name]
to create a new folder/file.
E.g., the command to create a folder named “test” is mkdir test
.
Use rmdir
followed by [new folder name]
to delete a folder.
Use del
followed by [new file name]
to delete a file.
CLI Management¶
Enter cls
to clear the previous content.
Press Ctrl+C to end a command that is running
Run Python Files in Command Prompt¶
To execute a Python file, simply type python followed by the path to the file.
To find the file directory, just right-click on the file, then click “Properties” and copy the directory of the folder.
The python file directory will be the name of the folder the file is in, followed by a backward slash \
and the file name. E.g., the file directory of the test.py
file is C:\Users\L\Desktop\TA
, and therefore the path for the python file is C:\Users\L\Desktop\TA\test.py
.
To run this file, use the command python "C:\Users\L\Desktop\TA\test.py"
.
Access the Terminal in Ubuntu (Linux)¶
Click on the Activities on the upper left of the screen, and type keywords like “terminal”, “command”, “prompt”, or “shell”.
Press Ctrl+Alt+T.
Terminal Basics¶
After opening a terminal window, username@linux-desktop:~$ will be displayed.
Basic Commands in Ubuntu (Linux)¶
Commands in Linux are case-sensitive, but commands in Windows are NOT case-sensitive.
The command help
in Linux is the same command as in Windows, and it displays the available command names.
Similarly, help
followed by a command-name will show the specific information of that command.
In Linux, ls
is used to list the files in a directory instead of the command dir
in Windows.
To run a python file in Linux terminal, type python3
followed by the file name.
References¶
“A Beginner’s Guide to the Windows Command Prompt” by Ben Stegner, available at: https://www.makeuseof.com/tag/a-beginners-guide-to-the-windows-command-line/.
“Command Line for Beginners – How to Use the Terminal Like a Pro” by German Cocca, available at: https://www.freecodecamp.org/news/command-line-for-beginners/.
BACK TO TOP