Using && in Linux to Chain Commands

In Linux, the symbol && is commonly referred to as the "AND operator" or the "logical AND operator". It can be used to chain commands or run them inline one after the other. It is extremely useful in situations where it's important to ensure that each command completes successfully before moving on to the next line of code.

You can use && in bash shell scripts or command line operations.
&& operator chain commands in Linux

How to chain commands using && operator

The syntax for using the logical AND operator is to separate each command with the two ampersands (&&). To use it to chain multiple commands, simply type the first command, followed by &&, second command &&, third and so on. Multiple commands can be daisy chained into a string.

For example, here are three chained commands on one single line or string of code:

command1 && command2 && command3

How the && command chain works:

In the example shown above, we are using && to run commands sequentially (one after the other). In this case, command1 is executed first. If command1 completes successfully, then command2 will be executed next. If command2 is also successful, then command3 will finally be executed. However, if any of the chained commands fail or return an error, subsequent commands after it will not be executed.

Tip * The next command only runs if the previous command exits with a success status (i.e., exit status 0).

How to Copy a file and then delete the original

To copy a file named "file.txt" to a directory named "backup" and then delete the original file, you would use the "cp command" like this:

cp file.txt backup/ && rm file.txt

This code will copy the file to the backup directory (assuming it exists), and if the copy was successful, it will proceed to remove the origin file. Note that if copying fails, the second chained "rm command" to remove the file will not execute. This use case could help you save disk space by removing a redundant file, while ensuring that a copy exists before doing so.

Copy directory and subdirectories then delete the directory

To recursively copy a directory including any subdirectories and files within to a "backup" directory, and then delete the original directory, you could chain the "cp and rm commands" as follows:

cp -r folder/ backup/ && rm -rf folder

This command copies the entire "folder" along with any subfolders and files within, recursively using option (-r) to our "backup" directory (preserving the folder contents i.e. backup/folders and all files). Then, only if copying was successful, the original folder you copied from along with all of its contents (subfolders and files) will be recursively (-r) and forcefully (-f) deleted via the && chained (rm) remove here command.

To conclude, those were just a few examples on how to run multiple commands in sequential precession by using the logical AND operator (&&) ampersands. Hopefully this section has helped you better understand how to chain commands on unix-based systems.

If you found this section useful, you might also be interested in learning how to clear bash history or perhaps rename file from the terminal.