[ad_1]
These days, about 50% of my time spent on Linux is via GUI applications. Of course, I also work with servers, so I do have to work with the command line as well. 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.
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.
Also: How to install Ubuntu Linux (It’s easy!)
However, there’s also my second option — and that’s 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 run multiple Linux commands at once
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.
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:
The above command creates a new file called zdnet_test.
The next command will add content to the file. That command looks something like this:
echo "Hello, ZDNET!" > zdnet_test
You can now view the contents of the new file with the command:
The output of the command will print “Hello, ZDNET!”.
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.
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.
[ad_2]
Source link