What is the purpose of "&&" in a shell command?

Captain picture Captain · Dec 22, 2010 · Viewed 405.9k times · Source

As far as I know, using & after the command is for running it in the background.

Example of & usage: tar -czf file.tar.gz dirname &

But how about &&? (look at this example: https://serverfault.com/questions/215179/centos-100-disk-full-how-to-remove-log-files-history-etc#answer-215188)

Answer

girasquid picture girasquid · Dec 22, 2010

&& lets you do something based on whether the previous command completed successfully - that's why you tend to see it chained as do_something && do_something_else_that_depended_on_something.