Command syntax. Command syntax. Elementary commands

02/12/15 21.3K

Why is the world so chaotic? Because the administrator of our system has forgotten about performing his duties. Or just lost the list of cmd commands from our world. Although this is a somewhat original view of the existing order of things, it nevertheless reflects part of the truth we need: using the command line, you can easily put things in order in your computer:

What is the command line

The command line is the simplest tool for managing your computer's operating system. Control is performed using a number of reserved commands and a set of text keyboard characters without the involvement of the mouse ( in Windows operating system).

On UNIX-based systems, the mouse can be used with the command line.

Some of the commands came to us from MS-DOS. The command line is also called the console. It is used not only to administer the operating system, but also to manage common programs. Most often, this set of commands includes the most rarely used ones.

The advantage of using basic cmd commands is that it consumes a minimum of system resources. And this is important in case of emergencies when all the power of the computer, one way or another, is involved.

In cmd, the ability to execute and create entire batch files is implemented, which is a certain order of execution of a number of commands (scripts). Thanks to this, they can be used to automate the execution of certain tasks ( account management, data archiving and more).

The Windows command shell for manipulating and redirecting commands to specific utilities and operating system tools is the Cmd.exe interpreter. It loads the console and redirects commands in a system-understandable format.

Working with the command line in the Windows operating system

There are several ways to call the console in Windows:

Both methods involve running the console as the current user. That is, with all the rights and restrictions that are imposed on its role in the operating system. To run cmd with administrator rights, select the program icon in the Start menu and select the appropriate item in the context menu:


After running the utility, you can get help information about the commands and their format in the console. To do this, you need to enter the operator help and press "Enter":

Basic commands for working with files and directories

The most commonly used commands are:

  • RENAME - renaming directories and files. Command syntax:

RENAME | REN [drive / path] original file / directory name | final filename
Example: RENAME C: UsershomeDesktoptost.txt test.txt

  • DEL (ERASE) - Used to delete only files, not directories. Its syntax is:

DEL | ERASE [processing method] [filename]
Example: Del C: UsershomeDesktoptest.txt / P

By processing method we mean a special flag that allows you to implement a certain condition when deleting a file. In our example, the "P" flag turns on the display of the permission dialog to delete each file:


For more information about possible values \u200b\u200bof the "processing method" parameter, refer to the technical documentation for the Windows operating system.

  • MD - allows you to create a folder at the specified path. Syntax:

MD [drive:] [path]
Example:
MD C: UsershomeDesktoptest1test2

The example will create a subfolder test2 in the test1 folder. If one of the root folders of the path does not exist, then it will also be created:

  • RD ( RMDIR) - deletes a specific folder or all directories in the specified path. Syntax:

RD | RMDIR [processing_key] [drive / path]
Example:
rmdir / s C: UsershomeDesktoptest1test2

The example uses the s flag, which will cause the entire directory branch to be deleted from the path. Therefore, you should not use the rmdir command with this processing switch unnecessarily.

In the next section, we'll take a closer look at network cmd commands.

Networking Commands

The command line allows you to control not only the file system of the PC, but also its network capabilities. The console network commands include a large number of operators to monitor and test the network. The most relevant ones are:

  • ping - the command is used to monitor the capabilities of a PC's network connection. A set number of packets are sent to the remote computer and then sent back to them. Packet transmission time and loss percentage are taken into account. Syntax:

ping [-t] [-a] [-n count] [-l size] [-f] [-i TTL] [-v type] [-r count] [-s count] [(-j hostlist | - k nodelist)] [-w int] [destination_PC_name]

Example of command implementation:
ping example.microsoft.com
ping –w 10000 192.168.239.132

In the last example of the cmd ping command, a request is sent to the recipient with the specified IP address. The waiting interval between packets is 10,000 (10 sec). By default, this parameter is set to 4000:

  • tracert - serves to determine the network path to the specified resource by sending a special echo message through the protocol
  • ICMP (Control Message Protocol). After running the command with parameters, a list of all routers through which the message path passes is displayed. The first item in the list is the first router from the side of the requested resource.

The syntax for the tracer cmd command is:
tracert [-d] [-h max_transitions] [-j hostlist] [-w spacing] [target_source_name]
Implementation example:
tracert -d -h 10 microsoft.com

The example traces the route to the specified resource. This increases the speed of the operation by using the d parameter, which prevents the command from attempting to obtain permission to read IP addresses. The number of transitions (jumps) is limited to 10 by the set value of the parameter h. By default, the number of jumps is 30:

shutdown [(-l | -s | -r | -a)] [-f] [-m [\\ PC_name]] [-t xx] [-c "messages"] [-d [u] [p]: xx: yy]
Example:
shutdown / s / t 60 / f / l / m \\ 191.162.1.53

The remote PC (m) with the specified IP address (191.162.1.53) will shutdown (s) after 60 seconds (t). This will force exit from all applications (f) and the session of the current user (l).

NET USER - user account management

NET USER Command intended for add, edit or view user accounts on computers. Running the command at the command prompt without parameters displays a list of Windows user accounts present on the computer (this command also works well on Windows 10). User account information is stored in a Windows database.

NET USER Command Syntax

net user [username [password | *] [parameters]]

net user username (password | *) / add [options]

net user username , where

  • username - Specifies the name of a user account that can be added, removed, edited or viewed. The name can be up to 20 characters long.
  • password - Assigns or changes the user's password. Enter an asterisk (*) to display a password prompt. Password characters are not displayed when entered from the keyboard.
  • / domain - Performs an operation on the primary domain controller for this computer.
  • parameters - Sets the command line parameter for the command.
  • net help command - Display help for the specified net command.
  • / delete -Removing a user account.

Additional parameters for the NET USER command

  • / active: (yes | no) -Activates or deactivates an account. If the account is not activated, the user cannot access the server. The account is activated by default.
  • / comment: "text" -Add a description for the user account (48 characters maximum). The description text is enclosed in quotation marks.
  • / countrycode: nnn -Uses the country code specified for the operating system to implement the appropriate language files when displaying custom help and error messages. A value of 0 corresponds to the default country code.
  • / expires: (date | never) -The expiration date of the account. Never is valid indefinitely. The date is in the format mm / dd / yy or dd / mm / yy depending on the country code. The month can be indicated in numbers, in full or in abbreviated form (three letters). The year can be indicated with two or four digits. Date elements are separated by a slash (/) without spaces.
  • / fullname: "name" -User's full name (as opposed to the user's account name). The name is indicated in quotation marks.
  • / homedir: path -Specifies the path to the user's home directory. The specified location must exist.
  • / passwordchg: (yes | no) -Indicates whether the user can change his password (default can).
  • / passwordreq: (yes | no) -Indicates whether the user account should have a password (by default, it should).
  • / profilepath [: path] -Specifies the path to the user's logon profile.
  • / scriptpath: path -The path to the script used by the user to log on.
  • / times: (time | all) -Time to login. The time parameter is specified in the format day [-day] [, day [-day]], hour [-hour] [, hour [-hour]], and the increment is 1 hour. The names of the days of the week can be indicated in full or in abbreviated form. Hours can be specified in 12- or 24-hour terms. For a 12-hour presentation, the notation is am, pm, a.m. or p.m. The value all corresponds to no time limit for logging on to the system, and an empty value indicates that logon is completely prohibited. The day of the week and time values \u200b\u200bare comma separated; multiple entries for day of week and time values \u200b\u200bare separated by semicolons.
  • / usercomment: "text" -Allows the administrator to add or change a comment for the account.
  • / workstations: (computername [, ...] | *) -Allows you to specify up to 8 computers from which the user can log on to the network. If you do not specify a list of computers for the / workstations parameter, or you specify *, the user can log on to the network from any computer.

NET USER Command Examples

  • To display a list of all users on this computer, use the command: net user;
  • To display information about user "petr" use the following command: net user petr;
  • To add a Petr user account with a full username and the right to connect from 8 am to 5 pm, Monday through Friday, use the following command: net user petr / add / times: Mon-Fri, 08: 00-17: 00 / fullname: "Petr".
  • To delete an account, enter the command: net user petr / delete;
  • To disable account must enter the command: net user petr / active: no.

Video - Working with the NET USER utility

Before you start learning about commands, you need to have a look at some of the initial information you need to learn more about the command line.

Command line syntax.

Each command has a specific syntax structure, according to which the user enters the command on the command line. For example, take the CD command, which is used to change the current directory. It has the following syntax cd [drive:] [path] [..]. Most of the command line commands have various parameters and switches. As a rule, a key is denoted by a letter of the Latin alphabet preceded by a vertical slash "/" (in some commands, keys may begin with a minus sign "-"). In our case, the CD command has one key. [drive:] [path] and [..] are parameters of the CD command. If any keys and parameters are written in square brackets, then their use in the command is optional. For example, in our case, for the CD command, the key and parameters are optional, i.e. the CD command can be used without them. For example, let's take another XCOPY command that allows you to copy files and folders. It has the following syntax: xcopy source [result]]] [(/ a | / m)]] [+ [file3]] [(/ y | / -y)]. As you can see, the command has many different options and options. However, only one parameter is required: "source". When entering a command on the command line, you must adhere to the syntax, i.e. enter keys and parameters (if necessary) in the order in which they are presented in the command syntax.

Register when typing commands.

You can type the names of commands, parameters and keys in any case, since it is irrelevant when working on the command line.

Working with the file system.

The first lessons on the command line will be devoted to working with the Windows file system, i.e. work with files and folders. Therefore, a few words should be said about the specifics of defining paths to files and folders. The file path is written as follows: [drive] [path \\] filename. Thus, it turns out that only "file_name" is a required parameter. In this case, if the file name is preceded by a "\\" character, then the route to this file is calculated from the root directory, if the file name is used without the "\\" character, then the route to it is calculated from the current directory. For example, let's create a file “text.txt” on the C drive. Let's start the command line. By default, the current directory is C: \\ Users \\ username. In order to open any file using the command line, you just need to write down the name of this file. However, the route to this file should be displayed correctly here. If we just type text.txt, then the message "" text.txt "is not an internal or external command, executable program or batch file will appear on the screen. The fact is that the “text.txt” file is located in the root of the “C” drive, and our current directory is the “username” folder (in my case, “vadim”) located in the “Users” folder on the “C” drive. If we need the route to the file to be counted from the root of the disk, we must put the "\\" sign in front of the file: \\ text.txt

Now let's go to the root of the C drive (this can be done using the CD \\ command). Now, to open the file "text.txt", it is enough to write text.txt in the command line without using the "\\" sign, since the current directory is drive "C", and the file is located in the root of drive "C"

Now let's create a file "file.txt" in the "Users" folder and change the current directory to "C: \\ Users \\ username" (in my case, "username" is "vadim"). This can be done using the command: cd users \\ username... The file "file.txt" is located neither in the root of the "C" drive, nor in the current directory, so to open it, you must specify not only the file name, but also the directory in which it is located (in our case, "Users"). If we write simply users \\ file.txt, then the message “The system cannot find the specified path” will be displayed on the screen, since this very “system” is trying to find the file “file.txt” in the “Users” folder, starting from the current directory, those. along the route C: \\ Users \\ vadim \\ Users \\ file.txt. If we write \\ users \\ file.txt in the command line, then the system will look for the file “file.txt” in the “Users” folder, starting from the root of the “C” drive, i.e. along the route "C: \\ Users \\ file.txt" which is correct.

If the file is located on another disk, then to access it, you must specify the full path with the disk name. For example, let's create a folder “FOLDER” on disk “D”, and in it the file “f1.txt”. Then the command to open the file "f1.txt" will look like this: d: \\ folder \\ f1.txt

Special designations are used to designate the current directory and its top 3 levels. So the current directory is indicated by the symbol "." (dot), its parent directory - by the ".." symbol, the 2nd level directory - by the "..." symbol, the 3rd level directory - by the "...." symbol. These symbols are used in some commands. For example, the command CD .. navigates to the parent directory.

Using wildcards.

When working in the command line, filenames can be replaced with wildcards, "*" (asterisk) and "?" (question mark). The "*" character replaces any number of characters in the file name, and the "?" indicates the presence or absence of one character in the file name. For example, the entry "* .txt" refers to all text files. The entry "file. *" Denotes files with the name "file" and any extension. The "*. *" Entry stands for all files. “Fi? E.txt” can mean any text file named file.txt, fie.txt, fife.txt, fi4e.txt, etc.

When using file names with more than one word, you must enclose them in double quotes. For example, let's create a “new folder” folder on the C drive and a “new document” file in it. Then, to open this file, the path to it must be enclosed in double quotes.

This article discusses the general syntax for shell commands ( shell) and demonstrates its use by the example of several elementary commands.

Formally, the command syntax is defined as follows:

Utility_name

Here, utility_name means name commands or utilities, options - accepted by the command the keys or optionsand operands is arguments teams. The square brackets around options and operands indicate that these options are optional and may not be present.

Let's write our "definition" in more detail:

Utility_name [-a] [-b] [-c optarg] [-d | -e] [-foptarg]

The key is a minus character followed by a single letter or number. Several keys can be combined under one minus symbol. For example, the -a and -b switches can be written as -ab. Some options may contain additional arguments (like optarg in -c and -f) that follow the option either immediately (-f) or separated from it by a space or tab character (-c). If the option accepts several arguments, they are written either separated by commas (optarg1, optarg2), or in quotes separated by spaces ("optarg1 optarg2"). Some keys can be mutually exclusive. In the example above, these are -d and -e, the pipe character indicates that these options are mutually exclusive and only one of them should be used. If such switches are specified together on the command line, then, as a rule, the one that was encountered last will be effective. Finally, command arguments, if any, appear after all options. In the example above, the ellipsis symbol indicates that there can be multiple arguments. In this case, they are separated by spaces or tabs.

A special separator can be used to indicate the end of options -. Anything after it is accepted by the command as its argument, even if it starts with a minus character, which is otherwise interpreted as an option character. For instance:

Command -abc -d optarg - -operand1 operand2

Here, -operand1 acts as an argument to the command, not as an option, despite the fact that it starts with a minus.

When a single minus character is used as a command argument, which is a filename, it denotes either standard input or standard output, depending on the context of the command.

Most of the utilities developed by the GNU project support, in addition to "short" one-letter keys, so-called "long" options, which are one or more words separated by a hyphen. Long options are preceded by two minus symbols. The general syntax for using long options can be written like this:

Utility_name [--longopt1] [--logopt2 \u003d optarg1] [--longopt3 optarg2]

The rules for writing option arguments differ from the "short" ones in that the combined spelling of an option and its argument is not allowed, but the argument must be separated by either a space (as in --longopt3) or an "equal" symbol (as in --longopt2). These notation forms are equivalent.

In many utilities that support "long" options, as a rule, there is a one-to-one correspondence between "short" and "long" options, that is, each "short" key corresponds to one "long" option. However, the opposite statement is not always true, and the "long" option does not always have an analogue among the "short" keys.

So, with the boring theory out of the way, let's see how it all looks in practice.

The interaction of the operator with the command shell can be simplified as follows: the operator enters a command from the keyboard, the system performs some action defined by this command, and if there is any output, it displays it on the screen.

Let's analyze the following example of a dialogue between an operator and the system:

$ whoami bofh $ hostname -s lucky $ uname -sr FreeBSD 7.1-STABLE $ echo Hello, world. Hello, world.

In this example, the dollar sign ($) means the prompt ( prompt) of the command shell, the commands entered by the operator are highlighted in bold text, and the system output is shown in plain text.

In the first command (whoami), the operator tries to find out what his name is, or rather under what user this session of work with the shell is started. As you can see, the system responded with the username bofh. Whoami does not require any parameters, it has no keys or arguments. You can try to convey them to her:

$ whoami -h whoami: illegal option - h usage: whoami $ whoami sometext usage: whoami

As you can see, the whoami utility has kindly reported that it does not know what the operator asked it for the -h switch. And the usage: whoami line suggests the command syntax.

The second command (hostname) allowed the operator to know which host the shell was running on. The operator specified the -s switch, which in this case instructs the hostname utility to display only the "short" host name, that is, without the domain part (in other words, up to the first dot).

By executing the third command (uname), the operator was able to find out which operating system he was dealing with, as well as the OS version. Keys can be written together, as demonstrated in the example above, or separately, and then each key must be preceded by a minus symbol:

$ uname -s -r FreeBSD 7.1-STABLE

Finally, the fourth command (echo) simply prints out the arguments passed to it.

As we can see, commands can have many different keys and arguments. Their number differs from utility to utility, their meaning differs, options can be "short" and "long", etc. Moreover, in different operating systems, the same command can have a different set of keys, which can be even more confusing.

It is quite natural that it is not possible to remember all the parameters of certain commands. This is not necessary. There is a way to quickly brush up on the syntax and options of a command - using the -h and / or --help options, if the utility supports them. For example, in the example of the whoami command in FreeBSD above, it was shown that this command does not know about any keys and, moreover, is rather laconic. In GNU / Linux OS, the output looks a little different and more detailed:

$ whoami -h whoami: invalid option - "h" Try `whoami --help" for more information. $ whoami --help Usage: whoami ... Print the user name associated with the current effective user ID. Same as id -un. --help display this help and exit --version output version information and exit

As a rule, the command that supports "long" options almost always has the --help option. But, as mentioned above, this option only allows you to brush up on how to use this command and its parameters. We'll talk about where to find more detailed information about a particular team in.

THEORETICAL PART

To some files from its structure, the operating system allows direct user access to perform some action or operation. The action will be performed using the programs written in these files, and the indication for such an action is called team

Internal operating system commands

The COMMAND.COM file describes the rules for a computer to execute simple, most frequently used commands. DOS commands that are written as codes in the COMMAND.COM file are called internal teams. The data from the COMMAND.COM file are loaded into memory for the entire session, so the system starts executing internal commands immediately. Here are examples of internal DOS commands, the execution codes of which are described in the COMMAND.COM file.

Command Internal command description
cd Display the name of the current directory or change directory
copy Copy a file or multiple files to a specified location
date Display the current date and change it if necessary
del, erase Delete specified files
dir List files and subdirectories in the current or specified directory
exit Terminate the work of the command processor COMMAND.COM and transfer control to the program from which it was launched
md, mdir Create directory or subdirectory
path Specify directories where DOS should look for executable files (programs)
rd, rmdir Remove directory
ren, rename Change the name of the selected file or files
time Display the system time and change it if necessary
type Display the contents of the specified file
verify Check and report the results of writing files to disk

Duplicate commands and abbreviated spellings are written separated by commas. The internal team does not require specifying who will carry it out.

External DOS Commands

In addition to system files, the DOS package includes a large set of programs written in separate files with the .corn, .exe extensions. A program from such a file is specially loaded into memory from disk only at the request of the command. This takes time, so the call pointing to the file name with the DOS program is called external teamDOS. External command is the name of the program that will perform the expected action; the command does not specify the file extension.

The COMMAND.COM file accepts various DOS commands from the keyboard, but only executes internal commands and command set files itself. When faced with a command that it is not ready to execute internally, the COMMAND.COM file will scan the list of directory paths and load the corresponding program file from disk for execution.

Programs for executing external commands come with DOS, are stored on disk in the DOS directory as regular files, and perform servicing actions.

Submitting a command

Command - this is an order to perform actions, operations.

1. A general-purpose command can be typed on the keyboard in Latin letters, while typing its text is displayed on the screen in the DOS prompt.

2. The command can be assigned to a keyboard key (hot key).

3. A command in programs with windowed graphic design can be described by selecting its parameters with a keyboard or mouse cursor according to the graphic display on the screen: menus, windows provided by the program.

Structure, command syntax

Above were only the names of the commands, but in most cases the command only begins with a name, and then additional elements must be written. The rules of writing and the order of the command elements are called syntax teams.

A command can contain four types of elements: command name (required at the beginning), parameters, keys (switches) and values. They determine the version of command execution: who (codes of which program, commands), over which object and how will perform the required actions. Command elements are separated by spaces.