EDU BLOG

Mar 01, 2025

Running Multiple Commands in One Line in Linux

Running multiple commands in one line can save a lot of time and help make working in Linux more efficient and easier.

In Linux, there are three ways to run multiple commands in one line:

  • ; command1 ; command2: Run command1 first, and then run command2.
  • && command1 && command2: Run command2 only if command1 succeeds.
  • || command1 || command2: Run command2 only if command1 fails.

Below is a detailed explanation of how to use multiple commands in Linux.

Using ; to Run Multiple Linux Commands

The simplest way is by using the semicolon ;. You just combine the commands you want to execute:

1
cmd1; cmd2; cmd3

Here, cmd1 will run first. Regardless of whether cmd1 succeeds or fails, cmd2 will run next. Once cmd2 finishes, cmd3 will run.

Example:

1
2
root@pc: mkdir new_dir; cd new_dir; pwd
/home/new_dir

In the above example, the mkdir command is used to create a new directory called new_dir. Then, the cd command changes to the newly created directory. Finally, the pwd command prints the current directory.

The space after the semicolon ; is optional but helps make the command more readable.

Using && to Run Multiple Linux Commands

Sometimes, you want the next command to run only if the previous one succeeds. In this case, you use the logical AND operator &&:

1
cmd1 && cmd2 && cmd3

For example, on Ubuntu or Debian-based systems, you might run:

1
sudo apt update && sudo apt upgrade

Here, the first command refreshes the package database. If it succeeds, the second command will upgrade all outdated packages.

Using the earlier example, if new_dir already exists, the mkdir command will return an error and output an error log:

1
2
root@pc: mkdir new_dir && cd new_dir && pwd
mkdir: cannot create directory ‘new_dir’: File exists

Using || to Run Multiple Linux Commands

You can also use the logical OR operator || to run multiple commands, but this will only execute the next command if the previous one fails, which is the opposite of how && works.

1
cmd1 || cmd2 || cmd3

If cmd1 fails, cmd2 will run. If cmd2 succeeds, cmd3 will not run.

Example:

1
2
3
root@pc: mkdir new_dir || cd new_dir || pwd
mkdir: cannot create directory ‘new_dir’: File exists
root@pc:/home/new_dir#

As shown, the mkdir new_dir command fails because new_dir already exists. Since this command fails, the next command cd new_dir runs successfully. After that, since cd succeeds, pwd will not run.

Combining && and ||

You can combine operators to run two or more Linux commands.

If you combine three commands with && and ||, it behaves like a ternary operator in C/C++.

1
cmd1 && cmd2 || cmd3

For example, you can check if a file exists in bash and print a message accordingly:

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

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

1
2
3
4
5
6
7
root@pc: [ -f file.txt ] && echo "File exists" || echo "File doesn't exist"
File doesn't exist

root@pc: vi file.txt

root@pc: [ -f file.txt ] && echo "File exists" || echo "File doesn't exist"
File exists

Running multiple commands in one line in the Linux terminal is one of many time-saving command-line tricks. Although simple, this is a basic concept every Linux terminal user should know.


Other

Cheap VPS Recommendations Page:

OLDER > < NEWER