Multiple command in one line linux

Execute combine multiple Linux commands in one line

If you want to execute each command only if the previous one succeeded, then combine them using the && operator:

cd /my_folder && rm *.jar && svn co path to repo && mvn compile package install 

If one of the commands fails, then all other commands following it won’t be executed.

If you want to execute all commands regardless of whether the previous ones failed or not, separate them with semicolons:

cd /my_folder; rm *.jar; svn co path to repo; mvn compile package install 

In your case, I think you want the first case where execution of the next command depends on the success of the previous one.

You can also put all commands in a script and execute that instead:

#! /bin/sh cd /my_folder \ && rm *.jar \ && svn co path to repo \ && mvn compile package install 

The backslashes at the end of the line are there to prevent the shell from thinking that the next line is a new command; if you omit the backslashes, you would need to write the whole command in a single line.

A more convenient way compared to using backslashes and && everywhere is to instruct the shell to exit the script if any of the commands fail. You do that using the set built-in function with the -e argument. With that, you can write a script in a much more natural way:

#! /bin/sh set -e cd /my_folder rm *.jar svn co path to repo mvn compile package install 

Save that to a file, for example myscript , and make it executable:

You can now execute that script like other programs on the machine. But if you don’t place it inside a directory listed in your PATH environment variable (for example /usr/local/bin , or on some Linux distributions ~/bin ), then you will need to specify the path to that script. If it’s in the current directory, you execute it with:

The commands in the script work the same way as the commands in the first example; the next command only executes if the previous one succeeded. For unconditional execution of all commands, simply don’t call set -e :

#! /bin/sh cd /my_folder rm *.jar svn co path to repo mvn compile package install 

Источник

Читайте также:  Linux сделал исполняемым файл

How to Run Multiple Linux Commands in One Single Command

If you use Linux daily, you will know that the command line is the most powerful tool when you working with files, installing and configuring system software, and running them. It becomes even more efficient if you run two or more commands at once on the command line and save a good deal of time.

In this tutorial, we’ll see the different ways in which we can combine and execute multiple Linux commands efficiently in one single line.

; cmd1; cmd2 The “;” operator runs all commands regardless of whether the earlier ones are failed or not.
&& cmd1 && cmd2 The “&&” operator carries out the second command only if the preceding command executes successfully.
|| cmd1 || cmd2 The “||” operator executes the second command only if the precedent command returns an error.

Let me explain to you in more detail how you can execute multiple commands in Linux in one go.

1. Using Semicolon (;) Operator to Run Multiple Linux commands

The semicolon (;) operator enables you to run one or more commands in succession, regardless of whether each earlier command succeeds or not. For example, run the following three commands on one line separated by semicolons (;) and hit enter.

Run Multiple Commands Using Semicolon Operator

This will show you a listing of the current directory ( ls ), show which directory you’re currently in ( pwd ), print the disk usage of files (du), and display your account login name ( whoami ) all at once.

2. Using AND (&&) Operator to Run Multiple Linux Commands

In some scenarios, you want to make sure that the second command only executes if the first command is executed successfully. For example, run the two commands separated by (&&) operator, which is two ampersands.

$ sudo apt update && sudo apt upgrade

Here the first command updates the package database lists for packages that need upgrading. If there are no errors, it will execute the second command that will upgrade all the packages to a newer version.

I highly recommend using the (&&) operator rather than using (;) semicolon operator most of the time. This assures that you don’t do anything terrible. For example, if you run the following command to change to a directory and then delete everything recursively in that directory, you could end up destroying your system if the directory change didn’t take place.

3. Using (||) Operator to Run Several Linux Commands

At times you might want to run a second command only if the first command returns an error. To do this, you need to use (||) operator. For example, you want to verify that if the MyFolder directory exists and create it if it doesn’t.

$ [ -d ~/MyFolder ] || mkdir ~/MyFolder

In the above example, the MyFolder directory does not exist, so the second command creates the directory.

Читайте также:  Kali linux и arduino
Conclusion

In this article, we have learned the three useful ways in which we can combine and run multiple Linux commands in the command line productively.

Источник

Running multiple commands in one line in shell

So, /templates/apple will be copied to /templates/used AND /templates/inuse and then after that I’d like to remove the original.

Is cp the best way to do this, followed by rm ? Or is there a better way?

I want to do it all in one line so I’m thinking it would look something like:

cp /templates/apple /templates/used | cp /templates/apple /templates/inuse | rm /templates/apple 

Is this the correct syntax?

6 Answers 6

You are using | (pipe) to direct the output of a command into another command. What you are looking for is && operator to execute the next command only if the previous one succeeded:

cp /templates/apple /templates/used && cp /templates/apple /templates/inuse && rm /templates/apple 
cp /templates/apple /templates/used && mv /templates/apple /templates/inuse 

To summarize (non-exhaustively) bash’s command operators/separators:

  • | pipes (pipelines) the standard output ( stdout ) of one command into the standard input of another one. Note that stderr still goes into its default destination, whatever that happen to be.
  • |& pipes both stdout and stderr of one command into the standard input of another one. Very useful, available in bash version 4 and above.
  • && executes the right-hand command of && only if the previous one succeeded.
  • || executes the right-hand command of || only it the previous one failed.
  • ; executes the right-hand command of ; always regardless whether the previous command succeeded or failed. Unless set -e was previously invoked, which causes bash to fail on an error.

Why not cp to location 1, then mv to location 2. This takes care of «removing» the original.

And no, it’s not the correct syntax. | is used to «pipe» output from one program and turn it into input for the next program. What you want is ; , which seperates multiple commands.

cp file1 file2 ; cp file1 file3 ; rm file1 

If you require that the individual commands MUST succeed before the next can be started, then you’d use && instead:

cp file1 file2 && cp file1 file3 && rm file1 

That way, if either of the cp commands fails, the rm will not run.

Источник

Run Multiple Commands in One Line in Ubuntu

Run multiple Linux commands at once

You have probably seen multiple commands in a single line, executing at once, and you might have tried some without realizing or understanding what exactly is going on.

Читайте также:  Настройка цветов в linux

Running multiple commands at once is a time-saving skill while dealing with the command line, and every Linux user should know about them.

Let me help you understand what they are.

Control Operators in Bash

Basically, there are three operators that control the flow of the commands. They are helpful in combining different commands. They are:

They modify the control flow based on the success or failure of commands, which we will discuss, one by one.

The semicolon (;) operator

The ; operator is the simplest of all. The commands separated by a semicolon are executed sequentially.

The syntax of this command is:

Here, a new directory (new_Dir) is created, then I switched to it, created a new file, and listed the contents of the directory. Even though one of the commands fails, the control flow is not broken; all commands are executed.

The and (&&) operator

The and (&&) operator is used to combine two commands, which behaves differently compared to the semicolon.

It executes the next command in the line if the output of the previous command turns out to be successful. That is just «exit code 0» in simpler terms.

The general syntax is shown below.

If sudo apt update fails, the upgrade command does not execute. Any exit code other than ‘0’ will not execute the other command.

Exit codes are another interesting topic too. There’s a whole universe beyond the exit codes 0 and 1, which you can read about here:

The or (||) operator

We can understand the ‘or’ operator like this, for the given syntax:

If mkdir fails, which implies the directory already exists, then the path is switched to that directory. If it does not exist already, then the directory is created (the first command is executed).

Bonus: Combining these operators

We can combine such operators in all sorts of ways to perform different operations based on select requirements.

A probably uncommon example would be checking the existence of something, like this:

[ -e apt ] && echo "Debian based distro" || echo "Not debian based"

Output of the conditional statement given above in Ubuntu and Fedora

See? That’s how exciting these operators can be! Single-line conditional statements, response-based execution of commands, you name it. A lot can be achieved by combining operators accordingly.

Recommended Read 📖: Wondering how I showed about the distribution I’m using? Check out this article that shows multiple ways regarding this.

Learn more commands to extend the chain!

Now, you know how to chain commands, it’s time to learn new commands and I would recommend you to start with the basics.

And for that purpose, we made a dedicated tutorial:

I hope you will find this guide helpful.

Источник

Оцените статью
Adblock
detector