Today’s article is about how you can string commands together in the terminal. You can do so in a few different ways, for a few different reasons. This article will explain some of those to you, hopefully making your life easier.
There are a number of reasons why you’d want to string commands together. I find it easier to just run certain commands together. I also find it easier for some aliases. (Speaking of which, I really need to do an article about aliases!)
For example, you might want to run change to the Foo directory and then list the files inside it. Well, one way to write that command would be cd Foo && ls -la
. In that case, you’d change the directory to Foo and then you’d list all the files within that directory. As I said, there are a number of ways to do this and a number of reasons why.
This article won’t be all that long – or even all that difficult. But, this is a pretty basic skill you’d want to have if you’re working in the terminal. It’s one of those tools you’ll (maybe) find yourself using more often once you’re used to it. I, for one, use it with some frequency. You can string commands together and then go make coffee!
Obviously, you’re going to need an open terminal. If you don’t know how to open the terminal, you can do so with your keyboard – just press CTRL + ALT + T and your default terminal should open.
With your terminal open, you’ll want one of the following operators:
;
run the next command no matter what.
&&
run the next command only if the first one succeeds.
||
run the next command only if the first one failed to run.
The ;
operator can be used something like this:
cd Downloads ; touch Foo
Them the &&
operator can be used something like this:
sudo apt update && sudo apt upgrade -y
Finally, the ||
operator can be used something like this:
cd Foo || mkdir Foo
You could string them along something like:
cd Foo || mkdir Foo && cd Foo ; ls -la
See? It’s not all that complicated. You can also swap the ; operator for a single ampersand. It’ll do the same thing, run the following command regardless of the results of the first. So, cd Downloads ; touch foo
and cd Downloads & touch foo
are functionally the same.
Yup, another article – said and done. This one is, as promised, not all that difficult or even all that long. Still, I hope you’ve learned something and know now how to string commands together in the terminal. It’s a pretty useful skill to have. Feel free to leave a comment explaining how you already use these operators to string commands together.
Thanks for reading! If you want to help, or if the site has helped you, you can donate, register to help, write an article, or buy inexpensive hosting to start your own site. If you scroll down, you can sign up for the newsletter, vote for the article, and comment.
Today we'll cover one way to enable or disable your network interface in the Linux…
Today's exercise is a nice and simple exercise where we check your NIC speed in…
Have you ever wanted to easily monitor your wireless connection? Well, now you can learn…
I think I've covered this before with the ls command but this time we'll count…
Today we'll be learning about a basic Linux command that's known as 'uname' and it…
If you've used hardinfo in the past, it may interest you to know that hardinfo…
View Comments
I know it might be out of the scope of your article but it might be useful to add that when used in an if statement that || means "or" and && means "and". And there's also true and false expressions where they can be used.