As Linux users, we often need to execute multiple commands at the same time in the terminal to improve efficiency and convenience. For beginners, however, the process can be very tedious and confusing. Today, we will introduce you to some techniques for using multiple commands in the Linux terminal, so that you can handle various tasks easily and happily.
Running two or more commands in one line can save a lot of time and increase efficiency in Linux. In Linux, there are three ways to run multiple commands in one line:
;
Command 1 ; Command 2 Run Command1 first, then Command2&&
Command 1 && Command 2 When Command1 runs successfully and ends, then run Command2||
Command 1 || Command 2 Run Command2 only when Command1 failsUse ; symbol to run multiple commands
The simplest one is the semicolon (;), which is used as follows:
cmd1; cmd2; cmd3
cmd1 will run first, whether cmd1 runs successfully or an error occurs, cmd2 will run after it, and when the cmd2 command completes, cmd3 will run.
[root@localhost ~]# mkdir /root/new_folder; cd /root/new_folder; touch test_file; pwd ; ls -l /root/new_folder total 0 -rw-r--r--. 1 root root 0 Sep 2 22:01 test_file [root@localhost new_folder]#
In the above command summary, first mkdir creates a folder in the /root directory, cd enters the previously created directory, touch creates an empty file, then pwd displays the current location, and ls -l views the files in the directory.
The space after the semicolon (;) may or may not be included, but the space makes the command line easier to read.
Use the && symbol to run multiple commands
Sometimes you want to ensure that in Linux commands, the next command will only run when the previous command ends successfully. This is where the logical AND operator&&
comes in:cmd1 && cmd2 && cmd3
If you use a Centos-based distribution, you must have come across this command that uses the && concept:
[root@localhost ~]# yum makecache && yum -y update
The first half of the command line summary updates the cache, and the second half updates all installation packages.
For example, if new_folder already exists, the mkdir command will return an error. The difference between ; and && can be seen in the screenshot below:
When an error occurs in the first command,&&
separated commands will stop executing subsequent commands.
Use the || symbol to run multiple commands
You can use the logical operator (||) to run the command line, but the next command will only be run if an error occurs in the previous command:
cmd1 || cmd2 || cmd3
If cmd1 fails to run, run cmd2. If cmd2 runs successfully, cmd3 will not run.
In the screenshot above, the command to create the folder fails because the folder already exists. Because this command failed, the next command cd /root/new_folder was executed successfully. This command has been run successfully, the next command pwd will not run.
Combining && and || operators
For example, one can check if the file exists and print a message accordingly.
[root@localhost ~]# [ -f file.txt ] && echo "File exists" || echo "File doesn't exist"
Run the above command before and after creating the file.txt file and see the difference:
This article details how to execute multiple commands at the same time by using various symbols and commands in the terminal, including using techniques such as semicolons, hyphens, backslashes, and pipes. By studying this article, you will be able to make better use of terminal tools and complete daily tasks and work efficiently. Whether you are a beginner or an experienced user, you can get a lot of practical tips and suggestions from it.
The above is the detailed content of With one machine in hand, Linux commands are worry-free - master the skills of using multiple commands in the terminal. For more information, please follow other related articles on the PHP Chinese website!