Hey there! If you’ve spent any time with Linux, you know the command line is where the magic happens.
But do you ever feel like you’re stuck in a slow-motion loop? You type a command, hit Enter, wait for it to finish, and then type the next one.
It works, but it’s not exactly fast, is it?
What if I told you there’s a much smarter way to work? You can actually tell your terminal to run a whole series of commands, one after the other, all with a single line of code. It’s like giving your computer a to-do list and letting it get to work.
Don’t worry, this isn’t some advanced, complicated trick. It’s a straightforward process that will completely change how you use Linux. In this short guide, we’ll walk through three simple methods to chain your commands together. Let’s dive in!
Table of Contents
Method 1: The Semicolon (;) — The Trusty Sequence
First up is the semicolon. It’s the simplest and most direct way to run multiple commands.
Think of the semicolon as saying, “Do this, and then do this, and then do this.” It runs each command in order, and it doesn’t care if the previous one was successful or not. It just keeps going down the line.
Let’s look at a common scenario: you want to go to your project folder and then list all the files inside.
Instead of doing it in two steps, you can use a semicolon:
Bash
See also: Mastering the Linux Command Line — Your Complete Free Training Guide
cd /home/user/myproject; ls -l
What’s happening here?
cd /home/user/myproject: First, the terminal navigates to your project directory.;: The semicolon tells the shell, “Okay, that’s done. Now for the next one.”ls -l: Then, it immediately runs thels -lcommand to show you a detailed list of all the files in that directory.
It’s a simple and clean way to group together commands that don’t depend on each other.
Great, that’s the first one down! See? Not so bad. Now, what if you need to be a bit more careful? What if you only want the next command to run if the first one actually worked?
That brings us to our next method.
Method 2: The Double Ampersand (&&) — The Smart Successor
This one is my personal favorite. The && operator is a “logical AND.” That might sound technical, but the idea is super simple.
Think of && as saying: “Do this, and only if it succeeds, do the next thing.”
This is incredibly useful for tasks where one step absolutely must work before the next one begins. A perfect example is updating your system. You want to make sure you successfully fetch the latest package lists before you try to upgrade anything.
Here’s how you’d do it:
Bash
sudo apt update && sudo apt upgrade -y
Let’s break it down:
sudo apt update: This command refreshes your system’s list of available software packages.&&: The magic part! The shell will now check if theupdatecommand finished without any errors.sudo apt upgrade -y: If—and only if—the update was successful, this command will start upgrading your packages.
If the first command fails (maybe your internet is down), the second command won’t even try to run. This prevents errors and gives you a much more reliable way to automate your tasks. It’s a real lifesaver!
You’re doing awesome. We’ve covered how to run commands in sequence and how to run them based on success. But what about the opposite? What if you want to run a command only when something fails?
Let’s get into our final and very clever method.
Method 3: The Double Pipe (||) — The Helpful Backup Plan
The || operator is a “logical OR.” It’s the perfect tool for creating a backup plan.
Think of || as saying: “Try to do this, but if it fails, do this other thing instead.”
This is fantastic for handling errors gracefully. For example, maybe you want to create a new directory. If that directory already exists, the mkdir command will fail. You can use || to print a friendly message in that case.
Check out this example:
Bash
mkdir my_new_folder || echo "Folder already exists, skipping."
Here’s the logic:
mkdir my_new_folder: The system tries to create a new directory namedmy_new_folder.||: The shell checks the result. Did the command fail?echo "Folder already exists, skipping.": If themkdircommand failed (likely because the folder is already there), thisechocommand will run and display a helpful message on your screen.
If the mkdir command succeeds, the echo command is completely ignored. It’s a simple and elegant way to build in fallbacks and make your scripts more user-friendly.
You’ve Leveled Up!
And that’s it! You’ve just learned three incredibly useful techniques to make your command line experience faster and smarter.
Let’s do a quick recap:
- Semicolon (;): Runs commands one after another, no matter what.
- Double Ampersand (&&): Runs the next command only if the previous one was successful.
- Double Pipe (||): Runs the next command only if the previous one failed.
Go ahead and give these a try in your own terminal. Start combining commands and see how much time you can save. You’re well on your way to becoming a Linux power user!
Got any other favorite command-line tricks? Share them in the comments below!



