Linux Command Example | Run Multiple Commands in a Line

In this linux command Example, we will introduce you how to run multiple commands in a line.

There are three methods:

OperatorSyntaxDescription
Semicolon (;)command 1; command2Execute command 1 first and then command 2
AND (&&)command 1 && command2Execute command 2 only if command 1 runs successfully
OR (||)command 1 || command2Execute command 2 only if command 1 fails

We will use some examples to explain.

Method 1: command 1; command2

For example:

pwd; cd magesh; uptime

It means linux will run pwd, cd magesh and uptime one by one.

Run this line command, you may get:

/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

Method 2: command 1 && command2

For example:

mkdir MyDirectory && cd MyDirectory

It means linux will execute cd MyDirectory after running mkdir MyDirectory successfully.

Method 3: command 1 || command2

For example:

cd cpufetch || ls -lh

It means ls -lh only can be run if cd cpufetch is failed.