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: Runcommand1first, and then runcommand2.&&command1 && command2: Runcommand2only ifcommand1succeeds.||command1 || command2: Runcommand2only ifcommand1fails.
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 | root@pc: mkdir new_dir; cd new_dir; pwd |
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 | root@pc: mkdir new_dir && cd new_dir && pwd |
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 | root@pc: mkdir new_dir || cd new_dir || pwd |
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 | root@pc: [ -f file.txt ] && echo "File exists" || echo "File doesn't exist" |
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: