The Command Line
Most users interact with their computers primarily through graphical user interfaces (GUIs). A GUI, however, is only a shortcut to system instructions, which we can also type directly into the command line.
The window used for command line inputs has different names in different operating systems. In Windows environments, it is often called a console, whereas it is more often referred to as a terminal in macOS and as a shell in Linux. In the following, we use the term console to refer to command line input windows under all operating systems.
For Windows operating systems, the following holds:
When we enter the name of a file into the command line, the system searches for an executable file with that name (e.g., .exe
or .bat
).
The search space includes the current directory as well as all directories listed in the operating systems environment variable PATH
.
Non-executable files can only be opened with a display program (e.g., notepad test.txt
starts the program notepad and passes test.txt
as an argument; notepad then opens the file test.txt
).
As Python programs are translated to machine code at runtime by means of an interpreter, Python scripts cannot be executed directly.
Instead, we need to call the Python compiler and pass the name of the script as an unnamed argument (e.g., python sorting.py
, where sorting.py
is the name of the file in which our sorting program resides.
Basic Command Line Instructions
All commands starting with C:\>
refer to the Windows command line; $
refers to operating systems from the UNIX family (including macOS).
Show the help for a command:
C:\> help command
or$ command -h
Delete all output:
C:\> cls
bzw.$ clear
Choose a hard drive:
C:\> D:
or$ cd /Volumes/OtherDrive
Change directories:
One level down:
C:\> cd Laws
or$ cd Laws
One level up:
C:\Laws\> cd ..
or$ cd ..
Directory names with spaces:
C:\> cd "Laws\International Law"
or$ cd "Laws/International Law"
Show the content of a directory:
C:\> dir
or$ ls -l
Show the content of a file:
C:\> type civilcode.txt
or$ cat civilcode.txt
Delete a file:
C:\> del civilcode.txt
or$ rm civilcode.txt
Create and delete directories:
C:\> md CriminalLaw
or$ mkdir CriminalLaw
,
andC:\> rd CriminalLaw
or$ rmdir CriminalLaw
Rename files or directories:
C:\> rename sales_contract_v3_2018-01-01_comm_johndoe.txt sales_contract.txt
or$ mv sales_contract_v3_2018-01-01_comm_johndoe.txt sales_contract.txt
Copy files:
C:\> copy civilcode.txt Laws\CivilLaw\
or$ cp civilcode.txt Laws/CivilLaw
Redirect an output to a file:
C:\> python sorting.py > output.txt
or$ python sorting.py > output.txt