keifer
keifer

Reputation: 155

How to run multiple Unix commands in one time?

I'm still new to Unix. Is it possible to run multiple commands of Unix in one time? Such as write all those commands that I want to run in a file, then after I call that file, it will run all the commands inside that file? or is there any way(or better) which i do not know?

Thanks for giving all the comments and suggestions, I will appreciate it.

Upvotes: 12

Views: 88600

Answers (8)

SultanLegend
SultanLegend

Reputation: 555

To have the commands actually run at the same time you can use the job ability of zsh

$ zsh -c "[command1] [command1 arguments] & ; [command2] [command2 arguments]"

Or if you are running zsh as your current shell:

$ ping google.com & ; ping 127.0.0.1

The ; is a token that lets you put another command on the same line that is run directly after the first command.

The & is a token placed after a command to run it in the background.

Upvotes: 0

crazy_prog
crazy_prog

Reputation: 1099

If you want to use multiple commands at command line, you can use pipes to perform the operations.

grep "Hello" <file-name> | wc -l

It will give number of times "Hello" exist in that file.

Upvotes: 2

minhas23
minhas23

Reputation: 9671

We can run multiple commands in shell by using ; as separator between multiple commands

For example,

ant clean;ant

If we use && as separator then next command will be running if last command is successful.

Upvotes: 7

Kernal
Kernal

Reputation: 53

you can also use a semicolon ';' and run multiple commands, like : $ls ; who

Upvotes: 3

Jack BeNimble
Jack BeNimble

Reputation: 36713

Sure. It's called a "shell script". In bash, put all the commands in a file with the suffix "sh". Then run this:

chmod +x myfile.sh

then type

. ./myFile

or

source ./myfile

or just

./myfile

Upvotes: 0

Sodved
Sodved

Reputation: 8607

Yep, just put all your commands in one file and then

bash filename

This will run the commands in sequence. If you want them all to run in parallel (i.e. don't wait for commands to finish) then add an & to the end of each line in the file

Upvotes: 2

adlawson
adlawson

Reputation: 6431

echo 'hello' && echo 'world'

Just separate your commands with &&

Upvotes: 22

baraboom
baraboom

Reputation: 1408

Short answer is, yes. The concept is known as shell scripting, or bash scripts (a common shell). In order to create a simple bash script, create a text file with this at the top:

#!/bin/bash

Then paste your commands inside of it, one to a line.

Save your file, usually with the .sh extension (but not required) and you can run it like:

sh foo.sh

Or you could change the permissions to make it executable:

chmod u+x foo.sh

Then run it like:

./foo.sh

Lots of resources available on this site and the web for more info, if needed.

Upvotes: 24

Related Questions