Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I run two commands in one line in Windows CMD?

I want to run two commands in a Windows CMD console.

In Linux I would do it like this

touch thisfile ; ls -lstrh 

How is it done on Windows?

like image 219
flybywire Avatar asked Nov 08 '11 18:11

flybywire


People also ask

Can you run 2 command prompts at once?

Click Start, type cmd, and press Enter to open a command prompt window. In the Windows taskbar, right-click the command prompt window icon and select Command Prompt. A second command prompt window is opened.

How do you run two commands simultaneously in Windows?

Run Multiple Commands Through &, &&, and || sign The first one is using “&” Combine your two commands with “&” sign. This will allow your second command to run after the complete execution of the first command. The second command will run as soon as the first command completely runs.

How do I run two command lines?

Try using the conditional execution & or the && between each command either with a copy and paste into the cmd.exe window or in a batch file. Additionally, you can use the double pipe || symbols instead to only run the next command if the previous command failed.


2 Answers

Like this on all Microsoft OSes since 2000, and still good today:

dir & echo foo 

If you want the second command to execute only if the first exited successfully:

dir && echo foo 

The single ampersand (&) syntax to execute multiple commands on one line goes back to Windows XP, Windows 2000, and some earlier NT versions. (4.0 at least, according to one commenter here.)

There are quite a few other points about this that you'll find scrolling down this page.

Historical data follows, for those who may find it educational.

Prior to that, the && syntax was only a feature of the shell replacement 4DOS before that feature was added to the Microsoft command interpreter.

In Windows 95, 98 and ME, you'd use the pipe character instead:

dir | echo foo 

In MS-DOS 5.0 and later, through some earlier Windows and NT versions of the command interpreter, the (undocumented) command separator was character 20 (Ctrl+T) which I'll represent with ^T here.

dir ^T echo foo 
like image 154
djdanlib Avatar answered Oct 25 '22 11:10

djdanlib


A quote from the documentation:

  • Source: Microsoft, Windows XP Professional Product Documentation, Command shell overview
  • Also: An A-Z Index of Windows CMD commands

Using multiple commands and conditional processing symbols

You can run multiple commands from a single command line or script using conditional processing symbols. When you run multiple commands with conditional processing symbols, the commands to the right of the conditional processing symbol act based upon the results of the command to the left of the conditional processing symbol.

For example, you might want to run a command only if the previous command fails. Or, you might want to run a command only if the previous command is successful.

You can use the special characters listed in the following table to pass multiple commands.

  • & [...]
    command1 & command2
    Use to separate multiple commands on one command line. Cmd.exe runs the first command, and then the second command.

  • && [...]
    command1 && command2
    Use to run the command following && only if the command preceding the symbol is successful. Cmd.exe runs the first command, and then runs the second command only if the first command completed successfully.

  • || [...]
    command1 || command2
    Use to run the command following || only if the command preceding || fails. Cmd.exe runs the first command, and then runs the second command only if the first command did not complete successfully (receives an error code greater than zero).

  • ( ) [...]
    (command1 & command2)
    Use to group or nest multiple commands.

  • ; or ,
    command1 parameter1;parameter2
    Use to separate command parameters.

like image 36
Raihan Avatar answered Oct 25 '22 13:10

Raihan