How to run multiple Linux command in terminal as a single entry

Debian multiple commands in terminal 179 times1 answer1 follower
0
Jeff Szydel 17 Rep. Avishek Kumar edited question

How to execute several Linux commands in terminal as a single command-line entry?
How to run multiple Linux command in terminal at once.

♥ Spread the love ♥
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
1
Avishek Kumar 449 Rep. Avishek Kumar edited answer

There are several ways to run multiple command in Linux Terminal in one go. Here are some of the examples.

  1. The Logical AND operator

  2. The Logical AND operator is denoted by &&. Here the execution of the second command depends upon the exit status of the first command. It means second command will run if the first command executed successfully. If not, second command won’t run.

    Example

    apt-get upgrade will run only when apt-get update executes successfully.

    File README will only be created if directory /home/$USER/test is created/exists.

    Logical AND is the safest way to perform something like below.

    This is considered safe because rm -rf * won’t run without successful execution of first part cd /home/$USER/work_space. Consider what catastrophe it could be if the first command couldn’t execute (due to any error/typo) and the later executes.

  3. Semicolon operator

  4. The semicolon operator is denoted by ;
    Semicolon operator runs multiple commands in sequential order. It means the second command in sequence runs after the first command execution completes and the third command will execute when execution of second command finishes, however, it is important to note here that the execution of commands in sequence occurs independent of the exit state of the previous command. Hence the second command in sequence will run when the first command execution finishes, but doesn’t matter if it was completed successfully or not.

    Example

    Sample Output

    Extra attention to be taken when you are running something like below.

    It is very dangerous to run something like the above example. The reason is second command is going to be executed anyhow no matter what the exit status of the first command. If you have a typo in the name of the directory or mistakenly wrote the name of the directory that doesn’t exists, the second command will delete everything in your current working directory.

  5. The Logical OR operator

  6. The Logical OR operator is denoted by ||
    The logical OR operator executes the second command only when the exit status of the first command is not successful.

    Example

    Notice the OR operator here. Commands to the right of OR operator will be executed only when the command to its left fails. In the above example, I am pinging to one of the critical production servers the IP of which is 52.77.24.56 for count=4. Till ping is successful the second part doesn’t executes. However, if the server/Internet goes down and ping command fails, you will hear an audible notification that the server is down. The script sleeps for 10 seconds before starting the process again and it continues.

  7. The Logical NOT operator

  8. The Logical NOR operator also called as Negation operator is represented by !
    Though this (NOT operator) doesn’t come under the category of multiple command execution at once, but to serve the purpose and for the sake of completeness of this post, it is mentioned here.

    Lets consider a hypothetical situation to understand NOT operator in detail. We have a directory /home/files which contain 1000’s of files of various extensions like

    Now we need to move all the files except *.xyz to location /opt/files. In the most general case, what will you do?

    With ! Not operator you can move all of them except *.xyz, in one go simply as

    You can run any command like cp, rm, etc with Negation operator. Be careful not to run rm and other similar commands this way. You know why?

  9. The command combination operator

  10. As the name suggests command combination operator combines more than one command and it is denoted by {}. The execution of the command depends upon the exit status of the previous command, here.

    Example

    Check is the directory /home/$user/Desktop/dokws exist or not. If not Print a message “Directory not found” and create it.

    You can create nested directories of any number using command combination operator in one go. Say you want to create a directory structure like below

    All you need to do is

  11. The Precedence operator

  12. The precedence operator is useful in running the command in precedence order. It is denoted by ().

    Example

    Here command2 will run only if the exit status is command1 is successful. Command3 will run if the exit status of the first part is not-successful. Command4 execution depends upon the exit status of command3.

  13. Pipe operator

  14. This is one of the most widely used operators in Linux and shell. Pipe operator is denoted by |.
    This operator sends the output of one command as the Input of another command.

    Example

    Sample Output

    Sample Output

  15. Ampersand operator

  16. The ampersand operator is another one of the most widely used operators in shell and shell scripting language. It is denoted by & and is useful in sending a command in the background.

    Example

    Last but not the least, if you have same set of multiple commands that you need to run frequently, it is best to make proper use above operators and put it in a shell script so that you don’t have to remember long commands. Moreover, it automates the task and decrease human error significantly.

♥ Spread the love ♥
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
×

Login