56tvmao: How-to instructions you can trust. Linux Be More Efficient By Executing Multiple Commands in Linux Simultaneously

Be More Efficient By Executing Multiple Commands in Linux Simultaneously

Want to save loads of time? Try executing multiple commands at once in your Linux terminal. This allows you to run simple sequential commands, execute commands in the background, and even handle conditional commands together.

In this post, we’ll explore various methods for executing multiple commands sequentially and in parallel on a Linux terminal.

Content

Why Run Multiple Commands at Once?

Whether you’re running simple maintenance tasks, handling large amounts of data, or just want to automate parts of your daily workflow, running multiple commands at once can optimize your processes and improve efficiency.

For example, if you’re backing up several folders using commands, you can chain them together instead of running each command separately and waiting for one to finish before typing the next.

Personally, I chain commands together when installing any Linux package that requires multiple modules. By chaining commands, I install the specific tool and its dependencies in one go without typing each command separately.

Run Multiple Commands in Succession

I use this technique most of the time when working in the terminal. In this method, we chain commands so they run one after the other, regardless of the previous command’s success.

To execute commands in succession, simply separate them with a semicolon ;. When you hit enter, your terminal runs the first command, waits for it to finish, then moves to the second, and so on.

For instance, if you want to upgrade your system and clean up unnecessary files simultaneously, run this:

sudo apt update; sudo apt upgrade -y; sudo apt autoremove

By chaining these commands, you ensure they run consecutively. Each step is completed before the next starts, making your process efficient and smooth.

Run Multiple Commands Based on Condition

Now, what if you want the second command to be carried out only if the first one succeeds? Or maybe you want a second command to run only if the first one fails. This is where conditional command execution comes in. You can use the && and || operators to achieve this.

Run if first command succeeded

To run a command only if the previous one succeeds, use the && operator between your commands.

For example, if you want to create a new folder and then change directories to it, only if the folder creation is successful, run:

mkdir new_folder && cd new_folder

However, if the first command fails, no further actions are taken.

Execute if first failed

On the other hand, if you want a second command to run only if the first one fails, use the || operator. This is useful for logging or fallback commands when something goes wrong.

I find this especially helpful in file operations, such as copying a file to a backup folder. If the copy fails, you want to know without interrupting your work. You can achieve this by using the || operator between two commands like this:

cp file_name /backup || echo "Failed!" >> error_log.txt

Here, the cp command tries to copy the file to the backup folder. However, if the copy fails, the || (or) symbol instructs the system to run the next command, which logs the error using the echo command. This way, you can continue working even if the copy fails, and you’ll have a record of the error to review later.

Run Command in Background

Sometimes, you don’t want to wait for a task to finish. Maybe it’s a long-running process, and you’d rather keep working while it happens in the background. You can do this easily by adding an ampersand & at the end of your command.

For example, if you want to download a large file from a specific URL and have the download process happen in the background, run:

sudo wget https://example.com/large-file.zip &

This allows you to continue using the terminal for other tasks while the download is still in progress.

Pipes and backgrounds the second command

Now, what if you want to chain two commands but have the second one run in the background while the first one finishes? To do this, use both the pipe | operator and the ampersand & symbol.

The pipe | sends the output of the first command as input to the second command, while the ampersand & allows the second command to run in the background.

Let’s read a large file and search for specific words within it. Then, save the results in a separate file using the redirect > operator:

cat samplefile.txt | grep "Project" > output.txt &

Here, Both the search and saving process will happen in the background. Moreover, while the search runs, you can continue working on other commands at the same time.

Run Multiple Commands in Parallel With Xargs

Want to take multitasking to a new level? Try running commands in parallel using the xargs command. This allows you to run several commands in parallel instead of one after the other.

The xargs command also breaks a list of arguments into chunks and runs a command on each chunk in parallel.

For example, if you have a list of URLs in the text file that you want to download all at once. Instead of downloading them sequentially, you can download them in parallel with this command:

cat urls_file.txt | xargs -n 1 -P 4 wget

Here, -n 1 tells xargs to use one argument per command, and -P 4 tells it to run four commands in parallel. The wget command downloads each URL simultaneously, significantly speeding up the process compared to sequential downloading.

Note: Running too many commands at once can overwhelm your system. Always monitor your system’s load, as trying to run too many processes could cause it to crash.

Wrapping up

To recap, you can chain commands together with semicolons ;, run commands conditionally using && or ||, execute tasks in the background with &, and step up your multitasking with parallel execution through xargs.

Image credit: Unsplash. All alterations and screenshots by Haroon Javed.


Haroon Javed
Contributor

Haroon is a lifelong tech enthusiast with over five years of experience writing thousands of articles about Linux, programming languages, and more. He loves exploring new technologies and experimenting with them to find innovative ways to use them. Haroon’s work has been featured on various online platforms, including HTG, Baeldung, and LinuxHint.

Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox

Sign up for all newsletters.
By signing up, you agree to our Privacy Policy and European users agree to the data transfer policy. We will not share your data and you can unsubscribe at any time. Subscribe

Related Post