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
- 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
May be interested
- How to get an explanation of the command in a terminal with Kmdronce you know what a command does, you can use it freely in the future. if you're struggling with linux commands, you can use the kmdr tool to explain them.
- Common Terminal commands in Raspberry Piyou have a raspberry pi on hand, but not completely confident with linux? although the main desktop is easy to use, sometimes users need to rely on the command line entry in the terminal.
- 8 best free Terminal Emulator apps for Androidterminal emulator applications on android are handy when you want to execute commands within the android operating system just like on linux.
- How to use Guake Terminal in Linuxfor many years, the terminal in linux remained unchanged. after all, there may not be much to fix or improve in a command prompt window. but guake proved this thinking wrong.
- How to Launch Programs from Command Line on Linuxmost linux distributions have a graphical user interface that allows us to open programs just by clicking on the program icon in the apps menu. however, sometimes you may want to launch programs from terminal. terminal is a powerful tool that allows users to launch programs and manage linux systems through keyboard commands. today's tipsmake will show you how to launch programs from terminal on linux.
- 5 Linux commands every sysadmin needs to knowin a world full of new tools and diverse development environments, learning some basic sysadmin commands is practically essential for any developer or engineer.
- 7 commands to manipulate the most basic files and folders everyone must knowon windows or linux operating systems, there are a number of things that users can only do with commands. in the following article, the network administrator will introduce you to the 7 most basic commands that any user must 'understand' to perform operations faster.
- Use commands and some tips for Mac OS Xin this tutorial we will show you how to use some commands and tips for mac os x.
- How to install dictionaries in Linux Terminala locally stored dictionary utility can really be useful when no other dictionary is available. there are many options in the linux software world. among these, there is an option that runs fairly 'smoothly' from the system terminal, called sdcv.
- 18 terminal commands on Chromebook you should knowdo you know that chromebooks have an integrated terminal? chrome os developer shell, or crosh for short, allows users to access the command line interface, which can be used for debugging, testing, etc.