Home> System Tutorial> LINUX> body text

With one machine in hand, Linux commands are worry-free - master the skills of using multiple commands in the terminal

王林
Release: 2024-02-12 12:40:18
forward
503 people have browsed it

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 fails

Use ; symbol to run multiple commands

The simplest one is the semicolon (;), which is used as follows:

cmd1; cmd2; cmd3
Copy after login

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]#
Copy after login

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.
With one machine in hand, Linux commands are worry-free - master the skills of using multiple commands in the terminal
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
Copy after login

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:
With one machine in hand, Linux commands are worry-free - master the skills of using multiple commands in the terminal
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
Copy after login

If cmd1 fails to run, run cmd2. If cmd2 runs successfully, cmd3 will not run.
With one machine in hand, Linux commands are worry-free - master the skills of using multiple commands in the terminal
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"
Copy after login

Run the above command before and after creating the file.txt file and see the difference:
With one machine in hand, Linux commands are worry-free - master the skills of using multiple commands in the terminal

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!

source:lxlinux.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!