How to run multiple terminal commands in one line in Ubuntu / Linux?

Writing multiple commands together is known as Command Chaining, in this article I will show you different methods to combine multiple commands on terminal / shell.

Using Semicolon (;)

If you combine multiple commands using semicolon (;) operator, commands will execute even if the previous command in chain succeeds or fails.

In following example command2 will execute regardless of the command1’s success.

command1;command2

Here is a more practical example.

ping google.com -c 3; whoami; pwd

Above chain of commands will:

  • ping: ping google.com 3 times.
  • whoami: print user name of currently logged in user.
  • pwd: print current working directory.

Using AND Operator (&&)

Chaining multiple commands using AND (&&) operator will execute the command only and only if preceding command executes successfully.

command2 from following snippet will only execute if command1 runs successfully.

command1 && command2

From following listing the cd newDirectory command will only execute if mkdir newDirectory command was successful.

mkdir newDirectory && cd newDirectory

Using OR Operator (||)

Using OR ( ) operator makes sure that the following command executes only and only if preceding command fails.

command2 from following snippet will only execute if command1 fails to execute.

command1 || command2

From following example the echo command will only execute if ping command fails which means you are facing internet connectivity issue.

ping -c 3 google.com  || echo "You are not connected to internet"

Using NOT Operator (!)

You can use NOT (!) operator to exclude some results, following is an example command to delete all files in a directory except with .text extension.

Important Note: if you are trying this example, please do it in a practice / temporary directory to avoid deletion of actual / important work files.

rm -r !(*.text)

Combining AND (&&) and OR (||) operators

You can combine AND (&&) and OR (||)operators to simulate if ... elsescenario, let’s rewrite the *OR ( ) operator* example to print both SUCCESS and FAILURE message for internet connectivity check.
ping -c 3 google.com1 && echo "Your internet connection is OK"  || echo "You are not connected to internet"

That’s it, hope you enjoyed it. This was part one of using operators on linux command line shell /terminal, in next part of this article I will show more operators to enhance your productivity while working with linux shell commands. You like this article, have some questions or suggestions please let us know in the comments section.

Thanks and Happy Learning!

Shoket Mahmood Ahmed

57 thoughts on “How to run multiple terminal commands in one line in Ubuntu / Linux?

  1. If you are going for most excellent contents like I do, only go to see this web site every day since it provides feature contents, thanks

  2. It’s in fact very complicated in this active life to listen news on TV,
    thus I just use world wide web for that purpose, and obtain the most up-to-date news.

  3. Thanks for the good writeup. It in fact was once a leisure account it.
    Glance complicated to far delivered agreeable from you!

    By the way, how can we keep in touch?

  4. Hello there, I discovered your blog by the use
    of Google whilst looking for a related subject,
    your web site got here up, it looks good. I have bookmarked
    it in my google bookmarks.
    Hi there, just became alert to your blog thru
    Google, and located that it’s truly informative.
    I’m going to be careful for brussels. I’ll be grateful when you
    continue this in future. Lots of other folks will probably be benefited out of your writing.
    Cheers!

  5. Hey I know this is off topic but I was wondering if you knew of any widgets I could add
    to my blog that automatically tweet my newest twitter
    updates. I’ve been looking for a plug-in like this for quite some time and was hoping maybe you
    would have some experience with something like this.
    Please let me know if you run into anything. I truly enjoy
    reading your blog and I look forward to your new updates.

  6. Exceptional post however I was wondering if you could write a
    litte more on this topic? I’d be very thankful if you could elaborate a little bit further.
    Thanks!

  7. What i don’t realize is in truth how you’re not really
    a lot more neatly-preferred than you might be right now.
    You’re so intelligent. You know therefore considerably in relation to this
    matter, made me in my view consider it from a lot of varied angles.
    Its like men and women aren’t fascinated unless it’s one thing
    to accomplish with Lady gaga! Your own stuffs nice.
    All the time care for it up!

  8. Unquestionably consider that which you stated. Your favorite justification appeared
    to be at the internet the easiest factor to take note of.

    I say to you, I certainly get irked even as other folks think about issues that they plainly don’t recognise about.
    You controlled to hit the nail upon the top as well as defined out the entire thing with no need
    side effect , other folks can take a signal.
    Will probably be again to get more. Thanks

  9. When I initially left a comment I appear to have clicked on the -Notify me when new comments are added- checkbox
    and now whenever a comment is added I recieve 4 emails with the same comment.
    There has to be a way you can remove me from that service?
    Thanks!

    My homepage xe88 best game

  10. What’s Happening i’m new to this, I stumbled upon this I have found It
    absolutely useful and it has helped me out loads.
    I hope to contribute & assist other customers
    like its aided me. Great job.

  11. I am really impressed with your writing skills as well as with
    the layout on your weblog. Is this a paid theme or did you customize it yourself?

    Either way keep up the excellent quality writing,
    it is rare to see a great blog like this one these days.

  12. We stumbled over here different web address and thought I might as well check things out.
    I like what I see so i am just following you. Look forward to looking over your web page
    repeatedly.

  13. I’m really enjoying the design and layout of your blog.
    It’s a very easy on the eyes which makes it much more pleasant for me to
    come here and visit more often. Did you hire out a developer
    to create your theme? Exceptional work!

  14. Awesome blog you have here but I was curious if you knew of any message boards that cover
    the same topics discussed in this article?
    I’d really like to be a part of community where I can get opinions from other experienced people that
    share the same interest. If you have any recommendations, please
    let me know. Cheers!

    Stop by my blog post :: mega888 for ios

  15. I am actually happy to read this web site posts which
    carries lots of helpful information, thanks for providing these kinds of information.

  16. I really love your blog.. Great colors & theme.
    Did you build this site yourself? Please reply back as
    I’m attempting to create my very own blog and want to know
    where you got this from or what the theme is
    called. Cheers!

  17. Do you mind if I quote a few of your articles as long
    as I provide credit and sources back to your blog?
    My blog site is in the exact same area of interest as
    yours and my users would genuinely benefit from a lot of the
    information you present here. Please let me know if this alright with you.
    Appreciate it!

  18. I’m extremely pleased to uncover this site.
    I wanted to thank you for your time for this particularly wonderful read!!
    I definitely liked every little bit of it and i also
    have you saved as a favorite to check out new information on your site.

  19. Do you mind if I quote a couple of your posts as long as I provide credit
    and sources back to your webpage? My website is in the very same niche
    as yours and my visitors would really benefit from some of the information you provide here.
    Please let me know if this alright with you.

    Many thanks!

  20. I seriously love your website.. Excellent colors & theme.

    Did you make this website yourself? Please reply back as I’m wanting to
    create my own site and want to know where you got this from or what the theme is called.
    Thank you!

  21. Hi, I think your site could be having internet browser compatibility issues.
    When I take a look at your site in Safari, it looks fine however when opening in I.E., it’s got some overlapping
    issues. I merely wanted to give you a quick heads up!
    Apart from that, fantastic site!

  22. It’s a pity you don’t have a donate button! I’d certainly donate to this excellent blog!

    I guess for now i’ll settle for bookmarking and adding your RSS feed to my Google account.
    I look forward to fresh updates and will talk about this site with my Facebook
    group. Chat soon!

  23. Great work! This is the type of info that are meant to be shared
    across the web. Disgrace on Google for no longer positioning this submit upper!
    Come on over and discuss with my site . Thanks
    =)

  24. Thank you a lot for sharing this with all of us you actually recognize what you’re talking about!
    Bookmarked. Kindly also talk over with my site =).
    We will have a link change contract among us

  25. hello there and thank you for your info – I have certainly picked up something new from right here.

    I did however expertise a few technical points using this web site, as I experienced to reload the site
    many times previous to I could get it to load properly.
    I had been wondering if your web hosting is OK? Not that I’m
    complaining, but slow loading instances times will very frequently affect your placement
    in google and can damage your quality score if advertising and
    marketing with Adwords. Well I’m adding this RSS to
    my e-mail and can look out for much more of your respective fascinating
    content. Ensure that you update this again soon.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top