> ## Content Index
> Fetch the complete content index at: https://itsfoss.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# How to Run Multiple Linux Commands at Once in Terminal
- URL: https://itsfoss.com/run-multiple-commands-linux/
- Published: 2020-09-15T12:28:11.000Z
- Updated: 2023-05-16T04:42:12.000Z
- Description: Running two or more commands in one line can save you a good deal of time and help you become more efficient and productive in Linux.
- Author: Abhishek Prakash
- Tags: Terminal

Running two or more commands in one line can save you a good deal of time and help you become more efficient and [productive in Linux](https://itsfoss.com/productivity-tips-ubuntu/).

There are three ways you can run multiple commands in one line in Linux:

| Operator | Example                 | Explanation                                      |
| -------- | ----------------------- | ------------------------------------------------ |
| ;        | Command 1 ; Command 2   | Run command 1 first and then command 2           |
| &&       | Command 1 && Command 2  | Run command 2 only if command 1 ends sucessfully |
| \||      | Command 1 \|| Command 2 | Run command 2 only if command 1 fails            |

Let me show you in detail how you can chain commands in Linux.

## Using ; to run multiple Linux commands in one line

The simplest of them all is the semicolon (;). You just combine several commands that you want to run using `;` in the following fashion:

```
cmd1; cmd2; cmd3
```

Here, `cmd1` will run first. Irrespective of whether `cmd1` runs successfully or with error, `cmd2` will run after it. And when `cmd2` command finishes, `cmd3` will run.

Let’s take an example you can practice easily (if you want to).

```
mkdir new_dir; cd new_dir; pwd
```

In the above command, you [create a new directory](https://itsfoss.com/make-directories/) named `new_dir` with mkdir command. Then you [switch to this newly created directory](https://itsfoss.com/change-directories/) using cd command. Lastly, you print your current location with pwd command.

![Running Multiple Commands Linux](https://itsfoss.com/content/images/wordpress/2020/09/running-multiple-commands-linux.png)

Running Multiple Commands Linux with ;

📋

The space after the semicolon (;) is optional but makes the chain of commands easily readable.

## Using && to run multiple Linux commands

Sometimes you want to ensure that the in the chain of Linux commands, the **next command only runs when the previous command ends successfully**. This is where the logical AND operator `&&` comes into the picture:

```
cmd1 && cmd2 && cmd3
```

If you use Ubuntu or Debian based distributions, you must have come across this command that utilizes && concept:

```
sudo apt update && sudo apt upgrade
```

Here, the first command (sudo apt update) refreshes the package database cache. If there is no error, it will then upgrade all the packages that have newer versions available.

Let’s take the earlier example. If the `new_dir` already exists, mkdir command will return an error. The difference in the behavior of `;` and `&&` can be seen in the screenshot below:

![Multiple Linux Commands One Line](https://itsfoss.com/content/images/wordpress/2020/09/multiple-linux-commands-one-line.png)

Did you see how commands separated by `&&` stopped when the first command resulted in an error?

## Using || to run several Linux commands at once

You can use the logical OR operator (||) to run a chain of commands but the **next command only runs when the previous command ends in error**. This is opposite to what you saw with &&.

```
cmd1 || cmd2 || cmd3
```

If cmd1 fails, cmd2 runs. If cmd2 runs successfully, cmd3 won’t run.

![Linux Chain Commands](https://itsfoss.com/content/images/wordpress/2020/09/linux-chain-commands.png)

In the screenshot above, `mkdir new_dir` command fails because \`new\_dir already exists. Since this command fails, the next command `cd new_dir` is executed successfully. And now that this command has run successfully, the next command `pwd` won’t run.

## Bonus Tip: Combine && and || operators

You may combine the operators to run two or more Linux commands.

If you combine three commands with `&&` and `||`, it will behave as the [ternary operator in C/C++](https://www.w3schools.com/cpp/cpp%5Fconditions%5Fshorthand.asp?ref=itsfoss.com) ( condition ? expression\_true ; expression\_false).

```
cmd1 && cmd2 || cmd3
```

For example, you can [check if file exists in bash](https://linuxhandbook.com/bash-check-if-file-exists/?ref=itsfoss.com), and print messages accordingly.

```
[ -f file.txt ] && echo "File exists" || echo "File doesn't exist"
```

Run the above command before and after creating the file.txt file to see the difference:

![Combine Multiple Commands Linux](https://itsfoss.com/content/images/wordpress/2020/09/combine-multiple-commands-linux-800x300.png)

You can also use `;`, `&&` and `||` to run multiple commands in bash scripts as well.

## More such Linux terminal tips

Like [copy-paste in Linux terminal](https://itsfoss.com/copy-paste-linux-terminal/), running multiple commands at once is also one of the many [Linux command line tips for saving time](https://itsfoss.com/linux-command-tricks/).

[21 Super Handy Linux Command Tips and Tricks That Will Save you a lot of Time and Increase Your ProductivityHere are some tiny but useful Linux commands, terminal tricks and shortcuts that will save you a lot of time while working with Linux command line.![](https://itsfoss.com/content/images/size/w256h256/2022/12/android-chrome-192x192.png)It's FOSSAbhishek Prakash![](https://itsfoss.com/content/images/wordpress/2021/07/linux-command-tips.png)](https://itsfoss.com/linux-command-tricks/)

Though elementary, it is an [essential concept any Linux terminal user should know](https://itsfoss.com/basic-terminal-tips-ubuntu/). Here are a few more.

[19 Basic But Essential Linux Terminal Tips You Must KnowLearn some small, basic but often ignored things about the terminal. With the small tips, you should be able to use the terminal with slightly more efficiency.![](https://itsfoss.com/content/images/size/w256h256/2022/12/android-chrome-192x192.png)It's FOSSAbhishek Prakash![](https://itsfoss.com/content/images/wordpress/2021/12/ubuntu-terminal-basic-tips.png)](https://itsfoss.com/basic-terminal-tips-ubuntu/)

And if you are [absolutely new to Linux commands](https://itsfoss.com/tag/terminal-basics/), I have created a series of tutorials that will help you.

[Linux Command Tutorials for Absolute BeginnersNever used Linux commands before? No worries. This tutorial series is for absolute beginners to the Linux terminal.![](https://itsfoss.com/content/images/size/w256h256/2022/12/android-chrome-192x192.png)It's FOSS![](https://itsfoss.com/content/images/2023/03/terminal-basics-series-tag.png)](https://itsfoss.com/tag/terminal-basics/)

I hope you liked this terminal trick. Stay tuned for more Linux command tips and tools.