Command Line
Although graphical interfaces can accomplish more and more tasks, many advanced operations still require the use of the command line.
This page will briefly introduce some usage methods of the command line.
Basics
Windows comes with two command-line interfaces. "Command Prompt" (cmd) is the older one, with relatively simple functionality. PowerShell is a newer command-line interface with rich built-in features but is relatively bloated. Both interfaces can be found in the Start Menu.
Unix-like systems (including macOS and Linux, hereafter referred to as Unix) are divided into those with a graphical interface and those without. If the system has a graphical interface (such as macOS or Linux with GNOME, KDE, etc.), the command line can usually be opened via a program called "Terminal" or "Console". Systems without a graphical interface will automatically enter the command line after startup.
The command line in Windows looks like this:
1 | |
Commands entered on the command line are displayed after the >.
1 | |
The command line in Unix looks like this (using Debian/Ubuntu as an example; command lines in other systems are generally similar):
1 | |
Commands entered on the command line are displayed after the $.
1 | |
If you log into the command line as root in Unix, the $ will be replaced by #:
1 | |
Before the >, $, or # on the command line, a path is displayed. This path is the working directory, or current directory. In Unix, the current directory is sometimes displayed in a form like ~/folder, where the leading ~ represents the home directory of the currently logged-in user. The home directory of user chtholly varies across systems: on Linux, it is located at /home/chtholly, while on macOS, it is at /Users/chtholly.
Syntax and Common Commands1
File System Related
First, introduce two ways to describe locations in the file system: relative paths and absolute paths.
-
Relative path: Describes a location relative to the current path. For example, if the current path is
~/folder, then./a.cppactually refers to the file~/folder/a.cpp. The location described by a relative path may change as the current path changes. -
Absolute path: Describes a location using the full path. For example,
~/folder/a.cppis an example of an absolute path. The location described by an absolute path does not change with the current path.Windows/Unix use
.to represent the current directory and..to represent the parent directory of the current directory. Specifically, in Unix,~represents the user's home directory (note that~is expanded by the shell, so it may not be available elsewhere).
In Windows/Unix, the pwd command can print the current directory, and the cd <directory> command can change the current directory. For example, cd folder switches to the folder subdirectory of the current directory; cd .. switches to the parent directory of the current directory.
In Windows, the dir command lists the files in the current directory. In Unix, the command to list files is ls. Notably, in PowerShell, you can use the same ls command as in Unix.
In Windows, use the md <directory> or mkdir <directory> command to create a new directory, and the rd <directory> command to delete a directory. In Unix, these two commands are mkdir and rmdir, respectively. Note that before using rd or rmdir to delete a directory, the directory must be empty. If you want to delete a non-empty directory (and all files under it), you can execute rm -r <directory> in Unix and rd /s <directory> in Windows.
Redirection Mechanism
I compiled a program that reads from standard input and outputs to standard output. However, both the input and output files are large. Is there a way to redirect the input to a specified input file and the output to a specified output file?
Use the following command to achieve this.
1 | |
For example, the command ./prog < 1.in > 1.out will make the program prog read data from 1.in in the current directory and overwrite the program output to 1.out.
Warning
The original content of 1.out will be overwritten. If you want to append to the end of the original output file, use >>, i.e., ./prog >> 1.out for output redirection.
Note that PowerShell only supports output redirection, not input redirection.
In fact, most OJs adopt such a redirection mechanism. The program submitted by the contestant uses standard input/output. Through redirection, the contestant's program can read data from a given input file and output to a specified output file, and then file comparison can be used for evaluation.
Executing Programs
For an executable program or batch script, simply enter its filename directly in the command line to execute it.
Of course, when executing a file, the command line does not search through all directories. The environment variable PATH describes the scope of the command line search path; the command line will look for the target file in the paths within PATH.
For Windows systems, the current directory is also within the default search scope of the command line. For example, in Windows systems, entering the hello command can execute hello.exe in the current directory. However, in PowerShell, PowerShell does not search for executables from the current directory by default (consistent with Unix behavior). Therefore, in PowerShell, you need to use a relative or absolute path to call an executable in the current directory, such as .\hello.exe. Otherwise, you will see the following error:
1 2 3 4 5 6 7 8 9 10 | |
In Unix systems, the current directory is not within the default search scope of the command line, so the command to execute the hello program in the current directory becomes ./hello:
1 2 3 4 | |
Summary
The usage introduced above is only a small part of command-line commands; many commands are not covered. Entering the help command help in the command line can query all basic commands and their purposes.
Below is a comparison table of commands for Windows and Unix systems for reference.
| Category | Windows System | Unix System |
|---|---|---|
| File List | dir |
ls |
| Change Directory | cd |
cd |
| Create Directory | md |
mkdir |
| Delete Directory | rd |
rmdir |
| Compare Files | fc |
diff |
| Copy File | copy |
cp |
| Move File | move |
mv |
| Rename File | ren |
mv |
| Delete File | del |
rm |
Compiling/Debugging Using the Command Line
Refer to: Command Line Compilation and Debugging.
Command Line Usage Tips
Auto-completion
Completion is one of the basic functions provided by the Shell, mainly used to reduce the amount of typing and the probability of typos in command line usage.
Generally, the shortcut for completion is Tab. After pressing it, the Shell will complete the information based on the characters already entered.
Different Shells provide varying levels of completion capabilities.
The following are the completion capabilities of common Shells2:
| Shell | Completion Capability (Scope) |
|---|---|
| cmd (Windows traditional console) | File paths |
| PowerShell | File paths, command names in PATH, built-in command names, function names, command parameters, supports fuzzy matching, auto-correction |
| Bash | File paths, command names in PATH, built-in command names, function names, command parameters |
| Zsh | File paths, command names in PATH, built-in command names, function names, command parameters, supports fuzzy matching, auto-correction and suggestions |
| Fish | File paths, command names in PATH, built-in command names, function names, command parameters, supports fuzzy matching, displays parameter functions during completion, auto-correction and suggestions |
Note
Some features of PowerShell require the PSReadline Module to be loaded or to be in PowerShell ISE.
Bash's completion functionality generally requires a package called bash-completions to obtain full functionality; some software packages come with their own completion files.
Zsh's full completion functionality requires user-defined files (usually installed with the Zsh package or corresponding software packages).
Fish provides good and complete completion functionality by default, but some software not covered by the official packages provide their own completion files.
Help Documentation
Generally, programs under the command line come with "help". In Windows, it is usually obtained using command /? or command -?. On Unix-like systems (e.g., Linux), it is generally obtained using command --help or command -h (but the "help" under BSD is often too brief to be useful).
Additionally, on Unix-like systems, there is a "manual" that can be obtained via man command, which is generally more detailed than "help".
built-in time and GNU time
When testing program runtime, we usually use the time command.
But this command actually corresponds to two commands in the system: one is built into some Shells (e.g., Bash), and the other is GNU time (a separate software). There are some differences between the two.
Generally, using time directly in Bash calls the Bash built-in version. We can use the TIMEFORMAT environment variable to control its output format. For example, setting it to %3lR outputs the actual runtime with three-digit precision, and %3lU outputs user-space runtime with three-digit precision.3
If you want to call the GNU version of time, you need to use \time or /usr/bin/time. However, its output format is not very readable. We can add the -p parameter (i.e., \time -p) to get readable output.
Pipes
Suppose we have two programs A and B, both using standard input/output. How can we redirect A's output to B's input?
We can use the redirection method mentioned above: first redirect A's output to a temporary file, then redirect B's input to that temporary file.
But this method is inefficient. It not only requires creating a new file, but disk IO operations may also become a bottleneck, and the two programs cannot run simultaneously; B must wait for A to finish. Is there a better method?
Yes, that is pipes, and they are very simple to use, as shown below:
1 | |
This creates a pipe in memory, and both programs are started simultaneously. Program A's output is redirected to this pipe each time it outputs, and the pipe itself does not store data (actually, it has a very small buffer). Before B reads, A's output operation will be blocked. After B reads the data, A's output can continue. This elegantly solves the above problems: no disk IO operations, both codes run simultaneously, and no extra memory is consumed to store intermediate results.
Named Pipes
Sometimes we need more than just redirecting one program's output to another's input. For example, when doing IO interactive problems, it is often necessary to redirect A's output to B's input and B's output to A's input. In this case, the ordinary pipes mentioned above are insufficient. Redirecting to files cannot let the two programs run simultaneously. This requires a pipe that looks like a file—a named pipe.
In Unix systems, you can create a named pipe (named my_pipe as an example) using the following command:
1 | |
At this point, using the ls command to list the files in the current directory, you will find an additional file my_pipe|. This creates a named pipe; the | after the filename indicates it is a pipe file. Then you can read and write to this pipe just like file redirection.
Through named pipes, we can make two programs interact in the following way:
1 2 3 | |
After use, you can delete the named pipe using the rm command, just like a regular file.
References and Notes
-
Liu Rujia's "Introduction to Algorithm Competitions (2nd Edition)" Appendix A Development Environment and Methods ↩
本页面最近更新:,更新历史
发现错误?想一起完善? 在 GitHub 上编辑此页!
本页面贡献者:OI-wiki
本页面的全部内容在 CC BY-SA 4.0 和 SATA 协议之条款下提供,附加条款亦可能应用