As a Linux administrator, you may know how useful the command line can be to work with various activity such as installing the application, installing the system patch, and restarting the service.
Running two or more commands at once is even more efficient and saves good time.
In this tutorial, we’ll see the different ways to combine and execute multiple Linux commands efficiently.
|Semicolon ||command 1; command2||Execute command 1 first and then command 2|
|AND ||command 1 && command2||Execute command 2 only if command 1 runs successfully|
|OR ||command 1 || command2||Execute command 2 only if command 1 fails|
1) Concatenate commands with the Semicolon operator (;)
If you want to execute all commands regardless of whether the previous ones failed or not, separate them with semicolons. This executes all commands one after another.
command 1; command 2; … command N
For instance: Just enter the following three commands in one line, separated by semicolons. This will show you the login name (whoami), check which directory you’re currently in ( pwd ), and how long the system has been running (uptime).
whoami; pwd; uptime linuxgeek /home/linuxgeek 03:35:48 up 2:00, 2 users, load average: 0.49, 0.45, 0.51
As i said, if one command fails in the command chain, the remaining commands will always execute as shows below.
pwd; cd magesh; uptime /home/linuxgeek bash: cd: magesh: No such file or directory 03:36:00 up 2:00, 2 users, load average: 0.72, 0.50, 0.53
2) Run two or more commands at once in Linux using the Logical AND operator (&&)
If you want to execute each command only when it’s previous command runs successfully, then combine them using the ‘&&’ operator.
command 1 && command 2 && … command N
For instance: Type the following two commands in one line, separated by two ampersands ( && ). This will create a directory called ‘MyDirectory’ and then change to that directory.
mkdir MyDirectory && cd MyDirectory
Re-executing the above command will fail because ‘MyDirectory’ already exists. Therefore, ‘command 1’ returns an error. The ‘AND’ operator will run ‘command 2’ only if ‘command 1’ runs successfully.
mkdir MyDirectory && cd MyDirectory mkdir: cannot create directory ‘MyDirectory’: File exists
3) Execute multiple Linux commands simultaneously with the Logical OR operator (||)
If you want to run the next command only if the previous command failed, concatenate them to the ‘||’ operator.
The ‘OR’ operator will only execute ‘command 2’ if ‘command 1’ fails or returns an error. Similarly, if ‘command 1’ runs successfully, ‘command 2’ won’t execute.
command 1 || command 2 || … command N
For instance: Here, ‘command 1’ ran successfully because we entered the ‘cpufetch’ directory, so command 2 ‘ls -lh’ is not executed.
cd cpufetch || ls -lh
When you re-execute the above command, ‘Command 2’ ran successfully because ‘command 1’ failed. So, we can list the contents of the ‘cpufetch’ directory.
cd cpufetch || ls -lh bash: cd: cpufetch: Not a directory total 100K -rw-r--r-- 1 linuxgeek users 3.0K Jul 29 18:20 CONTRIBUTING.md -rwxr-xr-x 1 linuxgeek users 74K Jul 29 18:28 cpufetch -rw-r--r-- 1 linuxgeek users 2.4K Jul 29 18:20 cpufetch.1 drwxr-xr-x 1 linuxgeek users 98 Jul 29 18:20 doc -rw-r--r-- 1 linuxgeek users 1.1K Jul 29 18:20 LICENSE -rw-r--r-- 1 linuxgeek users 2.1K Jul 29 18:20 Makefile drwxr-xr-x 1 linuxgeek users 132 Jul 29 18:20 pictures -rw-r--r-- 1 linuxgeek users 5.0K Jul 29 18:20 README.md drwxr-xr-x 1 linuxgeek users 24 Jul 29 18:20 src
In this tutorial, we’ve shown you the different ways to combine and execute multiple Linux commands efficiently using the Semicolon operator (;), the Logical AND operator (&&), and the Logical OR operator (||).
If you have any questions or feedback, feel free to comment below.
2 Comments on “How to Run Multiple Commands at once in Linux”
The phrase “at once” implies that the commands are going to be run at the same time.
While you have done a fine job of outlining cases where you can enter multiple commands on the same command line, they do not run in parallel or “at once.” By your own explanation, this is true.
; between two commands = run command 1 then run command 2
&& between two commands = run command 1 and if it finishes successfully, run command 2
|| between commands = If command 1 fails, run command 2
I was really expecting something that talked about background processes, which DOES entail running two commands at once.
None of those run multiple commands at once, they are all sequential. To run at the same time need & after first command.