X
Tech
Why you can trust ZDNET : ZDNET independently tests and researches products to bring you our best recommendations and advice. When you buy through our links, we may earn a commission. Our process

'ZDNET Recommends': What exactly does it mean?

ZDNET's recommendations are based on many hours of testing, research, and comparison shopping. We gather data from the best available sources, including vendor and retailer listings as well as other relevant and independent reviews sites. And we pore over customer reviews to find out what matters to real people who already own and use the products and services we’re assessing.

When you click through from our site to a retailer and buy a product or service, we may earn affiliate commissions. This helps support our work, but does not affect what we cover or how, and it does not affect the price you pay. Neither ZDNET nor the author are compensated for these independent reviews. Indeed, we follow strict guidelines that ensure our editorial content is never influenced by advertisers.

ZDNET's editorial team writes on behalf of you, our reader. Our goal is to deliver the most accurate information and the most knowledgeable advice possible in order to help you make smarter buying decisions on tech gear and a wide array of products and services. Our editors thoroughly review and fact-check every article to ensure that our content meets the highest standards. If we have made an error or published misleading information, we will correct or clarify the article. If you see inaccuracies in our content, please report the mistake via this form.

Close

How to run multiple Linux commands at once

When you finally start working with the Linux command line, you'll find numerous ways to make the process more efficient. By running two or more commands at once, you'll not have to wait until one finishes to start the next.
Written by Jack Wallen, Contributing Writer
man working on laptop
Getty Images/pixelfit

These days, the majority of my time spent on Linux is via GUI applications. That doesn't mean I can completely avoid the command line. Why? Partially because I also work with servers. But, every so often, I'll opt to use the command line on the desktop. For instance, when I need to run upgrades on a system, I'll open a terminal window and walk through the process manually.

Why? Because it's more efficient.

Also: Elive 3.8.34 is a thing of beauty that any old-school Linux user would love

With an Ubuntu-based system, that process often requires running a few commands. For that, I have two options: I can run a single command, wait for it to complete, run the next command, wait for it to complete, run the next command…you get the picture.

However, there's also my second option -- a much easier method, which allows you to run all of those commands from a single, typed line.

Now, before we continue, let me explain a few things. First, the commands you combine together do not run simultaneously. Instead, the first command will run and, when the first command completes, the second command will run (and so on). 

The next thing to keep in mind is that should the first command fail, the next commands will not run. Finally, this approach is different from piping commands (which I'll talk about in a later article), where the output of the first command serves as the input for the second command. 

Also: The best Linux laptops

What we're doing here is simply running multiple commands from a single typed line. These commands could be completely different. For example, you could list the contents of a directory, update apt, and then list the usage of your drives with the dh command. That's part of the beauty of this feature -- it's flexibility. You can also run a mixture of commands that require sudo privileges and commands that don't.

So, how is this done? It's very simple. Let me show you how.

How to combine Linux commands 

What you'll need: The only thing you'll need for this demonstration is a running instance of Linux. It doesn't matter what distribution you use, as this process is the same on all versions. Remember, to run certain admin-level commands, you'll need a user with sudo privileges. 

Also: How to choose the right Linux desktop distribution for you

With those things at the ready, let's run some commands. I'll first demonstrate the commands one at a time, so you can see how it all comes together.

1. Run your first command

Let's first run a simple command. We're going to create a new, empty, file. The command to be used is touch, and is run like this:

touch zdnet_test

The above command creates a new file called zdnet_test.

2. Run your second command

The next command will add content to the file. That command looks something like this:

echo "Hello, ZDNET!" > zdnet_test

3. View the contents of the file

You can now view the contents of the new file with the command:

cat zdnet_test

The output of the command will print "Hello, ZDNET!".

4. Run them all at once

Now that you know the command sequence (and what it does), let's combine them all together, such that we'll create the file, add content to the file, and view the file. The command for this will be:

touch zdnet_test && echo "Hello, ZDNET!" > zdnet_test && cat zdnet_test

As you can see, the && is the trick for combing commands together. It really is that simple.

5. One last trick

Remember, I said you would need sudo privileges for some commands? Let's say you want to update apt, run and upgrade, and then clean up your system by removing any unused dependencies. 

Also: How to get started with Git on Linux

The three individual commands for that are sudo apt-get update, sudo apt-get upgrade, and sudo apt-get autoremove. To combine them together, all three commands will require sudo privileges, so the command looks like this:

sudo apt-get update && sudo apt-get upgrade -y && sudo apt-get autoremove

Ah, but there's another trick. Instead of typing sudo for each command, you could use this:

sudo sh -c 'apt-get update && apt-get upgrade && apt-get autoremove'

What we've done above is use the sh command interpreter with the -c option, such that everything in single quotes gets sudo privilege escalation.

Also: Ready to ditch Windows for Linux? This is the ideal distro for you

And that's all there is to it. Although this approach won't save you a ton of time, it will prevent you from having to wait until one command completes before running the next. This can be very helpful when running a group of commands, each of which takes considerable time (such as a backup). 

Instead of having to wait for one command to complete before running the next, run them all at once, and walk away to take care of other business. That's efficient. 

Editorial standards