How to run 2 or more Terminal commands at the same time on Linux
If you use Linux, you probably know how to use useful commands to work with files, install software and launch programs. However, one thing you don't know is that you can still run multiple commands at the same time.
Combining 2 or more commands at the same time is also called "command chaining". In the following article, Network Administrator will guide you how to run 2 or more Terminal commands at the same time on Linux.
1. Semicolon (;) Operator (semicolon (;))
The Semicolon (;) operator is a semicolon character (;) that allows you to execute multiple consecutive statements, including whether or not the previous statements were successfully executed.
For example, open up Terminal window (use Ctrl + Alt + T on Ubuntu and Linux Mint). Then you enter each of the three commands below, each separated by a semicolon (;) and press Enter. On the screen will display the list of current directories (ls), the directory you are currently using (pwd) and display the username (whoami) at the same time.
ls; pwd; whoami
Also, you don't need to use spaces after the semicolon (;) and spaces between statements. You can enter 3 commands, ls; pwd; whoami . However, using spaces to make it easier for users to read.
2. Logical AND Operator (&&)
If you want to run the second command after running the first command successfully, you can split the statements using logical AND operator, which is 2 characters (&&). For example, if you want to create a folder called MyFolder and then change this folder, enter the following command into the Terminal window and press Enter:
mkdir MyFolder && cd MyFolder
The directory will be created successfully, then the cd command will be executed and you are opening the new folder.
3. Logical OR Operator (||)
In some cases, if you want to execute the second command in case the first command fails, you can use logical OR operator, which is 2 characters (||).
For example, if you want to check if the MyFolder folder exists ([-d ~ / MyFolder]) and create a new MyFolder folder if this folder does not exist ((mkdir ~ / MyFolder), you enter the command below enter the Terminal window and press Enter:
[-d ~ / MyFolder] || mkdir ~ / MyFolder
Note:
Make sure there is a space in the above statement.
In the above example, the MyFolder folder does not exist, so the second statement is to create the directory.
4. Multiple Operator Combinations
Also you can combine multiple operators on one command. For example, you first want to check if a file exists or not ([-f ~ / sample.txt]). If you want to display a message on the screen, use (echo 'File exists.'). If the file does not exist, you can create a new file (touch ~ / sample.txt).
You combine these commands into a single command, enter the Command Prompt window and press Enter:
[-f ~ / sample.txt] && echo 'File exists.' || touch ~ / sample.txt
In the above example, the file does not exist so it needs to create a new file.
Below is a summary of the commands:
- A; B - Run command A and run command B, even running A successfully or failed.
- A && B - Only run B if running A successfully.
- A || B - Only run B if running A command.
Refer to some of the following articles:
- Anyone should know these basic Linux commands
- Certain deadly commands never run on Linux
- Learn the file system and folders on Linux operating systems
Good luck!
You should read it
- 14 interesting Linux commands in Terminal
- 6 interesting terminal commands on Mac
- Common Terminal commands in Raspberry Pi
- How to use Terminal on a Mac
- How to execute Linux commands in the background
- The Cat command in Linux
- How to fix 'apt-get: command not found' error in Linux Terminal
- 7 commands to manipulate the most basic files and folders everyone must know
- How to use Guake Terminal in Linux
- How to Launch Programs from Command Line on Linux
- 18 terminal commands on Chromebook you should know
- How to use the history command in Linux
Maybe you are interested
How to use the file search command on Windows, find saved files
Why is tldr command better than man command in Linux?
Should I use PowerShell or Command Prompt?
What to do when open command window here does not appear?
How to switch users on the Linux command line
How to fix Mac Homebrew error 'zsh: command not found: brew'