Tutorial 2 - Terminal and Command Line

View notebook on Github Open In Collab

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.

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.

331736cc9c8641b09f603af1490dcc90

  • Or, press Win + R to open the Run box, then enter “cmd” and press Enter.

cdbe96472e794241b70bc8bcb1ba2864

  • 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”.

bfb236e0175b41059927fd7cc07117a4

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.

9522fc079a1145fbaff399d7de2d37c0

  • 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.

c2a19e05b6a14c02bfb8daca89d0541d

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.

6fcc6db37a114c38ae3dc6d0f71c3e6c

Only valid commands will be accepted in the Command Prompt, otherwise a message like this will be displayed.

6d22f5aa53d74608b9090883e43ff6dd

Help command: typing help in the Command Prompt will display a list of commands and the associated descriptions.

201b06edb5954892991bea6aa046ad22

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.

4c5999bf2d85415eaf57a3b957e5d289

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.

04eef4a121814a4eb18bde90ae622c03

To change directory, use CD followed by the name of the folder you want to access.

8b6fb1006fe4481b9db21284797137c9

To go back to the parent directory, use CD ..

3ef68f4495ff4c4cb0d3d5ac6b66734f

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.

e30559027cf6484c988105828481a33c

Use rmdir followed by [new folder name] to delete a folder.

ac32bbfcd516452ab5228cd7827cc166

Use del followed by [new file name] to delete a file.

10d7250261a945c08e8a977ced461b5e

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.

a8baace75970493a8167fd03ce26b06d

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".

30f37cbbe124440d8f3b15aefc6fd12f

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”.

8ba13b1e91b5411695b8ba7549c39dc4

  • Press Ctrl+Alt+T.

Terminal Basics

After opening a terminal window, username@linux-desktop:~$ will be displayed.

d214a22ce06c4741b8c07096079f9679

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

  1. “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/.

  2. “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