Command Combination

CHEATSHEET

    • A; B Run A and then B, regardless of success of A

    • A && B Run B if A succeeded

    • A || B Run B if A failed

    • A & Run A in background.

Running command in single line.

1st Choice

ls ; pwd ; whoami

2nd Choice ||| The Logical AND Operator (&&)

If you want the second command to only run if the first command is successful, separate the commands with the logical AND operator, which is two ampersands ( && ).

mkdir DirData && cd DirData

3rd Choice ||| The Logical OR Operator (||)

The logical OR operator, or two vertical bars ( || ). For example, we want to check to see if the MyFolder directory exists ( [ -d ~/Documents ] ) and create it if it doesn’t ( mkdir ~/RexTmpora ).

[rx@localhost ~]$ [ -d ~/Documents ] || mkdir ~/RexTmpora

[rx@localhost ~]$ cd RexTmp/

Combining the command

[ -f ~/sample.txt ] && echo “File exists.” || touch ~/sample.txt

sudo apt-get update && sudo apt-get install pyrenamer

&& can be equivalently paraphrased IN a script.

if sudo apt-get update ; then

sudo apt-get install pyrenamer

fi