Jump to content

How to run multiple Linux commands at once


aum

Recommended Posts

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.

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.

 

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.

 

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. 

 

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. 

 

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. 

 

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.

 

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.

 

Source

Edited by Matt
Remove text format.
Link to comment
Share on other sites


  • Administrator

@aum mate you know better than anyone that coloring the text with black makes any topic unrenderable on dark themes, so please avoid doing that. Thanks.

  • Like 2
Link to comment
Share on other sites


On 7/30/2023 at 5:32 AM, Matt said:

@aum mate you know better than anyone that coloring the text with black makes any topic unrenderable on dark themes, so please avoid doing that. Thanks.

Thanks, Mate!

 

I didn't see any coloring of the text with black otherwise I'd have not posted it.  I don't know what happened?

 

Anyway, I'll be extra careful in my future posts.  

 

Best and warmest regards!

Link to comment
Share on other sites


38 minutes ago, aum said:

Thanks, Mate!

 

I didn't see any coloring of the text with black otherwise I'd have not posted it.  I don't know what happened?

 

Anyway, I'll be extra careful in my future posts.  

 

Best and warmest regards!

Weird You can't see. It's strange why this problem has not been solved until now, because it has been going on for a long time, almost 10 years, and I have personally pointed it out many times.
Apparently, this can only be caused by childish tantrums.
Here are the pictures/screenshots of page now, ie today on 03. Aug 2023 11:43 UTC:

  1. 1st picture - theme IPS Dark
  2. 2nd picture - theme IPS Night

And of course all have been copied with the text and formatting of the entire article,
and, of course, the black color added to the text specially (using the force method).

p style

element.style {
    color: #080a12;
    font-size: 1.125rem;
    padding: 0px;
}


and then, in addition, a semi-transparent/blurring "curtain" is also placed in front.

Why, dear people? 

And it's really, really good to read stuff like this.
Saves the eyes very well.
And of course, all those who point this out are terribly bad people.

Pictures:

1. theme IPS Dark

screenshot-nsaneforums.com-2023_08.03-14_28_57.thumb.png.59be4b3d9b7e5a889c071a608aa16e1a.png

 

2. theme IPS Night

screenshot2-nsaneforums.com-2023_08.03-14_30_43.thumb.png.9750dc5eb1aaf2f5a1ed42e4406ffad3.png

 

Article html code in spoiler (there you can see <div style="color:#080a12;font-size:16px;"> and <p style="color:#080a12;font-size:1.125rem;padding:0px;"> etc and this color:#080a12 is forced black text)

Spoiler
<div data-role="commentContent" class="ipsType_normal ipsType_richText ipsPadding_bottom ipsContained" data-controller="core.front.core.lightboxedImages">
<p>
<span style="font-size:22px;"><strong>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.</strong></span>
</p>
<p>
&nbsp;
</p>
<p style="color:#080a12;font-size:1.125rem;padding:0px;">
<span style="font-size:14px;">These days, the majority of&nbsp;<a href="https://www.zdnet.com/article/the-most-important-reason-you-should-be-using-linux-at-home/" rel="external nofollow noopener" style="color:#080a12;" target="_blank">my time spent on Linux</a>&nbsp;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.</span>
</p>
<div style="color:#080a12;font-size:16px;">
&nbsp;
</div>
<p style="color:#080a12;font-size:1.125rem;padding:0px;">
<span style="font-size:14px;">Why? Because it's more efficient.</span>
</p>
<p style="color:#080a12;font-size:1.125rem;padding:0px;">
&nbsp;
</p>
<p style="color:#080a12;font-size:1.125rem;padding:0px;">
<span style="font-size:14px;">With an&nbsp;<a href="https://www.zdnet.com/article/how-to-install-steam-on-any-ubuntu-based-linux-distribution-so-you-can-play-a-world-of-games/" rel="external nofollow noopener" style="color:#080a12;" target="_blank">Ubuntu-based system</a>, 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.</span>
</p>
<p style="color:#080a12;font-size:1.125rem;padding:0px;">
&nbsp;
</p>
<p style="color:#080a12;font-size:1.125rem;padding:0px;">
<span style="font-size:14px;">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).&nbsp;</span>
</p>
<div style="color:#080a12;font-size:16px;">
&nbsp;
</div>
<p style="color:#080a12;font-size:1.125rem;padding:0px;">
<span style="font-size:14px;">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.&nbsp;</span>
</p>
<p style="color:#080a12;font-size:1.125rem;padding:0px;">
&nbsp;
</p>
<p style="color:#080a12;font-size:1.125rem;padding:0px;">
<span style="font-size:14px;">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&nbsp;<em>dh&nbsp;</em>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.</span>
</p>
<p style="color:#080a12;font-size:1.125rem;padding:0px;">
&nbsp;
</p>
<p style="color:#080a12;font-size:1.125rem;padding:0px;">
<span style="font-size:14px;">So, how is this done? It's very simple. Let me show you how.How to combine Linux commands&nbsp;</span>
</p>
<p style="color:#080a12;font-size:1.125rem;padding:0px;">
&nbsp;
</p>
<p style="color:#080a12;font-size:1.125rem;padding:0px;">
<span style="font-size:14px;"><strong>What you'll need:&nbsp;</strong>The only thing you'll need for this demonstration is&nbsp;<a href="https://www.zdnet.com/article/how-to-enable-linux-on-your-chromebook-and-why-you-should/" rel="external nofollow noopener" style="color:#080a12;" target="_blank">a running instance of Linux</a>. 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.&nbsp;</span>
</p>
<p style="color:#080a12;font-size:1.125rem;padding:0px;">
&nbsp;
</p>
<p style="color:#080a12;font-size:1.125rem;padding:0px;">
<span style="font-size:14px;">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.</span>
</p>
<p style="color:#080a12;font-size:1.125rem;padding:0px;">
&nbsp;
</p>
<p style="color:#080a12;font-size:1.125rem;padding:0px;">
<strong><span style="font-size:14px;">1. Run your first command</span></strong>
</p>
<div style="color:#080a12;font-size:16px;">
<div>
<div>
<div>
<p style="font-size:1.125rem;padding:0px;">
&nbsp;
</p>
<p style="font-size:1.125rem;padding:0px;">
<span style="font-size:14px;">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:</span>
</p>
</div>
</div>
</div>
</div>
<div style="color:#080a12;font-size:16px;">
&nbsp;
</div>
<div style="color:#080a12;font-size:16px;">
<pre style="font-size:1em;padding:0px;"><span style="font-size:14px;">touch zdnet_test</span></pre>
</div>
<p style="color:#080a12;font-size:1.125rem;padding:0px;">
<span style="font-size:14px;">The above command creates a new file called zdnet_test.</span>
</p>
<p style="color:#080a12;font-size:1.125rem;padding:0px;">
&nbsp;
</p>
<p style="color:#080a12;font-size:1.125rem;padding:0px;">
<strong><span style="font-size:14px;">2. Run your second command</span></strong>
</p>
<div style="color:#080a12;font-size:16px;">
<div>
<div>
<div>
<p style="font-size:1.125rem;padding:0px;">
&nbsp;
</p>
<p style="font-size:1.125rem;padding:0px;">
<span style="font-size:14px;">The next command will add content to the file. That command looks something like this:</span>
</p>
</div>
</div>
</div>
</div>
<div style="color:#080a12;font-size:16px;">
<pre style="font-size:1em;padding:0px;"><span style="font-size:14px;">echo "Hello, ZDNET!" &gt; zdnet_test</span></pre>
</div>
<div style="color:#080a12;font-size:16px;">
<div>
<div>
<div>
<h2 style="font-size:2.25rem;padding:0px;">
<span style="font-size:14px;">3. View the contents of the file</span>
</h2>
</div>
</div>
</div>
<div>
<div>
<div>
<p style="font-size:1.125rem;padding:0px;">
<span style="font-size:14px;">You can now view the contents of the new file with the command:</span>
</p>
</div>
</div>
</div>
</div>
<div style="color:#080a12;font-size:16px;">
<pre style="font-size:1em;padding:0px;"><span style="font-size:14px;">cat zdnet_test</span></pre>
</div>
<p style="color:#080a12;font-size:1.125rem;padding:0px;">
<span style="font-size:14px;">The output of the command will print "Hello, ZDNET!".</span>
</p>
<div style="color:#080a12;font-size:16px;">
<div>
<div>
<div>
<h2 style="font-size:2.25rem;padding:0px;">
<span style="font-size:14px;">4. Run them all at once</span>
</h2>
</div>
</div>
</div>
<div>
<div>
<div>
<p style="font-size:1.125rem;padding:0px;">
<span style="font-size:14px;">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:</span>
</p>
</div>
</div>
</div>
</div>
<div style="color:#080a12;font-size:16px;">
<pre style="font-size:1em;padding:0px;"><span style="font-size:14px;">touch zdnet_test &amp;&amp; echo "Hello, ZDNET!" &gt; zdnet_test &amp;&amp; cat zdnet_test</span></pre>
</div>
<p style="color:#080a12;font-size:1.125rem;padding:0px;">
<span style="font-size:14px;">As you can see, the &amp;&amp; is the trick for combing commands together. It really is that simple.</span>
</p>
<div style="color:#080a12;font-size:16px;">
<div>
<div>
<div>
<h2 style="font-size:2.25rem;padding:0px;">
<span style="font-size:14px;">5. One last trick</span>
</h2>
</div>
</div>
</div>
<div>
<p style="font-size:1.125rem;padding:0px;">
<span style="font-size:14px;">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.&nbsp;</span>
</p>
&nbsp;
<p style="font-size:1.125rem;padding:0px;">
<span style="font-size:14px;">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:</span>
</p>
</div>
</div>
<div style="color:#080a12;font-size:16px;">
&nbsp;
</div>
<div style="color:#080a12;font-size:16px;">
<pre style="font-size:1em;padding:0px;"><span style="font-size:14px;">sudo apt-get update &amp;&amp; sudo apt-get upgrade -y &amp;&amp; sudo apt-get autoremove</span></pre>
</div>
<p style="color:#080a12;font-size:1.125rem;padding:0px;">
<span style="font-size:14px;">Ah, but there's another trick. Instead of typing sudo for each command, you could use this:</span>
</p>
<div style="color:#080a12;font-size:16px;">
<pre style="font-size:1em;padding:0px;"><span style="font-size:14px;">sudo sh -c 'apt-get update &amp;&amp; apt-get upgrade &amp;&amp; apt-get autoremove'</span></pre>
</div>
<p style="color:#080a12;font-size:1.125rem;padding:0px;">
<span style="font-size:14px;">What we've done above is use the&nbsp;<em>sh&nbsp;</em>command interpreter with the -c option, such that everything in single quotes gets sudo privilege escalation.</span>
</p>
<p style="color:#080a12;font-size:1.125rem;padding:0px;">
&nbsp;
</p>
<p style="color:#080a12;font-size:1.125rem;padding:0px;">
<span style="font-size:14px;">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).&nbsp;</span>
</p>
<p style="color:#080a12;font-size:1.125rem;padding:0px;">
&nbsp;
</p>
<p style="color:#080a12;font-size:1.125rem;padding:0px;">
<span style="font-size:14px;">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.</span>
</p>
<p style="color:#080a12;font-size:1.125rem;padding:0px;">
&nbsp;
</p>
<p style="color:#080a12;font-size:1.125rem;padding:0px;">
<span style="font-size:14px;"><strong><a href="https://www.zdnet.com/article/how-to-run-multiple-linux-commands-at-once/" rel="external nofollow noopener" target="_blank">Source</a></strong></span>
</p>
</div>

 

 

Link to comment
Share on other sites


  • Administrator
1 hour ago, aum said:

I don't know what happened?

It is always best to use the remove format button we have on the editor, and then you format the topic as you wish from title and text etc.. Also, it would be nice if you edit the topic and do that in order to be readable for everyone now and in the future.

 

@Kalju I always appreciate your wisdom. I have tried to work on a script that strip away all copied formatting/CSS Styles on paste action in the editor, but IPS Ckedior has limits and doesn't allow everything. I hope they will have a solution for this on their IPS 5 version since they will have Dark/Light theme included.

Link to comment
Share on other sites


6 hours ago, Matt said:

It is always best to use the remove format button we have on the editor, and then you format the topic as you wish from title and text etc..

 

Thanks @Matt.  A really helpful and actionable advice -- focused on solving the problem.   Much appreciated.

Link to comment
Share on other sites


For the record, the command "apt-get" and "aptitude" has been replace since several years (probably when "Debian 8 aka Jessie" was published).

If you need one example : "apt update && apt full-upgrade" @aum you can take this one.

But they didn't remove yet ? That's right (even "Debian 12 aka BookWorm") has the both preinstalled ("apt-get --help" and "aptitude --help" inside the .iso official).

Despite the fact a huge number of people use "apt" (everyday), there is a drawback because a lot of people doesn't want to learn sic... To resume, they read one old topic over internet and that's enough for them.

Unless a big change (announce), as you can assume this kind of change will need a very long time (years or decades).

 

About the dark mode ? I don't use it at all but @Kalju@Matt you can probably fixes these troubles with the file called "userContent.css" (which work's for Firefox and Vivaldi (follow this guide)).

 

@-moz-document domain(nsaneforums.com)
{
	#cb-nexxe__topbar{position:unset !important;}
 	/* 1) Create a folder called "chrome" in your profil (because usually it doesn't exist). */
 	/* 2) Then put (recpoy) the file inside this folder.
 	/* Every change will be apply but for that it's require a full reboot of your web browser. */
 	/* How to avoid the reboot ? There is probably an addon but I won't be able to give any advice. */  
}

Otherwise you can use an addon but according to my memory, it's better to use an alternative to Stylus (which has a bad story)

Link to comment
Share on other sites


12 hours ago, Ecarion said:

For the record, the command "apt-get" and "aptitude" has been replace since several years ..

For the record, I use commend apt-get almost every day.  In fact just checked and it works!

Link to comment
Share on other sites


Black text under the dark theme has been a 'problem' for years, but at the end those who want to read the thread always find a way to do so ;) It's just not as direct as it should be.

Anyway in this windows world it's always nice to read threads about linux. Thanks to those who post.

Link to comment
Share on other sites


5 hours ago, mp68terr said:

... those who want to read the thread always find a way to do so ;) ...

And it takes less energy ...  overall.  ;)

5 hours ago, mp68terr said:

Anyway in this windows world it's always nice to read threads about linux. Thanks to those who post.

Nice to see an appreciative kind heart!  🙂

Link to comment
Share on other sites


  • 2 weeks later...
On 8/4/2023 at 3:21 PM, aum said:

For the record, I use commend apt-get almost every day.  In fact just checked and it works!

Apparently there is some kind of confusion. Because I had never said the opposite (read carefully again the 3rd line (which confirm the fact I had check it myself)).

 

PS : In the case, you still don't understand that's not my trouble and it will be useless to reply (from my point of view, it's very clear and I was a helper for almost 2 decades (on the internet)).

Link to comment
Share on other sites


8 hours ago, Ecarion said:

Apparently there is some kind of confusion.

 

On 8/3/2023 at 8:47 PM, Ecarion said:

For the record, the command "apt-get" and "aptitude" has been replace[d] since several years (probably when "Debian 8 aka Jessie" was published) ...  But they didn't remove yet ? That's right (even "Debian 12 aka BookWorm") has the both preinstalled ("apt-get --help" and "aptitude --help" inside the .iso official).

 

 

On 8/3/2023 at 8:47 PM, Ecarion said:

...

8 hours ago, Ecarion said:

In the case, you still don't understand that's not my trouble and it will be useless to reply

 

With respect and best and warmest regards @Ecarion

 

Edited by aum
Link to comment
Share on other sites


Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...