Structure, command syntax. Command syntax. Additional NET USER Command Options

Before you begin learning the commands, you need to get acquainted with some initial information that is necessary for further exploration of the command line.

Command line command syntax.

Each command has a specific syntactic structure according to which the user enters the command at the command line. For example, let's take the CD command, which is used to change the current directory. It has the following syntax cd [drive:][path] [..]. Most command line commands have various parameters and switches. Typically, a key is indicated 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 to 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, the CD command has a key and parameters that are optional, i.e. The CD command can be used without them. For example, let's take another command, XCOPY, which 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 keys and parameters. However, only one parameter is required: "source". When entering a particular command on the command line, you must adhere to the syntax, i.e. enter switches and parameters (if required) in the order in which they are presented in the syntactic structure of the command.

Register when typing commands.

You can type the names of commands, parameters and switches in any case, because it has no meaning when running on the command line.

Working with the file system.

The first command line lessons will be devoted to working with the Windows file system, i.e. working with files and folders. Therefore, a few words should be said about the features of determining paths to files and folders. The path to the file is written as follows: [drive][path\]file_name. Thus, it turns out that only “file_name” is a required parameter. Moreover, if the file name is preceded by a “\” sign, then the route to this file is calculated from the root directory; if the file name is used without the “\” sign, then the route to it is calculated from the current directory. For example, let’s create a file “text.txt” on drive “C”. Let's launch the command line. By default, the current directory is "C:\Users\username". In order to open a file using the command line, you just need to write down the name of the file. However, the route to this file must be correctly displayed here. If we simply 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 “user_name” folder (in my case “Vadim”), located in the “Users” folder on the “C” drive. If we need the route to a file to be counted from the root of the disk, we need to put a “\” sign in front of the file: \text.txt

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

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”). You can do this using the command: cd users\username. The file “file.txt” is located neither in the root of drive “C” nor in the current directory, so to open it you need to specify not only the name of the file, 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 same “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 on 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. via the route "C:\Users\file.txt" which is correct.

If the file is located on another drive, then to access it you must specify the full path indicating the drive name. For example, let’s create a folder “FOLDER” on drive “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 notations are used to designate the current directory and its 3 upper levels. So the current directory is indicated by the symbol “.” (dot), its parent directory - with the symbol "..", the 2nd level directory - with the symbol "...", the 3rd level directory - with the symbol "....". These notations are used in some commands. For example, the CD.. command changes to the parent directory.

Using wildcards.

When working on the command line, file names 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" denotes all text files. The entry "file.*" denotes files with the name "file" and any extension. The entry "*.*" denotes all files. The entry "fi?e.txt" can represent any text file named file.txt, fie.txt, fife.txt, fi4e.txt, etc.

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

Using the already described dir command as an example, let's look at the rules for writing commands in the MS-DOS system. The specified command can be represented in general form as dir [path]

As you understand, this command should not be entered into the computer in this form. This notation specifies only the syntax of the command and is possible on paper, but not on the command line. The command entry begins with its name (in this example, the name is dir). The command name is followed by a space and then the drive name, which is indicated by d:. Because in some cases the drive may not be specified (see examples above), this part of the command is enclosed in square brackets. These parentheses indicate that a given command element may be missing. Naturally, the square brackets themselves should not be typed when entering a command. The drive name in the shared entry is followed by the path. It is also not necessary to specify it, in this case the contents of the root directory will be displayed on the screen.

MS-DOS commands may include so-called switches that set the conditions for executing the command. So, if you type /P after the dir command, the contents of the directory will be displayed page by page. This mode is useful for large directories whose contents may span several screen pages. For the SCHOOL directory, the paging command will look like: dir c:school/p

In addition to the P switch, the dir command can have a W switch, which specifies the output of only file names - five names on each line. The possibility of having switches in a command is reflected by an entry like: dir [path]

Any MS-DOS command can be roughly represented as:

Command-name [argument]... [switch]...

The ellipsis in these entries means that the command element preceding the ellipsis can be repeated an arbitrary number of times. The ellipsis should not be included in the command itself. An argument is usually understood as the object to which the command is applied (file name, directory name, etc.). Arguments are typically separated from the command name and from each other by a space.

Creating and deleting directories

You are already familiar with several commands for working with directories: dir, tree, cd. Let's supplement this list with commands designed to create and delete directories. These are the md and rd commands respectively. The md command can be written as mkdir, which is short for “make directory” - create a directory. The rd command can also be written in the more verbose form rmdir (short for “remove directory”). The syntax for the md and rd commands is the same:

md and rd

Using the md command, you can create a tree structure of directories by specifying the path to the new directory. Here are some examples:

Create a GRAPH3 directory in the current directory;

Create a REST directory in the root directory of drive C.

The rd command allows you to remove directories other than the current directory. For example, to delete the HOBBY directory on drive C, you need to type:

Note that the directory deleted by the rd command must be empty, that is, all files and subdirectories in it must first be deleted, and the deletion procedure must begin from the lowest level (operations with files will be discussed in the next paragraph). Versions of MS-DOS starting from 6.0 provide for the deletion of directories along with all their contents. To do this, you need to use the deltree command, which has a format similar to the md and rd commands:

deltree [path]

All deletions are made with confirmation, but if you specify the /y parameter in the command, then confirmation is not required. When you need to delete a directory (such as EXERC) contained in the current directory, you simply type deltree exerc

Before deleting a directory, the system will ask you to confirm the deletion. Type Y (Yes) and press Enter.

This article covers the general syntax of shell commands ( shell) and an example of several elementary commands shows its use.

Formally, the command syntax is defined as follows:

Utility_name

Here, utility_name means name teams or utilities, options - accepted by the command keys or options, and operands - arguments teams. Square brackets around options and operands indicate that these parameters are optional and may be missing.

Let’s write our “definition” in more detail:

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

The key is a minus symbol followed by a single letter or number. Several keys can be combined under one minus symbol. For example, the -a and -b options can be written as -ab . Some options may contain additional arguments (like optarg in -c and -f), which are written after the option either immediately (-f) or separated from it by a space or tab character (-c). If an option takes several arguments, they are written either separated by commas (optarg1,optarg2) or in quotes separated by spaces ("optarg1 optarg2"). Some keys may be mutually exclusive. In the example above, these are -d and -e , the vertical bar symbol indicates that these keys 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 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 more than one argument. In this case, they are separated by spaces or tabs.

A special delimiter -- can be used to indicate the end of options. Anything after it is treated as an argument by the command, even if it begins with a minus character, which is otherwise interpreted as an option character. For example:

Command -abc -d optarg -- -operand1 operand2

Here -operand1 appears as a command argument rather than as an option, even though it starts with a minus.

If a single minus character is used as a command argument, which is a file name, it denotes either standard input or standard output, depending on the context in which the command is used.

Most of the utilities developed within the GNU Project support, in addition to “short” single-letter switches, 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 using long options can be written as follows:

Utility_name [--longopt1] [--logopt2=optarg1] [--longopt3 optarg2]

The rules for writing option arguments differ from “short” ones in that writing an option and its argument together is not allowed, but requires separating the argument with either a space (as in --longopt3) or an “equals” symbol (as in --longopt2). These recording 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, having finished with the boring theory, let's see how it all looks in practice.

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

Let's look at this example of an operator's dialogue with the system:

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

In this example, the dollar sign ($) indicates a prompt ( prompt) 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 his name, or rather, under which user this shell session is running. As you can see, the system responded with the username bofh. The whoami command does not require any parameters and has no options 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 kindly reported that it does not know what the -h key the operator gave it. And the line usage: whoami tells you the command syntax.

The second command (hostname) allowed the operator to know which host the shell was running on. In this case, 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 recorded together, as demonstrated in the example above, or separately, and then each key must have a minus symbol in front of it:

$ uname -s -r FreeBSD 7.1-STABLE

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

As we can see, commands can have many different options and arguments. Their number varies from utility to utility, their meaning varies, options can be “short” and “long”, etc. Moreover, in different operating systems the same command may 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 refresh your memory of the syntax and keys of a particular command - using the -h and/or --help options, if the utility supports them. For example, above, in the example of the whoami command in the FreeBSD OS, it was shown that this command does not know about any keys and is also quite laconic. In GNU/Linux OS, the output looks slightly 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, a command that supports long switches almost always has a --help option. But, as mentioned above, this option only allows you to refresh your memory of how to use this command and its parameters. Where to find more detailed information about a particular team will be discussed in.

02/12/15 21.3K

Why is there such chaos in the world? Yes, because the administrator of our system forgot to fulfill his duties. Or I just lost the list of cmd commands from our world. Although this is a somewhat original look at the existing order of things, it nevertheless reflects part of the truth we need: using the command line, you can easily bring order to your computer:

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

in the Windows operating system

On UNIX-based systems, you can use the mouse when working with the command line.

Some 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, the most rarely used commands are included in this set of commands.

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

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

Working with the command line in the Windows operating system

You can call the console in Windows in several ways:

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, you need to select the program icon in the Start menu and select the appropriate item in the context menu:


After launching the utility, you can get help information about commands and the format for writing them in the console. To do this, enter the help statement and press “Enter”:

Basic commands for working with files and directories

The most frequently 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 files only, 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 enables the display of a permission dialog for deleting each file:


More information about the possible values ​​of the “processing method” parameter can be found in 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 within the test1 folder. If one of the path's root folders does not exist, it will be created too:

  • RD ( RMDIR) – deleting a specific folder or all directories at a specified path. Syntax:

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

The example uses the s flag, which will cause the entire branch of directories specified in the path to be deleted. Therefore, you should not use the rmdir command unnecessarily with this processing key.

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

Commands for working with the network

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

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

ping [-t] [-a] [-n counter] [-l size] [-f] [-i TTL] [-v type] [-r counter] [-s counter] [(-j host_list | - k node_list)] [-w interval] [target_PC_name]

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

In the last example of the cmd ping command, the 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 – used to determine the network path to a 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 travels passes is displayed. The first element in the list is the first router on the side of the requested resource.

The cmd command tracer syntax is:
tracert [-d] [-h maximum_hops] [-j node_list] [-w interval] [target_resource_name]
Example implementation:
tracert -d -h 10 microsoft.com

The example traces the route to a specified resource. This increases the speed of the operation due to the use of 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 using the set value of the h parameter. 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 shut down (s) after 60 seconds (t). This will force you to log out of all applications (f) and the current user's session (l).

) is widely used as the standard notation, but in the early days people experimented. On some machines, the sort command no longer supports the + notation. However, various commands (notably ar and tar) accept controls without a prefix character - and dd (mentioned by Alok in a comment) uses a different convention altogether.

The GNU convention of using " -- " for long options (supported by getopt_long(3)) has been modified to use " + ". Of course, X11 software uses a single dash before multi-character options. So, this is all a collection of historical relics from when people experimented with how best to handle it.

POSIX documents the Utility Conventions for which it operates, except where historical precedent is stronger.

What options processing styles are there?

[At one time SO 367309 contained the following material as my response. It was originally asked on 2008-12-15 02:02 FerranB, but was subsequently closed and removed.]

How many different types of options do you recognize? I can think of many, including:

  • Single-letter options preceded by a single dash are grouped when there are no arguments, the argument can be attached to the option letter or in the next argument (many, many Unix commands, most POSIX commands).
  • Single-letter options preceded by a single dash, grouping is not allowed, arguments must be appended (RCS).
  • Single letter options preceded by a single dash, grouping is not allowed, arguments must be separate (pre-POSIX SCCS, IIRC).
  • Multi-letter options that are preceded by a single dash can have arguments prefixed or in the next argument (X11 programs, as well as Java and many programs on Mac OS X with the NeXTSTEP legacy).
  • Multi-letter options preceded by a single dash can be abbreviated (Atria Clearcase).
  • Multi-letter options preceded by a single plus (obsolete).
  • Multi-letter options preceded by a double dash; the arguments may follow the "=" or be separate (GNU utilities).
  • Parameters without prefix/suffix, some names have abbreviations or implied arguments must be separate. (AmigaOS Shell)

For options that take an optional argument, it is sometimes necessary to append the argument (co -p1.3 rcsfile.c), sometimes it must be followed by a "=" sign. POSIX does not meaningfully support extra arguments (POSIX getopt() only allows them for the last one on the command line).

All reasonable option systems use an option consisting of a double bar (" -- ") only to indicate the "end of options" - the following arguments: "optional arguments" (usually file names, POSIX calls them "operands") even if they begin with a dash . Note that if a -- parameter is preceded by an option that requires an argument, -- will be treated as an option argument rather than as an "end of options" marker.)

Many, but not all programs accept a single dash as a file name to indicate standard input (usually) or standard output (sometimes). Sometimes, as with GNU "tar", both can be used on the same command line:

... | tar -cf - -F - | ...

The first personal dash means "write to stdout"; the second one means "read file names from stdin".

Some programs use other conventions - that is, parameters that are not preceded by a dash. Many of them date back to the oldest days of Unix. For example, "tar" and "ar" both accept parameters without a dash, so:

Tar cvzf /tmp/somefile.tgz some/directory

The dd command uses opt=value exclusively:

Dd if=/some/file of=/another/file bs=16k count=200

Some programs allow options and other arguments to be alternated entirely; the C compiler, make and GNU utilities work without POSIXLY_CORRECT in the environment - examples. Many programs expect parameters that precede other arguments.

Note that git and other VCS teams often use a hybrid system:

Git commit -m "This is why it was committed"

There is a subcommand as one of the arguments. Often there will be additional "global" parameters added that can be specified between the command and subcommand. POSIX has examples; sccs command is in this category; you could argue that some other commands that run other commands also fall into this category: nice and xargs spring, to take POSIX into account; sudo is a non-POSIX example, as are svn and cvs.

I don't have strong preferences between the different systems. when there are multiple options, then single letters with mnemonic meaning are convenient. GNU supports this, but recommends multi-letter options preceded by a double dash.

There are some things I object to. One of the worst is that the same option letter is used with different meanings depending on what other option letters precede it. In my book that's a no-no, but I know the software where this is done.

Another undesirable behavior is inconsistency in the style of handling arguments (especially within a single program, but also within a set of programs). Either one requires attached arguments or requires detaching arguments (or resolves either), but lacks some options that require an attached argument and others that require a separate argument. And be that "=" can be used to separate an option and an argument.

As with many things, many (software related) things - consistency is more important than individual decisions. Using tools that automate and standardize argument processing helps with consistency.

Whatever you do, read TAOUP Command Line Options and review Standards for Command Line Interfaces. (Added by J F Sebastian - thank you; I agree.)