Be More Efficient By Executing Multiple Commands in Linux Simultaneously
Want to save multiple time? Try carrying out numerous commands at when in your Linux incurable. This permits you to dashed humble sequential commands, implement commands in the background, and also also collar treatment of conditional commands together.
In this post, we’ll dissect numerous strategies for carrying out numerous commands sequentially and also in parallel on a Linux incurable.
- Why Rushed Plenty of Commands at Once?
- Rushed Plenty of Commands in Succession
- Rushed Plenty of Commands Based on Ailment
- Rushed Command in Background
- Rushed Plenty of Commands in Parallel Through Xargs
Why Rushed Plenty of Commands at Once?
Whether you’re running humble upkeep work, taking care of vast quantities of documents, or purely want to automate parts of your everyday workflow, running numerous commands at when can render ideal utilise of your processes and also simplify practicality.
For example, if you’re market upward multiple folders wearing commands, you can chain them together instead of running each command independently and also waiting for one to coating in the past keying the next off.
Personally, I chain commands together when installing any kind of Linux tactic that necessitates numerous parts. By chaining commands, I mount the particular tool and also its dependencies in one go without keying each command independently.
Rushed Plenty of Commands in Succession
I utilise this tactic most of the time when working in the incurable. In this tactic, we chain commands so they dashed one after the polymorphous other, regardless of the previous command’s triumph.
To implement commands in sequence, purely ensconced unconcerned them wearing a semicolon ;
. When you hit amass in, your incurable runs the first command, waits for it to coating, then acts to the second, and also so on.
For instance, if you want to upgrade your mechanism and also untainted upward unnecessary files simultaneously, dashed this:
sudo apt update; sudo apt upgrade -y; sudo apt autoremove

By chaining these commands, you make certain they dashed uninterruptedly. Each note is ended up in the past the next off propels, gaining your process definite and also centralized.
Rushed Plenty of Commands Based on Ailment
Presently, what if you want the second command to be towed out only if the first one does well? Or perhaps you want a second command to dashed only if the first one fails. This is in which conditional command masterstroke comes in. You can utilise the &&
and also ||
operators to accomplish this.
Rushed if first command done well
To dashed a command only if the previous one does well, utilise the &&
licensed operator in between your commands.
For example, if you want to inflict a neoteric folder and also then adjust catalogs to it, only if the folder furtherance does well, dashed:
mkdir new_folder && cd new_folder

But, if the first command fails, no better acts are taken.
Implement if first failed
Meanwhile, if you want a second command to dashed only if the first one fails, utilise the ||
licensed operator. This is rewarding for logging or contingency commands when something goes dishonorable.
I situate this specifically rewarding in file process, such as photocopying a file to a fallback folder. If the xerox fails, you want to expired upward being aware without interfering with your work. You can accomplish this by wearing the ||
licensed operator in between 2 commands pick this:
cp file_name /backup || echo "Failed!" >> error_log.txt
Below, the cp
command tries to xerox the file to the fallback folder. But, if the xerox fails, the ||
(or) symbol teaches the mechanism to dashed the next off command, which logs the misstep wearing the resemble command. This means, you can perpetuate working also if the xerox fails, and also you’ll have a file of the misstep to mull later.
Rushed Command in Background
Recurrently, you wear’t want to postpone for a openings to coating. Potentially it’s a long-running process, and also you’d rather preserve working while it confiscates territory in the background. You can do this comfortably by adding an ampersand &
at the run out of your command.
For example, if you want to download a vast file from a particular URL and also have the download process ensue in the background, dashed:
sudo wget https://example.com/large-file.zip &
This permits you to perpetuate wearing the incurable for polymorphous other work while the download is still in floater.
Hoses and also histories the second command
Presently, what if you want to chain 2 commands but have the second one dashed in the background while the first one coatings? To do this, utilise both the tube |
licensed operator and also the ampersand &
symbol.
The tube |
sends the result of the first command as input to the second command, while the ampersand &
permits the second command to dashed in the background.
Permit’s read a vast file and also search for particular words within it. Then, save the outputs in a ensconced unconcerned file wearing the reroute >
licensed operator:
cat samplefile.txt | grep "Project" > output.txt &

Below, Both the search and also keeping process will most clearly ensue in the background. In addition, while the search runs, you can perpetuate working on polymorphous other commands simultaneously.
Rushed Plenty of Commands in Parallel Through Xargs
Want to collar multitasking to a neoteric level? Try running commands in parallel wearing the xargs
command. This permits you to dashed multiple commands in parallel instead of one after the polymorphous other.
The xargs
command similarly respites a checklist of arguments proper into chunks and also runs a command on each portion in parallel.
For example, if you have a checklist of URLs in the message file that you want to download with one voice at when. Instead of downloading and also install them sequentially, you can download them in parallel wearing this command:
cat urls_file.txt | xargs -n 1 -P 4 wget
Below, -n 1
notifies xargs
to utilise one argument per command, and also -P 4
notifies it to dashed 4 commands in parallel. The wget command downloads each URL simultaneously, notably speeding upward the process contrasted to sequential downloading and also install.
Tab: Running also multiple commands at when can bewilder your mechanism. Always sport your mechanism’s boatload, as attempting to dashed also multiple processes could generate it to crash.
Summarizing
To recap, you can chain commands together wearing semicolons ;
, dashed commands conditionally wearing &&
or ||
, implement work in the background wearing &
, and also note upward your multitasking wearing parallel masterstroke wearing xargs
.
Image credit report: Unsplash. Unanimously alterations and also screenshots by Haroon Javed.