| by Arround The Web | No comments

How to Run Multiple Commands at once in Linux

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.

Operator Syntax Description
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.

Common Syntax:

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
Concatenate commands with the Semicolon operator (;)

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.

Common Syntax:

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
Run two or more commands at once in Linux using the Logical AND operator (&&)

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.

Common Syntax:

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
Execute multiple Linux commands with the Logical OR operator (||)

Closing Notes

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.

The post How to Run Multiple Commands at once in Linux first appeared on 2DayGeek.

Share Button

Source: 2DayGeek

Leave a Reply